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.
This commit is contained in:
evazion
2018-09-02 22:53:24 -05:00
parent 8f87fb90d9
commit e37844303d
2 changed files with 39 additions and 11 deletions

View File

@@ -86,19 +86,20 @@ module Sources
end end
def profile_url def profile_url
if url =~ PATH_PROFILE return "" if artist_name.blank?
return url "https://www.deviantart.com/#{artist_name.downcase}"
end
if artist_name.blank?
return nil
end
return "https://www.deviantart.com/#{artist_name}"
end 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 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 end
def artist_commentary_title def artist_commentary_title
@@ -114,7 +115,7 @@ module Sources
end end
def normalizable_for_artist_finder? def normalizable_for_artist_finder?
url =~ PATH_ART || url =~ SUBDOMAIN_ART normalize_for_artist_finder.present?
end end
def normalize_for_artist_finder def normalize_for_artist_finder
@@ -173,10 +174,24 @@ module Sources
end end
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 def deviation_id
self.class.deviation_id_from_url(url) || self.class.deviation_id_from_url(referer_url) self.class.deviation_id_from_url(url) || self.class.deviation_id_from_url(referer_url)
end end
def artist_name_from_url
self.class.artist_name_from_url(url) || self.class.artist_name_from_url(referer_url)
end
def page def page
return nil if deviation_id.blank? return nil if deviation_id.blank?
deviation_url = "https://www.deviantart.com/deviation/#{deviation_id}" deviation_url = "https://www.deviantart.com/deviation/#{deviation_id}"

View File

@@ -15,14 +15,21 @@ module Sources
should "work" do 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(["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(@site.image_url, @site.canonical_url)
assert_equal("aeror404", @site.artist_name)
assert_equal("https://www.deviantart.com/aeror404", @site.profile_url)
end end
end end
context "The source for a deleted DeviantArt image URL" do context "The source for a deleted DeviantArt image URL" do
should "work" 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") @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("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(@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 } assert_nothing_raised { @site.to_h }
end end
end end
@@ -79,6 +86,7 @@ module Sources
setup do setup do
@url = "http://fc08.deviantart.net/files/f/2007/120/c/9/cool_like_me_by_47ness.jpg" @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" @ref = "https://47ness.deviantart.com/art/Cool-Like-Me-54339311"
@artist = FactoryBot.create(:artist, name: "47ness", url_string: "https://www.deviantart.com/47ness")
end end
context "without a referer" do context "without a referer" do
@@ -90,6 +98,7 @@ module Sources
assert_equal("https://www.deviantart.com/47ness", @site.profile_url) assert_equal("https://www.deviantart.com/47ness", @site.profile_url)
assert_equal("", @site.page_url) assert_equal("", @site.page_url)
assert_equal(@site.image_url, @site.canonical_url) assert_equal(@site.image_url, @site.canonical_url)
assert_equal([@artist], @site.artists)
assert_nothing_raised { @site.to_h } assert_nothing_raised { @site.to_h }
end end
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", @site.profile_url)
assert_equal("https://www.deviantart.com/47ness/art/Cool-Like-Me-54339311", @site.page_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(@site.page_url, @site.canonical_url)
assert_equal([@artist], @site.artists)
assert_nothing_raised { @site.to_h } assert_nothing_raised { @site.to_h }
end end
end end
@@ -112,6 +122,7 @@ module Sources
setup do setup do
@url = "http://pre06.deviantart.net/8497/th/pre/f/2009/173/c/c/cc9686111dcffffffb5fcfaf0cf069fb.jpg" @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" @ref = "https://www.deviantart.com/edsfox/art/Silverhawks-Quicksilver-126872896"
@artist = FactoryBot.create(:artist, name: "edsfox", url_string: "https://edsfox.deviantart.com")
end end
context "without a referer" do context "without a referer" do
@@ -123,6 +134,7 @@ module Sources
assert_equal("", @site.profile_url) assert_equal("", @site.profile_url)
assert_equal("", @site.page_url) assert_equal("", @site.page_url)
assert_equal(@site.image_url, @site.canonical_url) assert_equal(@site.image_url, @site.canonical_url)
assert_equal([], @site.artists)
assert_nothing_raised { @site.to_h } assert_nothing_raised { @site.to_h }
end end
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", @site.profile_url)
assert_equal("https://www.deviantart.com/edsfox/art/Silverhawks-Quicksilver-126872896", @site.page_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(@site.page_url, @site.canonical_url)
assert_equal([@artist], @site.artists)
assert_nothing_raised { @site.to_h } assert_nothing_raised { @site.to_h }
end end
end end