Module: Processors::PhotoService

Includes:
LogAware
Defined in:
lib/processors/photo_service.rb

Overview

utilities for dealing with photo services

Constant Summary collapse

LOCATION_START_INDICATOR =
'Location: '
LOCATION_STOP_INDICATOR =
"\r\n"
PIC_REGEXP =
/(.*?)\.(jpg|jpeg|png|gif)/i

Class Method Summary collapse

Methods included from LogAware

log=

Class Method Details

.extract_id(link) ⇒ Object

extract the pic id from a given link



158
159
160
# File 'lib/processors/photo_service.rb', line 158

def self.extract_id(link)
  link.split('/').last if link.split('/')
end

.find_image_url(link, embedly_key = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/processors/photo_service.rb', line 19

def self.find_image_url(link, embedly_key=nil)
  url = nil
  if link && !(photo? link)
    url = image_url_eyeem link if link.index 'eyeem.com'
    url = image_url_instagram link if (link.index('instagr.am') || link.index('instagram.com'))
    url = image_url_picplz link if link.index 'picplz'
    url = image_url_twitpic link if link.index 'twitpic'
    url = image_url_yfrog link if link.index 'yfrog'
    url = image_url_imgly link if link.index 'img.ly'
    url = image_url_tco link, embedly_key if link.index 't.co'
    url = image_url_twimg link if link.index 'twitter.com'
    url = image_url_lockerz link if link.index 'lockerz.com'
    url = image_url_path link if link.index 'path.com'
    url = image_url_foursqaure link if link.index '4sq.com'
    url = image_url_embedly link, embedly_key if url.nil? #just try embed.ly for anything else. could do all image url processing w/ embedly, but there's probably some kind of rate limit invovled.
  elsif photo? link
    url = link
  end
  url
end

.image_url_embedly(link_url, key) ⇒ Object

find the image’s url via embed.ly



76
77
78
79
80
81
82
83
# File 'lib/processors/photo_service.rb', line 76

def self.image_url_embedly(link_url, key)
  response = Processors::Http::http_get_json "http://api.embed.ly/1/oembed?key=#{key}&url=#{link_url}"
  log.debug "embedly call: http://api.embed.ly/1/oembed?key=#{key}&url=#{link_url}"
  if response && response['type'] == 'photo'
    image_url = response['url'] 
  end
  image_url
end

.image_url_eyeem(link_url) ⇒ Object

extract the image of an eyeem.com pic



51
52
53
54
55
56
57
# File 'lib/processors/photo_service.rb', line 51

def self.image_url_eyeem(link_url)
  service_url = link_url_redirect link_url #follow possible redirects
  link_url = service_url if service_url #if there's no redirect, service_url will be nil
  response = Processors::Http::http_get link_url
  image_url = parse_html_for '.viewport-pic img', Nokogiri::HTML.parse(response.body_str)
  return image_url
end

.image_url_foursqaure(link_url) ⇒ Object

extract the image of a foursquare.com pic



59
60
61
62
63
64
65
# File 'lib/processors/photo_service.rb', line 59

def self.image_url_foursqaure(link_url)
  service_url = link_url_redirect link_url #follow possible redirects
  link_url = service_url if service_url #if there's no redirect, service_url will be nil
  response = Processors::Http::http_get link_url
  image_url = parse_html_for '.commentPhoto img', Nokogiri::HTML.parse(response.body_str)
  return image_url
end

.image_url_imgly(link_url) ⇒ Object

find the image’s url for a img.ly link



125
126
127
# File 'lib/processors/photo_service.rb', line 125

def self.image_url_imgly(link_url)
  image_url_redirect link_url, "http://img.ly/show/full/", "\r\n"
end

.image_url_instagram(link_url) ⇒ Object

find the image’s url for an instagram link



95
96
97
98
99
# File 'lib/processors/photo_service.rb', line 95

def self.image_url_instagram(link_url)
  link_url['instagram.com'] = 'instagr.am' if link_url.index 'instagram.com' #instagram's oembed does not work for .com links
  response = Processors::Http::http_get_json "http://api.instagram.com/oembed?url=#{link_url}"
  response['url'] if response
end

.image_url_lockerz(link_url) ⇒ Object

find the image’s url for a lockerz link



85
86
87
88
# File 'lib/processors/photo_service.rb', line 85

def self.image_url_lockerz(link_url)
  response = Processors::Http::http_get_json "http://api.plixi.com/api/tpapi.svc/json/metadatafromurl?details=false&url=#{link_url}"
  response["BigImageUrl"] if response
end

.image_url_path(link_url) ⇒ Object

extract the image of a path.com pic



67
68
69
70
71
72
73
# File 'lib/processors/photo_service.rb', line 67

def self.image_url_path(link_url)
  service_url = link_url_redirect link_url #follow possible redirects
  link_url = service_url if service_url #if there's no redirect, service_url will be nil
  response = Processors::Http::http_get link_url
  image_url = parse_html_for 'img.photo-image', Nokogiri::HTML.parse(response.body_str)
  return image_url
end

.image_url_picplz(link_url) ⇒ Object

find the image’s url for a picplz short/longlink



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/processors/photo_service.rb', line 102

def self.image_url_picplz(link_url)
  id = extract_id link_url
  #try short url
  response = Processors::Http::http_get_json "http://picplz.com/api/v2/pic.json?shorturl_ids=#{id}"
  #if short url fails, try long url
  #response = HTTParty.get "http://picplz.com/api/v2/pic.json?longurl_ids=#{id}"
  #extract url
  if response && response['value'] && response['value']['pics'] && response['value']['pics'].first && response['value']['pics'].first['pic_files'] && response['value']['pics'].first['pic_files']['640r']
    response['value']['pics'].first['pic_files']['640r']['img_url'] 
  else
    nil
  end
end

.image_url_redirect(link_url, service_endpoint, stop_indicator = LOCATION_STOP_INDICATOR) ⇒ Object

extract image url from services like twitpic & img.ly that do not offer oembed interfaces



130
131
132
# File 'lib/processors/photo_service.rb', line 130

def self.image_url_redirect(link_url, service_endpoint, stop_indicator = LOCATION_STOP_INDICATOR)
  link_url_redirect "#{service_endpoint}#{extract_id link_url}", stop_indicator
end

.image_url_tco(link_url, embedly_key = nil) ⇒ Object

find the image’s url for an twitter shortened link



90
91
92
93
# File 'lib/processors/photo_service.rb', line 90

def self.image_url_tco(link_url, embedly_key = nil)
  service_url = link_url_redirect link_url
  find_image_url service_url, embedly_key
end

.image_url_twimg(link_url) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/processors/photo_service.rb', line 43

def self.image_url_twimg(link_url)
  service_url = link_url_redirect link_url #follow possible redirects
  link_url = service_url if service_url #if there's no redirect, service_url will be nil
  response = Processors::Http::http_get(link_url)
  image_url = parse_html_for '.twimg img', Nokogiri::HTML.parse(response.body_str)
  return image_url
end

.image_url_twitpic(link_url) ⇒ Object

find the image’s url for a twitpic link



116
117
118
# File 'lib/processors/photo_service.rb', line 116

def self.image_url_twitpic(link_url)
  image_url_redirect link_url, "http://twitpic.com/show/full/"
end

.image_url_yfrog(link_url) ⇒ Object

find the image’S url for a yfrog link



120
121
122
123
# File 'lib/processors/photo_service.rb', line 120

def self.image_url_yfrog(link_url)
  response = Processors::Http::http_get_json("http://www.yfrog.com/api/oembed?url=#{link_url}")
  response['url'] if response
end


134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/processors/photo_service.rb', line 134

def self.link_url_redirect(short_url, stop_indicator = LOCATION_STOP_INDICATOR)
  tries = 3
  begin
    resp = Curl::Easy.http_get(short_url) { |res| res.follow_location = true }
  rescue Curl::Err::CurlError => err
      log.error "Curl::Easy.http_get failed: #{err}"
      tries -= 1
      sleep 3
      if tries > 0
          retry
      else
         return nil
      end
  end
  if(resp && resp.header_str && resp.header_str.index(LOCATION_START_INDICATOR) && resp.header_str.index(stop_indicator))
    start = resp.header_str.index(LOCATION_START_INDICATOR) + LOCATION_START_INDICATOR.size
    stop  = resp.header_str.index(stop_indicator, start)
    resp.header_str[start...stop]
  else
    nil
  end
end

.logObject



15
16
17
# File 'lib/processors/photo_service.rb', line 15

def self.log
  LogAware.log #TODO why doesn't the include make the log method accessible?
end

.parse_html_for(element_signature, html_doc) ⇒ Object

parse html doc for element signature



162
163
164
165
166
167
168
169
170
171
# File 'lib/processors/photo_service.rb', line 162

def self.parse_html_for(element_signature, html_doc)
  image_url= nil
  if html_doc
    photo_container_div = html_doc.css(element_signature)
    if photo_container_div && photo_container_div.first && photo_container_div.first.attributes["src"]
      image_url = photo_container_div.first.attributes["src"].value
    end
  end
  image_url
end

.photo?(link) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/processors/photo_service.rb', line 40

def self.photo?(link)
  link =~ PIC_REGEXP
end