twitter: fix handling of direct image urls without a referer url.

This commit is contained in:
evazion
2018-08-29 16:16:12 -05:00
parent cd11ae6008
commit a1044dbc19
2 changed files with 41 additions and 10 deletions

View File

@@ -24,6 +24,8 @@ module Sources::Strategies
def image_urls
if url =~ /(#{ASSET}[^:]+)/
return [$1 + ":orig" ]
elsif api_response.blank?
return [url]
end
[url, referer_url].each do |x|
@@ -31,8 +33,6 @@ module Sources::Strategies
return service.image_urls(api_response)
end
end
rescue Twitter::Error::NotFound
url
end
memoize :image_urls
@@ -47,21 +47,28 @@ module Sources::Strategies
end
def profile_url
return "" if api_response.blank?
if url =~ %r{\Ahttps?://(?:mobile\.)?twitter\.com/(\w+)}i
if $1 != "i"
return "https://twitter.com/#{$1}"
end
end
"https://twitter.com/" + api_response.attrs[:user][:screen_name]
rescue Twitter::Error::NotFound
nil
"https://twitter.com/" + artist_name
end
def artists
if profile_url.present?
Artist.find_artists(profile_url)
else
[]
end
end
def artist_name
return "" if api_response.blank?
api_response.attrs[:user][:screen_name]
rescue Twitter::Error::NotFound
nil
end
def artist_commentary_title
@@ -69,9 +76,8 @@ module Sources::Strategies
end
def artist_commentary_desc
return "" if api_response.blank?
api_response.attrs[:full_text]
rescue Twitter::Error::NotFound
nil
end
def normalizable_for_artist_finder?
@@ -79,10 +85,12 @@ module Sources::Strategies
end
def normalize_for_artist_finder
profile_url.downcase
profile_url.try(:downcase)
end
def tags
return [] if api_response.blank?
api_response.attrs[:entities][:hashtags].map do |text:, indices:|
[text, "https://twitter.com/hashtag/#{text}"]
end
@@ -90,6 +98,8 @@ module Sources::Strategies
memoize :tags
def dtext_artist_commentary_desc
return "" if artist_commentary_desc.blank?
url_replacements = api_response.urls.map do |obj|
[obj.url.to_s, obj.expanded_url.to_s]
end
@@ -116,6 +126,8 @@ module Sources::Strategies
def api_response
service.client.status(status_id, tweet_mode: "extended")
rescue ::Twitter::Error::NotFound
{}
end
memoize :api_response

View File

@@ -143,6 +143,25 @@ module Sources
end
end
context "The source site for a direct image url (pbs.twimg.com/media/*.jpg) without a referer url" do
setup do
skip "Twitter key is not set" unless Danbooru.config.twitter_api_key
@site = Sources::Strategies.find("https://pbs.twimg.com/media/B4HSEP5CUAA4xyu.png:large")
end
should "work" do
assert_equal("https://pbs.twimg.com/media/B4HSEP5CUAA4xyu.png:orig", @site.image_url)
assert_equal(["https://pbs.twimg.com/media/B4HSEP5CUAA4xyu.png:orig"], @site.image_urls)
assert(@site.artist_name.blank?)
assert(@site.profile_url.blank?)
assert(@site.artists.empty?)
assert(@site.tags.empty?)
assert(@site.artist_commentary_desc.blank?)
assert(@site.dtext_artist_commentary_desc.blank?)
assert_nothing_raised { @site.to_h }
end
end
context "The source site for a https://twitter.com/i/web/status/:id url" do
setup do
skip "Twitter key is not set" unless Danbooru.config.twitter_api_key