gelbooru: fix exception when fetching data for deleted post.

Fix exceptions when fetching deleted or nonexistent posts from Gelbooru. The Gelbooru API doesn't
return any data for deleted posts.
This commit is contained in:
evazion
2022-11-08 15:17:51 -06:00
parent 00db63e885
commit 03a4f1a46e
2 changed files with 32 additions and 4 deletions

View File

@@ -28,7 +28,7 @@ module Source
end
def gelbooru_tags
return [] if api_response.nil?
return [] if api_response.blank?
tags = api_response[:tags].split + ["rating:#{api_response[:rating]}"]
tags.map do |tag|
@@ -40,6 +40,10 @@ module Source
sub_extractor&.tags.to_a
end
def other_names
sub_extractor&.other_names.to_a
end
def post_id
parsed_url.post_id || parsed_referer&.post_id || post_id_from_md5
end
@@ -50,12 +54,12 @@ module Source
end
memoize def api_response
return nil unless api_url.present?
return {} unless api_url.present?
response = http.cache(1.minute).get(api_url)
return nil unless response.status == 200
return {} unless response.status == 200
response.parse["post"].first.with_indifferent_access
response.parse["post"]&.first.to_h.with_indifferent_access
end
memoize def post_id_from_md5

View File

@@ -68,6 +68,30 @@ module Sources
)
end
context "A deleted Gelbooru post" do
strategy_should_work(
"https://gelbooru.com/index.php?page=post&s=list&md5=9d06e876937d46eeda7a5e0ca52f63a8",
image_urls: [],
artist_name: nil,
profile_url: nil,
tags: %w[],
artist_commentary_title: nil,
artist_commentary_desc: nil,
)
end
context "A nonexistent Gelbooru post" do
strategy_should_work(
"https://gelbooru.com/index.php?page=post&s=list&md5=ffffffffffffffffffffffffffffffff",
image_urls: [],
artist_name: nil,
profile_url: nil,
tags: %w[],
artist_commentary_title: nil,
artist_commentary_desc: nil,
)
end
should "normalize gelbooru links" do
source1 = "https://gelbooru.com//images/ee/5c/ee5c9a69db9602c95debdb9b98fb3e3e.jpeg"
source2 = "http://simg.gelbooru.com//images/2003/edd1d2b3881cf70c3acf540780507531.png"