tumblr: don't fail when api data is unavailable (#3948).

The api data is unavailable when the work is deleted (bad_tumblr_id), or
when the source is a direct image url with no page referer.
This commit is contained in:
evazion
2018-10-09 11:57:23 -05:00
parent 4c55c809b0
commit 0c31a5d6a9
3 changed files with 23 additions and 27 deletions

View File

@@ -93,7 +93,7 @@ module Sources::Strategies
end
def tags
post[:tags].map do |tag|
post[:tags].to_a.map do |tag|
# normalize tags: space, underscore, and hyphen are equivalent in tumblr tags.
etag = tag.gsub(/[ _-]/, "_")
[etag, "https://tumblr.com/tagged/#{CGI.escape(etag)}"]
@@ -136,7 +136,7 @@ module Sources::Strategies
return list
end
raise "image url not found for (#{url}, #{referer_url})"
[]
end
# Look for the biggest available version on media.tumblr.com. A bigger
@@ -177,24 +177,26 @@ module Sources::Strategies
end
memoize :inline_images
def client
return {} unless self.class.enabled?
TumblrApiClient.new(Danbooru.config.tumblr_consumer_key)
end
memoize :client
def api_response
return {} unless self.class.enabled?
blog_name, post_id = self.class.parse_info_from_url(page_url)
raise "Page url not found for (#{url}, #{referer_url})" if blog_name.nil?
body, code = HttpartyCache.get("/#{blog_name}/posts",
params: { id: post_id, api_key: Danbooru.config.tumblr_consumer_key },
base_uri: "https://api.tumblr.com/v2/blog/"
)
client.posts(blog_name, post_id)
if code == 200
return JSON.parse(body, symbolize_names: true)
else
Rails.logger.debug("TumblrApiClient call failed (code=#{code}, body=#{body}, blog_name=#{blog_name}, post_id=#{post_id})")
return {}
end
end
memoize :api_response
def post
api_response[:posts].first
api_response.dig(:response, :posts)&.first || {}
end
end
end

View File

@@ -1,14 +0,0 @@
class TumblrApiClient < Struct.new(:api_key)
def posts(blog_name, post_id)
body, code = HttpartyCache.get("/#{blog_name}/posts",
params: { id: post_id, api_key: api_key },
base_uri: "https://api.tumblr.com/v2/blog/"
)
if code == 200
return JSON.parse(body)["response"].with_indifferent_access
end
raise "TumblrApiClient call failed (code=#{code}, body=#{body}, blog_name=#{blog_name}, post_id=#{post_id})"
end
end

View File

@@ -155,7 +155,7 @@ module Sources
should "get the image urls" do
urls = %w[
https://vtt.tumblr.com/tumblr_os31dkexhK1wsfqep.mp4
https://ve.media.tumblr.com/tumblr_os31dkexhK1wsfqep.mp4
https://media.tumblr.com/afed9f5b3c33c39dc8c967e262955de2/tumblr_inline_os31dclyCR1v11u29_1280.png
]
@@ -178,5 +178,13 @@ module Sources
assert_match("test answer", @site.artist_commentary_desc)
end
end
context "A deleted tumblr post" do
should "work" do
site = Sources::Strategies.find("http://shimetsukage.tumblr.com/post/176805588268/20180809-ssb-coolboy")
assert_nothing_raised { site.to_h }
end
end
end
end