Module: Tweetlr::Processors::PhotoService
- Includes:
- LogAware
- Defined in:
- lib/tweetlr/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
-
.extract_id(link) ⇒ Object
extract the pic id from a given
link
. - .find_image_url(link, embedly_key = nil) ⇒ Object
-
.image_url_embedly(link_url, key) ⇒ Object
find the image’s url via embed.ly.
-
.image_url_eyeem(link_url) ⇒ Object
extract the image of an eyeem.com pic.
-
.image_url_foursqaure(link_url) ⇒ Object
extract the image of a foursquare.com pic.
-
.image_url_imgly(link_url, embedly_key) ⇒ Object
find the image’s url for a img.ly link.
-
.image_url_instagram(link_url) ⇒ Object
find the image’s url for an instagram link.
-
.image_url_path(link_url) ⇒ Object
extract the image of a path.com pic.
-
.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.
-
.image_url_tco(link_url, embedly_key = nil) ⇒ Object
find the image’s url for an twitter shortened link.
- .image_url_twimg(link_url) ⇒ Object
-
.image_url_twitpic(link_url) ⇒ Object
find the image’s url for a twitpic link.
-
.image_url_yfrog(link_url) ⇒ Object
find the image’S url for a yfrog link.
- .link_url_redirect(short_url, stop_indicator = LOCATION_STOP_INDICATOR) ⇒ Object
- .log ⇒ Object
-
.parse_html_for(element_signature, html_doc, identifier = "src") ⇒ Object
parse html doc for element signature.
- .photo?(link) ⇒ Boolean
- .retrieve_image_url_by_css(link_url, css_path, selector = 'src') ⇒ Object
Methods included from LogAware
Class Method Details
.extract_id(link) ⇒ Object
extract the pic id from a given link
104 105 106 |
# File 'lib/tweetlr/processors/photo_service.rb', line 104 def self.extract_id(link) link.split('/').last if link.split('/') end |
.find_image_url(link, embedly_key = nil) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/tweetlr/processors/photo_service.rb', line 20 def self.find_image_url(link, =nil) url = nil if link && !(photo? link) url = process_link link, elsif photo? link url = link end url end |
.image_url_embedly(link_url, key) ⇒ Object
find the image’s url via embed.ly
52 53 54 55 56 57 58 59 60 |
# File 'lib/tweetlr/processors/photo_service.rb', line 52 def self.(link_url, key) link_url = follow_redirect(link_url) log.debug "embedly call: http://api.embed.ly/1/oembed?key=#{key}&url=#{link_url}" response = Tweetlr::Processors::Http::http_get_json "http://api.embed.ly/1/oembed?key=#{key}&url=#{link_url}" if response && (response['type'] == 'photo' || response['type'] == 'image') image_url = response['url'] end image_url end |
.image_url_eyeem(link_url) ⇒ Object
extract the image of an eyeem.com pic
37 38 39 |
# File 'lib/tweetlr/processors/photo_service.rb', line 37 def self.image_url_eyeem(link_url) retrieve_image_url_by_css link_url, '.viewport-pic img' end |
.image_url_foursqaure(link_url) ⇒ Object
extract the image of a foursquare.com pic
41 42 43 44 45 |
# File 'lib/tweetlr/processors/photo_service.rb', line 41 def self.image_url_foursqaure(link_url) link_url = follow_redirect(link_url) image_url = retrieve_image_url_by_css link_url, 'meta[property="og:image"]', 'content' image_url unless image_url.include? "foursquare.com/img/categories" end |
.image_url_imgly(link_url, embedly_key) ⇒ Object
find the image’s url for a img.ly link
81 82 83 |
# File 'lib/tweetlr/processors/photo_service.rb', line 81 def self.image_url_imgly(link_url, ) retrieve_image_url_by_css link_url, '#the-image' end |
.image_url_instagram(link_url) ⇒ Object
find the image’s url for an instagram link
67 68 69 70 71 |
# File 'lib/tweetlr/processors/photo_service.rb', line 67 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 = Tweetlr::Processors::Http::http_get_json "http://api.instagram.com/oembed?url=#{link_url}" response['url'] if response end |
.image_url_path(link_url) ⇒ Object
extract the image of a path.com pic
47 48 49 |
# File 'lib/tweetlr/processors/photo_service.rb', line 47 def self.image_url_path(link_url) retrieve_image_url_by_css link_url, 'img.photo-image' 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
86 87 88 |
# File 'lib/tweetlr/processors/photo_service.rb', line 86 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
62 63 64 65 |
# File 'lib/tweetlr/processors/photo_service.rb', line 62 def self.image_url_tco(link_url, = nil) service_url = link_url_redirect link_url find_image_url service_url, end |
.image_url_twimg(link_url) ⇒ Object
33 34 35 |
# File 'lib/tweetlr/processors/photo_service.rb', line 33 def self.image_url_twimg(link_url) retrieve_image_url_by_css link_url, '.media img' end |
.image_url_twitpic(link_url) ⇒ Object
find the image’s url for a twitpic link
73 74 75 |
# File 'lib/tweetlr/processors/photo_service.rb', line 73 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
77 78 79 |
# File 'lib/tweetlr/processors/photo_service.rb', line 77 def self.image_url_yfrog(link_url) retrieve_image_url_by_css link_url, '#input-direct', 'value' end |
.link_url_redirect(short_url, stop_indicator = LOCATION_STOP_INDICATOR) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/tweetlr/processors/photo_service.rb', line 90 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 (tries > 0) ? retry : return end process_reponse_header resp, stop_indicator end |
.log ⇒ Object
16 17 18 |
# File 'lib/tweetlr/processors/photo_service.rb', line 16 def self.log Tweetlr::LogAware.log #TODO why doesn't the include make the log method accessible? end |
.parse_html_for(element_signature, html_doc, identifier = "src") ⇒ Object
parse html doc for element signature
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/tweetlr/processors/photo_service.rb', line 108 def self.parse_html_for(element_signature, html_doc, identifier="src") 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[identifier] image_url = photo_container_div.first.attributes[identifier].value end end image_url end |
.photo?(link) ⇒ Boolean
30 31 32 |
# File 'lib/tweetlr/processors/photo_service.rb', line 30 def self.photo?(link) link =~ PIC_REGEXP end |
.retrieve_image_url_by_css(link_url, css_path, selector = 'src') ⇒ Object
118 119 120 121 122 123 |
# File 'lib/tweetlr/processors/photo_service.rb', line 118 def self.retrieve_image_url_by_css(link_url, css_path, selector='src') link_url = follow_redirect link_url response = Tweetlr::Processors::Http::http_get link_url image_url = parse_html_for css_path, Nokogiri::HTML.parse(response.body_str), selector return image_url end |