Merge pull request #3852 from evazion/fix-twitter-direct-url
Twitter: fix handling of direct image urls without a referer url.
This commit is contained in:
@@ -24,6 +24,8 @@ module Sources::Strategies
|
|||||||
def image_urls
|
def image_urls
|
||||||
if url =~ /(#{ASSET}[^:]+)/
|
if url =~ /(#{ASSET}[^:]+)/
|
||||||
return [$1 + ":orig" ]
|
return [$1 + ":orig" ]
|
||||||
|
elsif api_response.blank?
|
||||||
|
return [url]
|
||||||
end
|
end
|
||||||
|
|
||||||
[url, referer_url].each do |x|
|
[url, referer_url].each do |x|
|
||||||
@@ -31,8 +33,6 @@ module Sources::Strategies
|
|||||||
return service.image_urls(api_response)
|
return service.image_urls(api_response)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
rescue Twitter::Error::NotFound
|
|
||||||
url
|
|
||||||
end
|
end
|
||||||
memoize :image_urls
|
memoize :image_urls
|
||||||
|
|
||||||
@@ -51,17 +51,24 @@ module Sources::Strategies
|
|||||||
if $1 != "i"
|
if $1 != "i"
|
||||||
return "https://twitter.com/#{$1}"
|
return "https://twitter.com/#{$1}"
|
||||||
end
|
end
|
||||||
|
elsif artist_name.present?
|
||||||
|
"https://twitter.com/" + artist_name
|
||||||
|
else
|
||||||
|
""
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
"https://twitter.com/" + api_response.attrs[:user][:screen_name]
|
def artists
|
||||||
rescue Twitter::Error::NotFound
|
if profile_url.present?
|
||||||
nil
|
Artist.find_artists(profile_url)
|
||||||
|
else
|
||||||
|
[]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def artist_name
|
def artist_name
|
||||||
|
return "" if api_response.blank?
|
||||||
api_response.attrs[:user][:screen_name]
|
api_response.attrs[:user][:screen_name]
|
||||||
rescue Twitter::Error::NotFound
|
|
||||||
nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def artist_commentary_title
|
def artist_commentary_title
|
||||||
@@ -69,9 +76,8 @@ module Sources::Strategies
|
|||||||
end
|
end
|
||||||
|
|
||||||
def artist_commentary_desc
|
def artist_commentary_desc
|
||||||
|
return "" if api_response.blank?
|
||||||
api_response.attrs[:full_text]
|
api_response.attrs[:full_text]
|
||||||
rescue Twitter::Error::NotFound
|
|
||||||
nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def normalizable_for_artist_finder?
|
def normalizable_for_artist_finder?
|
||||||
@@ -79,10 +85,12 @@ module Sources::Strategies
|
|||||||
end
|
end
|
||||||
|
|
||||||
def normalize_for_artist_finder
|
def normalize_for_artist_finder
|
||||||
profile_url.downcase
|
profile_url.try(:downcase)
|
||||||
end
|
end
|
||||||
|
|
||||||
def tags
|
def tags
|
||||||
|
return [] if api_response.blank?
|
||||||
|
|
||||||
api_response.attrs[:entities][:hashtags].map do |text:, indices:|
|
api_response.attrs[:entities][:hashtags].map do |text:, indices:|
|
||||||
[text, "https://twitter.com/hashtag/#{text}"]
|
[text, "https://twitter.com/hashtag/#{text}"]
|
||||||
end
|
end
|
||||||
@@ -90,6 +98,8 @@ module Sources::Strategies
|
|||||||
memoize :tags
|
memoize :tags
|
||||||
|
|
||||||
def dtext_artist_commentary_desc
|
def dtext_artist_commentary_desc
|
||||||
|
return "" if artist_commentary_desc.blank?
|
||||||
|
|
||||||
url_replacements = api_response.urls.map do |obj|
|
url_replacements = api_response.urls.map do |obj|
|
||||||
[obj.url.to_s, obj.expanded_url.to_s]
|
[obj.url.to_s, obj.expanded_url.to_s]
|
||||||
end
|
end
|
||||||
@@ -116,6 +126,8 @@ module Sources::Strategies
|
|||||||
|
|
||||||
def api_response
|
def api_response
|
||||||
service.client.status(status_id, tweet_mode: "extended")
|
service.client.status(status_id, tweet_mode: "extended")
|
||||||
|
rescue ::Twitter::Error::NotFound
|
||||||
|
{}
|
||||||
end
|
end
|
||||||
memoize :api_response
|
memoize :api_response
|
||||||
|
|
||||||
|
|||||||
@@ -143,6 +143,25 @@ module Sources
|
|||||||
end
|
end
|
||||||
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
|
context "The source site for a https://twitter.com/i/web/status/:id url" do
|
||||||
setup do
|
setup do
|
||||||
skip "Twitter key is not set" unless Danbooru.config.twitter_api_key
|
skip "Twitter key is not set" unless Danbooru.config.twitter_api_key
|
||||||
|
|||||||
Reference in New Issue
Block a user