`normalize_for_source` was used to convert image URLs to page URLs when displaying sources on the post show page. Move all the code for converting image URLs to page URLs from `Sources::Strategies#normalize_for_source` to `Source::URL#page_url`. Before we had to be very careful in source strategies not to make any network calls in `normalize_for_source`, since it was used in the view for the post show page. Now all the code for generating page URLs is isolated in Source::URL, which makes source strategies simpler. It also makes it easier to check if a source is an image URL or page URL, and if the image URL is convertible to a page URL, which will make autotagging bad_link or bad_source feasible. Finally, this fixes it to generate better page URLs in a handful of cases: * https://www.artstation.com/artwork/qPVGP instead of https://anubis1982918.artstation.com/projects/qPVGP * https://yande.re/post/show?md5=b4b1d11facd1700544554e4805d47bb6s instead of https://yande.re/post?tags=md5:b4b1d11facd1700544554e4805d47bb6 * http://gallery.minitokyo.net/view/365677 instead of http://gallery.minitokyo.net/download/365677 * https://valkyriecrusade.fandom.com/wiki/File:Crimson_Hatsune_H.png instead of https://valkyriecrusade.wikia.com/wiki/File:Crimson_Hatsune_H.png * https://rule34.paheal.net/post/view/852405 instead of https://rule34.paheal.net/post/list/md5:854806addcd3b1246424e7cea49afe31/1
109 lines
4.3 KiB
Ruby
109 lines
4.3 KiB
Ruby
require 'test_helper'
|
|
|
|
module Sources
|
|
class NewgroundsTest < ActiveSupport::TestCase
|
|
context "The source for a newgrounds picture" do
|
|
setup do
|
|
@url = "https://www.newgrounds.com/art/view/hcnone/sephiroth"
|
|
@image_url = "https://art.ngfiles.com/images/1539000/1539538_hcnone_sephiroth.png?f1607668234"
|
|
@image_1 = Sources::Strategies.find(@url)
|
|
@image_2 = Sources::Strategies.find(@image_url)
|
|
end
|
|
|
|
should "get the artist name" do
|
|
assert_equal("hcnone", @image_1.artist_name)
|
|
assert_equal("hcnone", @image_2.artist_name)
|
|
end
|
|
|
|
should "get the artist commentary title" do
|
|
assert_equal("Sephiroth", @image_1.artist_commentary_title)
|
|
assert_equal("Sephiroth", @image_2.artist_commentary_title)
|
|
end
|
|
|
|
should "get profile url" do
|
|
assert_equal("https://hcnone.newgrounds.com", @image_1.profile_url)
|
|
assert_equal("https://hcnone.newgrounds.com", @image_2.profile_url)
|
|
end
|
|
|
|
should "get the image urls" do
|
|
assert_equal([@image_url], @image_1.image_urls)
|
|
assert_equal([@image_url], @image_2.image_urls)
|
|
end
|
|
|
|
should "get the canonical url" do
|
|
assert_equal(@url, @image_1.canonical_url)
|
|
assert_equal(@image_url, @image_2.canonical_url)
|
|
end
|
|
|
|
should "download an image" do
|
|
assert_downloaded(4224, @image_1.image_urls.sole)
|
|
assert_downloaded(4224, @image_2.image_urls.sole)
|
|
end
|
|
|
|
should "get the tags" do
|
|
tags = [
|
|
%w[sephiroth https://www.newgrounds.com/search/conduct/art?match=tags&tags=sephiroth],
|
|
%w[supersmashbros https://www.newgrounds.com/search/conduct/art?match=tags&tags=supersmashbros],
|
|
]
|
|
|
|
assert_equal(tags, @image_1.tags)
|
|
assert_equal(tags, @image_2.tags)
|
|
end
|
|
|
|
should "find the right artist" do
|
|
artist_1 = create(:artist, name: "hcnone1", url_string: "https://hcnone.newgrounds.com/art")
|
|
artist_2 = create(:artist, name: "hcnone2", url_string: "https://www.newgrounds.com/art/view/hcnone/sephiroth")
|
|
artist_3 = create(:artist, name: "bad_artist", url_string: "https://www.newgrounds.com/art")
|
|
|
|
assert_equal([artist_1, artist_2], @image_1.artists)
|
|
assert_equal([artist_1, artist_2], @image_2.artists)
|
|
|
|
assert_not_equal([artist_3], @image_1.artists)
|
|
end
|
|
end
|
|
|
|
context "A multi-image Newgrounds post" do
|
|
should "get all the images" do
|
|
source = Sources::Strategies.find("https://www.newgrounds.com/art/view/natthelich/weaver")
|
|
image_urls = [
|
|
"https://art.ngfiles.com/images/1520000/1520217_natthelich_weaver.jpg?f1606365031",
|
|
"https://art.ngfiles.com/comments/199000/iu_199826_7115981.jpg",
|
|
]
|
|
|
|
assert_equal(image_urls, source.image_urls)
|
|
end
|
|
end
|
|
|
|
context "A deleted or not existing picture" do
|
|
setup do
|
|
@fake_1 = Sources::Strategies.find("https://www.newgrounds.com/art/view/ThisUser/DoesNotExist")
|
|
@artist_1 = create(:artist, name: "thisuser", url_string: "https://thisuser.newgrounds.com")
|
|
|
|
@fake_2 = Sources::Strategies.find("https://www.newgrounds.com/art/view/natthelich/nopicture")
|
|
@artist_2 = create(:artist, name: "natthelich", url_string: "https://natthelich.newgrounds.com")
|
|
|
|
@fake_3 = Sources::Strategies.find("https://www.newgrounds.com/art/view/theolebrave/sensitive-pochaco")
|
|
@artist_3 = create(:artist, name: "taffytoad", url_string: "https://taffytoad.newgrounds.com")
|
|
end
|
|
|
|
should "still find the artist name" do
|
|
assert_equal("thisuser", @fake_1.artist_name)
|
|
assert_equal([@artist_1], @fake_1.artists)
|
|
assert_equal("https://thisuser.newgrounds.com", @fake_1.profile_url)
|
|
|
|
assert_equal("natthelich", @fake_2.artist_name)
|
|
assert_equal([@artist_2], @fake_2.artists)
|
|
|
|
assert_equal([@artist_3], @fake_3.artists)
|
|
end
|
|
end
|
|
|
|
context "generating page urls" do
|
|
should "work" do
|
|
assert_equal("https://www.newgrounds.com/art/view/natthelich/fire-emblem-marth-plus-progress-pic", Source::URL.page_url("https://art.ngfiles.com/images/1033000/1033622_natthelich_fire-emblem-marth-plus-progress-pic.png?f1569487181"))
|
|
assert_nil(Source::URL.page_url("https://art.ngfiles.com/comments/57000/iu_57615_7115981.jpg"))
|
|
end
|
|
end
|
|
end
|
|
end
|