refactor twitter service to handle cards (fixes #3031)

This commit is contained in:
r888888888
2017-05-09 12:46:40 -07:00
parent 2b74fe669e
commit ff5586cb01
3 changed files with 97 additions and 27 deletions

View File

@@ -17,25 +17,59 @@ class TwitterService
end
end
def extract_urls_for_status(tweet)
tweet.media.map do |obj|
if obj.is_a?(Twitter::Media::Photo)
obj.media_url.to_s + ":orig"
elsif obj.is_a?(Twitter::Media::Video)
video = obj.video_info.variants.select do |x|
x.content_type == "video/mp4"
end.max_by {|y| y.bitrate}
if video
video.url.to_s
end
end
end.compact
end
def extract_og_image_from_page(url, n = 5)
raise "too many redirects" if n == 0
Net::HTTP.start(url.host, url.port, :use_ssl => (url.normalized_scheme == "https")) do |http|
resp = http.request_get(url.request_uri)
if resp.is_a?(Net::HTTPMovedPermanently) && resp["Location"]
redirect_url = Addressable::URI.parse(resp["Location"])
redirect_url.host = url.host if redirect_url.host.nil?
redirect_url.scheme = url.scheme if redirect_url.scheme.nil?
redirect_url.port = url.port if redirect_url.port.nil?
return extract_og_image_from_page(redirect_url, n - 1)
elsif resp.is_a?(Net::HTTPSuccess)
doc = Nokogiri::HTML(resp.body)
images = doc.css("meta[property='og:image']")
return images.first.attr("content")
end
end
end
def extract_urls_for_card(attrs)
url = attrs.urls.map {|x| x.expanded_url}.reject {|x| x.host == "twitter.com"}.first
[extract_og_image_from_page(url)].compact
end
def image_urls(tweet_url)
tweet_url =~ %r{/status/(\d+)}
twitter_id = $1
attrs = client.status(twitter_id).attrs
tweet = client.status(twitter_id)
urls = []
attrs[:entities][:media].each do |obj|
urls << obj[:media_url] + ":orig"
end
attrs[:extended_entities][:media].each do |obj|
if obj[:video_info]
largest = obj[:video_info][:variants].select {|x| x[:url] =~ /\.mp4$/}.max_by {|x| x[:bitrate]}
urls.clear
urls << largest[:url] if largest
else
urls << obj[:media_url] + ":orig"
end
if tweet.media.any?
urls = extract_urls_for_status(tweet)
elsif tweet.urls.any?
urls = extract_urls_for_card(tweet)
end
urls.uniq
rescue => e
[]
# rescue => e
# []
end
end