From bfbc9320256e7ecf8ecbbef388497264196b1004 Mon Sep 17 00:00:00 2001 From: evazion Date: Wed, 30 Mar 2022 18:19:08 -0500 Subject: [PATCH] Fix #5082: NoMethodError when searching an old-style dead fanbox url in artist urls. This API call: # profile: https://www.pixiv.net/fanbox/creator/40684196 curl -H "Origin: https://fanbox.cc" "https://api.fanbox.cc/creator.get?userId=40684196" returns `{ "body": nil }` when the artist is deleted. We didn't expect `body` to be nil. Also fix it so that `profile_url` returns the `https://www.pixiv.net/fanbox/creator/40684196` URL if we can't get the `https://.fanbox.cc` URL, usually because the API call failed because the artist is deleted. --- app/logical/source/extractor/fanbox.rb | 19 +++++++++++-------- test/unit/sources/fanbox_test.rb | 9 +++++++++ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/app/logical/source/extractor/fanbox.rb b/app/logical/source/extractor/fanbox.rb index 362647798..9cafcfc38 100644 --- a/app/logical/source/extractor/fanbox.rb +++ b/app/logical/source/extractor/fanbox.rb @@ -35,17 +35,19 @@ module Source end def profile_url - return if artist_name.blank? - - "https://#{artist_name}.fanbox.cc" + if artist_name.present? + "https://#{artist_name}.fanbox.cc" + elsif artist_id_from_url.present? + "https://www.pixiv.net/fanbox/creator/#{artist_id_from_url}" + end end def artist_name - artist_name_from_url || api_response["creatorId"] || artist_api_response["creatorId"] + artist_name_from_url || api_response["creatorId"] || artist_api_response.dig("body", "creatorId") end def display_name - api_response.dig("user", "name") || artist_api_response.dig("user", "name") + api_response.dig("user", "name") || artist_api_response.dig("body", "user", "name") end def other_names @@ -110,10 +112,11 @@ module Source def artist_api_response # Needed to fetch artist from cover pages return {} if artist_id_from_url.blank? + resp = client.get("https://api.fanbox.cc/creator.get?userId=#{artist_id_from_url}") - JSON.parse(resp)["body"] - rescue JSON::ParserError - {} + return {} if resp.status != 200 + + resp.parse end def client diff --git a/test/unit/sources/fanbox_test.rb b/test/unit/sources/fanbox_test.rb index 057223704..50800cc15 100644 --- a/test/unit/sources/fanbox_test.rb +++ b/test/unit/sources/fanbox_test.rb @@ -135,6 +135,15 @@ module Sources end end + context "A deleted /fanbox/creator/:id profile url" do + should "not raise an exception" do + source = Source::Extractor.find("https://www.pixiv.net/fanbox/creator/40684196") + + assert_equal("https://www.pixiv.net/fanbox/creator/40684196", source.profile_url) + assert_nothing_raised { source.to_h } + end + end + context "generating page urls" do should "convert cover images to the profile url" do cover = "https://pixiv.pximg.net/c/400x400_90_a2_g5/fanbox/public/images/creator/1566167/profile/Ix6bnJmTaOAFZhXHLbWyIY1e.jpeg"