From 03a4f1a46e758eea2dabb96d17eb4e1859faa167 Mon Sep 17 00:00:00 2001 From: evazion Date: Tue, 8 Nov 2022 15:17:51 -0600 Subject: [PATCH] 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. --- app/logical/source/extractor/gelbooru.rb | 12 ++++++++---- test/unit/sources/gelbooru_test.rb | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/app/logical/source/extractor/gelbooru.rb b/app/logical/source/extractor/gelbooru.rb index 51fd5a1f9..e8dd371a2 100644 --- a/app/logical/source/extractor/gelbooru.rb +++ b/app/logical/source/extractor/gelbooru.rb @@ -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 diff --git a/test/unit/sources/gelbooru_test.rb b/test/unit/sources/gelbooru_test.rb index f3adb4a86..d8d043cd8 100644 --- a/test/unit/sources/gelbooru_test.rb +++ b/test/unit/sources/gelbooru_test.rb @@ -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"