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://<username>.fanbox.cc` URL, usually because the API call failed
because the artist is deleted.
This commit is contained in:
evazion
2022-03-30 18:19:08 -05:00
parent a272c19b98
commit bfbc932025
2 changed files with 20 additions and 8 deletions

View File

@@ -35,17 +35,19 @@ module Source
end end
def profile_url def profile_url
return if artist_name.blank? if artist_name.present?
"https://#{artist_name}.fanbox.cc"
"https://#{artist_name}.fanbox.cc" elsif artist_id_from_url.present?
"https://www.pixiv.net/fanbox/creator/#{artist_id_from_url}"
end
end end
def artist_name 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 end
def display_name 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 end
def other_names def other_names
@@ -110,10 +112,11 @@ module Source
def artist_api_response def artist_api_response
# Needed to fetch artist from cover pages # Needed to fetch artist from cover pages
return {} if artist_id_from_url.blank? return {} if artist_id_from_url.blank?
resp = client.get("https://api.fanbox.cc/creator.get?userId=#{artist_id_from_url}") resp = client.get("https://api.fanbox.cc/creator.get?userId=#{artist_id_from_url}")
JSON.parse(resp)["body"] return {} if resp.status != 200
rescue JSON::ParserError
{} resp.parse
end end
def client def client

View File

@@ -135,6 +135,15 @@ module Sources
end end
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 context "generating page urls" do
should "convert cover images to the profile url" 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" cover = "https://pixiv.pximg.net/c/400x400_90_a2_g5/fanbox/public/images/creator/1566167/profile/Ix6bnJmTaOAFZhXHLbWyIY1e.jpeg"