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:
@@ -93,7 +93,7 @@ module Sources::Strategies
|
|||||||
end
|
end
|
||||||
|
|
||||||
def tags
|
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.
|
# normalize tags: space, underscore, and hyphen are equivalent in tumblr tags.
|
||||||
etag = tag.gsub(/[ _-]/, "_")
|
etag = tag.gsub(/[ _-]/, "_")
|
||||||
[etag, "https://tumblr.com/tagged/#{CGI.escape(etag)}"]
|
[etag, "https://tumblr.com/tagged/#{CGI.escape(etag)}"]
|
||||||
@@ -136,7 +136,7 @@ module Sources::Strategies
|
|||||||
return list
|
return list
|
||||||
end
|
end
|
||||||
|
|
||||||
raise "image url not found for (#{url}, #{referer_url})"
|
[]
|
||||||
end
|
end
|
||||||
|
|
||||||
# Look for the biggest available version on media.tumblr.com. A bigger
|
# Look for the biggest available version on media.tumblr.com. A bigger
|
||||||
@@ -177,24 +177,26 @@ module Sources::Strategies
|
|||||||
end
|
end
|
||||||
memoize :inline_images
|
memoize :inline_images
|
||||||
|
|
||||||
def client
|
|
||||||
return {} unless self.class.enabled?
|
|
||||||
|
|
||||||
TumblrApiClient.new(Danbooru.config.tumblr_consumer_key)
|
|
||||||
end
|
|
||||||
memoize :client
|
|
||||||
|
|
||||||
def api_response
|
def api_response
|
||||||
|
return {} unless self.class.enabled?
|
||||||
blog_name, post_id = self.class.parse_info_from_url(page_url)
|
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
|
end
|
||||||
memoize :api_response
|
memoize :api_response
|
||||||
|
|
||||||
def post
|
def post
|
||||||
api_response[:posts].first
|
api_response.dig(:response, :posts)&.first || {}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -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
|
|
||||||
@@ -155,7 +155,7 @@ module Sources
|
|||||||
|
|
||||||
should "get the image urls" do
|
should "get the image urls" do
|
||||||
urls = %w[
|
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
|
https://media.tumblr.com/afed9f5b3c33c39dc8c967e262955de2/tumblr_inline_os31dclyCR1v11u29_1280.png
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -178,5 +178,13 @@ module Sources
|
|||||||
assert_match("test answer", @site.artist_commentary_desc)
|
assert_match("test answer", @site.artist_commentary_desc)
|
||||||
end
|
end
|
||||||
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
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user