From a1044dbc1972e06a85f708def1bb15986ce769dd Mon Sep 17 00:00:00 2001 From: evazion Date: Wed, 29 Aug 2018 16:16:12 -0500 Subject: [PATCH] twitter: fix handling of direct image urls without a referer url. --- app/logical/sources/strategies/twitter.rb | 32 ++++++++++++++++------- test/unit/sources/twitter_test.rb | 19 ++++++++++++++ 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/app/logical/sources/strategies/twitter.rb b/app/logical/sources/strategies/twitter.rb index 719f40cb6..c5520fdb2 100644 --- a/app/logical/sources/strategies/twitter.rb +++ b/app/logical/sources/strategies/twitter.rb @@ -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 diff --git a/test/unit/sources/twitter_test.rb b/test/unit/sources/twitter_test.rb index 08068646d..f6a0272eb 100644 --- a/test/unit/sources/twitter_test.rb +++ b/test/unit/sources/twitter_test.rb @@ -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