From e37844303d12388f304d8d32760deaa596b5be41 Mon Sep 17 00:00:00 2001 From: evazion Date: Sun, 2 Sep 2018 22:53:24 -0500 Subject: [PATCH] deviantart: take artist name from url when unavailable from API. In some cases we can't get the artist name from the API, either because we can't do the API call because the url doesn't contain a deviation id, or because the work is deleted: * http://fc08.deviantart.net/files/f/2007/120/c/9/cool_like_me_by_47ness.jpg (work: http://fav.me/dwcohb) * https://pre00.deviantart.net/423b/th/pre/i/2017/281/e/0/mindflayer_girl01_by_nickbeja-dbpxdt8.png (work: http://fav.me/dbpxd58) Switch to taking the artist name from the url (when present) to deal with these cases. Fixes the artist finder and the artist url normalizer to work in this situation. --- app/logical/sources/strategies/deviant_art.rb | 37 +++++++++++++------ test/unit/sources/deviantart_test.rb | 13 +++++++ 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/app/logical/sources/strategies/deviant_art.rb b/app/logical/sources/strategies/deviant_art.rb index 8324a94e1..f593950b9 100644 --- a/app/logical/sources/strategies/deviant_art.rb +++ b/app/logical/sources/strategies/deviant_art.rb @@ -86,19 +86,20 @@ module Sources end def profile_url - if url =~ PATH_PROFILE - return url - end - - if artist_name.blank? - return nil - end - - return "https://www.deviantart.com/#{artist_name}" + return "" if artist_name.blank? + "https://www.deviantart.com/#{artist_name.downcase}" end + # Prefer the name from the url because the api metadata won't be present when + # the input url doesn't contain a deviation id, or the deviation is private or deleted. def artist_name - api_metadata.dig(:author, :username).try(&:downcase) + if artist_name_from_url.present? + artist_name_from_url + elsif api_metadata.present? + api_metadata.dig(:author, :username) + else + "" + end end def artist_commentary_title @@ -114,7 +115,7 @@ module Sources end def normalizable_for_artist_finder? - url =~ PATH_ART || url =~ SUBDOMAIN_ART + normalize_for_artist_finder.present? end def normalize_for_artist_finder @@ -173,10 +174,24 @@ module Sources end end + def self.artist_name_from_url(url) + if url =~ ASSET || url =~ PATH_ART || url =~ PATH_PROFILE + $~[:artist] + elsif url !~ RESERVED_SUBDOMAINS && (url =~ SUBDOMAIN_ART || url =~ SUBDOMAIN_PROFILE) + $~[:artist] + else + nil + end + end + def deviation_id self.class.deviation_id_from_url(url) || self.class.deviation_id_from_url(referer_url) end + def artist_name_from_url + self.class.artist_name_from_url(url) || self.class.artist_name_from_url(referer_url) + end + def page return nil if deviation_id.blank? deviation_url = "https://www.deviantart.com/deviation/#{deviation_id}" diff --git a/test/unit/sources/deviantart_test.rb b/test/unit/sources/deviantart_test.rb index 68fd9fb55..4bf3960c1 100644 --- a/test/unit/sources/deviantart_test.rb +++ b/test/unit/sources/deviantart_test.rb @@ -15,14 +15,21 @@ module Sources should "work" do assert_equal(["http://origin-orig.deviantart.net/d533/f/2014/004/8/d/holiday_elincia_by_aeror404-d70rm0s.jpg"], @site.image_urls) assert_equal(@site.image_url, @site.canonical_url) + assert_equal("aeror404", @site.artist_name) + assert_equal("https://www.deviantart.com/aeror404", @site.profile_url) end end context "The source for a deleted DeviantArt image URL" do should "work" do @site = Sources::Strategies.find("https://pre00.deviantart.net/423b/th/pre/i/2017/281/e/0/mindflayer_girl01_by_nickbeja-dbpxdt8.png") + @artist = FactoryBot.create(:artist, name: "nickbeja", url_string: "https://nickbeja.deviantart.com") + assert_equal("https://pre00.deviantart.net/423b/th/pre/i/2017/281/e/0/mindflayer_girl01_by_nickbeja-dbpxdt8.png", @site.image_url) assert_equal(@site.image_url, @site.canonical_url) + assert_equal("nickbeja", @site.artist_name) + assert_equal("https://www.deviantart.com/nickbeja", @site.profile_url) + assert_equal([@artist], @site.artists) assert_nothing_raised { @site.to_h } end end @@ -79,6 +86,7 @@ module Sources setup do @url = "http://fc08.deviantart.net/files/f/2007/120/c/9/cool_like_me_by_47ness.jpg" @ref = "https://47ness.deviantart.com/art/Cool-Like-Me-54339311" + @artist = FactoryBot.create(:artist, name: "47ness", url_string: "https://www.deviantart.com/47ness") end context "without a referer" do @@ -90,6 +98,7 @@ module Sources assert_equal("https://www.deviantart.com/47ness", @site.profile_url) assert_equal("", @site.page_url) assert_equal(@site.image_url, @site.canonical_url) + assert_equal([@artist], @site.artists) assert_nothing_raised { @site.to_h } end end @@ -103,6 +112,7 @@ module Sources assert_equal("https://www.deviantart.com/47ness", @site.profile_url) assert_equal("https://www.deviantart.com/47ness/art/Cool-Like-Me-54339311", @site.page_url) assert_equal(@site.page_url, @site.canonical_url) + assert_equal([@artist], @site.artists) assert_nothing_raised { @site.to_h } end end @@ -112,6 +122,7 @@ module Sources setup do @url = "http://pre06.deviantart.net/8497/th/pre/f/2009/173/c/c/cc9686111dcffffffb5fcfaf0cf069fb.jpg" @ref = "https://www.deviantart.com/edsfox/art/Silverhawks-Quicksilver-126872896" + @artist = FactoryBot.create(:artist, name: "edsfox", url_string: "https://edsfox.deviantart.com") end context "without a referer" do @@ -123,6 +134,7 @@ module Sources assert_equal("", @site.profile_url) assert_equal("", @site.page_url) assert_equal(@site.image_url, @site.canonical_url) + assert_equal([], @site.artists) assert_nothing_raised { @site.to_h } end end @@ -136,6 +148,7 @@ module Sources assert_equal("https://www.deviantart.com/edsfox", @site.profile_url) assert_equal("https://www.deviantart.com/edsfox/art/Silverhawks-Quicksilver-126872896", @site.page_url) assert_equal(@site.page_url, @site.canonical_url) + assert_equal([@artist], @site.artists) assert_nothing_raised { @site.to_h } end end