fanbox: don't raise error on age-restricted posts.

Prevent age-restricted fanbox posts from raising errors when source data
is fetched. This prevents error messages from being shown to users when
switching to the edit tab on a post.

This will cause uploads of age-restricted posts to fail with an
unrelated error because we either can't find the image url (if we were
given only the html page) or we can't download the image (because we're
not logged in to Fanbox).
This commit is contained in:
evazion
2020-08-18 14:43:07 -05:00
parent 5bad5c6012
commit 1d2a8a7898
3 changed files with 22 additions and 12 deletions

View File

@@ -105,11 +105,7 @@ module Sources
end end
def display_name def display_name
if api_response.present? api_response.dig("user", "name") || artist_api_response.dig("user", "name")
api_response["user"]["name"]
elsif artist_api_response.present?
artist_api_response["user"]["name"]
end
end end
def other_names def other_names
@@ -125,8 +121,9 @@ module Sources
end end
def artist_commentary_desc def artist_commentary_desc
return if api_response.blank?
body = api_response["body"] body = api_response["body"]
return if body.blank?
if body["text"].present? if body["text"].present?
body["text"] body["text"]
elsif body["blocks"].present? elsif body["blocks"].present?
@@ -160,11 +157,12 @@ module Sources
return {} if illust_id.blank? return {} if illust_id.blank?
resp = client.get("https://api.fanbox.cc/post.info?postId=#{illust_id}") resp = client.get("https://api.fanbox.cc/post.info?postId=#{illust_id}")
json_response = JSON.parse(resp)["body"] json_response = JSON.parse(resp)["body"]
if json_response["restrictedFor"] == 2 && json_response["body"].blank?
# Pixiv Fanbox login is protected by Google Recaptcha, so it's not possible for us to extract anything from them (save for the title). # Pixiv Fanbox login is protected by Google Recaptcha, so it's not
# Other projects like PixivUtils ask the user to periodically extract cookies from the browser, but this is not feasible for Danbooru. # possible for us to extract anything from them (save for the title).
raise Sources::Error, "Age-restricted posts from Pixiv Fanbox are not supported." # Other projects like PixivUtils ask the user to periodically extract
end # cookies from the browser, but this is not feasible for Danbooru.
return {} if json_response["restrictedFor"] == 2 && json_response["body"].blank?
json_response json_response
rescue JSON::ParserError rescue JSON::ParserError
@@ -181,7 +179,7 @@ module Sources
end end
def client def client
Danbooru::Http.headers(Origin: "https://fanbox.cc").cache(1.minute) @client ||= http.headers(Origin: "https://fanbox.cc").cache(1.minute)
end end
end end
end end

View File

@@ -371,6 +371,9 @@ class UploadsControllerTest < ActionDispatch::IntegrationTest
should_upload_successfully("https://art.ngfiles.com/images/1254000/1254722_natthelich_pandora.jpg") should_upload_successfully("https://art.ngfiles.com/images/1254000/1254722_natthelich_pandora.jpg")
should_upload_successfully("https://art.ngfiles.com/comments/57000/iu_57615_7115981.jpg") should_upload_successfully("https://art.ngfiles.com/comments/57000/iu_57615_7115981.jpg")
should_upload_successfully("https://www.newgrounds.com/art/view/puddbytes/costanza-at-bat") should_upload_successfully("https://www.newgrounds.com/art/view/puddbytes/costanza-at-bat")
should_upload_successfully("https://www.fanbox.cc/@tsukiori/posts/1080657")
should_upload_successfully("https://downloads.fanbox.cc/images/post/1080657/SaakPC251KafLL6jIo1WPPmr.png")
end end
end end
end end

View File

@@ -90,6 +90,15 @@ module Sources
end end
end end
context "an age-restricted fanbox post" do
should "not raise an error" do
@source = Sources::Strategies.find("https://mfr.fanbox.cc/posts/1306390")
assert_nothing_raised { @source.to_h }
assert_equal("mfr", @source.artist_name)
end
end
context "A link in the old format" do context "A link in the old format" do
should "still work" do should "still work" do
post = Sources::Strategies.find("https://www.pixiv.net/fanbox/creator/1566167/post/39714") post = Sources::Strategies.find("https://www.pixiv.net/fanbox/creator/1566167/post/39714")