diff --git a/app/logical/mastodon_api_client.rb b/app/logical/mastodon_api_client.rb index ed26a1da7..d8401b8fd 100644 --- a/app/logical/mastodon_api_client.rb +++ b/app/logical/mastodon_api_client.rb @@ -15,7 +15,7 @@ class MastodonApiClient def json return {} if id.blank? || access_token.blank? JSON.parse(access_token.get("/api/v1/statuses/#{id}").body) - rescue + rescue JSON::ParserError {} end diff --git a/app/logical/source/extractor/art_station.rb b/app/logical/source/extractor/art_station.rb index d77611252..d19fcc2fd 100644 --- a/app/logical/source/extractor/art_station.rb +++ b/app/logical/source/extractor/art_station.rb @@ -48,7 +48,7 @@ class Source::Extractor def tags api_response[:tags].to_a.map do |tag| - [tag, "https://www.artstation.com/search?q=" + CGI.escape(tag)] + [tag, "https://www.artstation.com/search?q=#{CGI.escape(tag)}"] end end @@ -94,7 +94,7 @@ class Source::Extractor image_sizes = %w[original 4k large medium small] urls = image_sizes.map { |size| parsed_url.full_image_url(size) } - chosen_url = urls.find { |url| http_exists?(url) } + chosen_url = urls.find { |u| http_exists?(u) } chosen_url || url end end diff --git a/app/logical/source/extractor/mastodon.rb b/app/logical/source/extractor/mastodon.rb index ef8fd0695..a65ee8cff 100644 --- a/app/logical/source/extractor/mastodon.rb +++ b/app/logical/source/extractor/mastodon.rb @@ -52,7 +52,7 @@ class Source::Extractor end def artist_name - api_response.account_name + api_response.account_name || artist_name_from_url end def artist_name_from_url @@ -60,7 +60,7 @@ class Source::Extractor end def other_names - [api_response.display_name] + [api_response.display_name].compact end def account_id diff --git a/test/unit/sources/art_station_test.rb b/test/unit/sources/art_station_test.rb index 57bbd0a87..6b4a0cb35 100644 --- a/test/unit/sources/art_station_test.rb +++ b/test/unit/sources/art_station_test.rb @@ -2,35 +2,17 @@ require 'test_helper' module Sources class ArtStationTest < ActiveSupport::TestCase - context "The source site for an art station artwork page" do - setup do - @site = Source::Extractor.find("https://www.artstation.com/artwork/04XA4") - end - - should "get the image url" do - assert_equal(["https://cdn.artstation.com/p/assets/images/images/000/705/368/4k/jey-rain-one1.jpg?1443931773"], @site.image_urls) - end - - should "get the page url" do - assert_equal("https://jeyrain.artstation.com/projects/04XA4", @site.page_url) - end - - should "get the profile" do - assert_equal("https://www.artstation.com/jeyrain", @site.profile_url) - end - - should "get the artist name" do - assert_equal("jeyrain", @site.artist_name) - end - - should "get the tags" do - assert_equal([], @site.tags) - end - - should "get the artist commentary" do - assert_equal("pink", @site.artist_commentary_title) - assert_equal("", @site.dtext_artist_commentary_desc) - end + context "An ArtStation /artwork/:id URL" do + strategy_should_work( + "https://www.artstation.com/artwork/04XA4", + image_urls: ["https://cdn.artstation.com/p/assets/images/images/000/705/368/4k/jey-rain-one1.jpg?1443931773"], + page_url: "https://jeyrain.artstation.com/projects/04XA4", + profile_url: "https://www.artstation.com/jeyrain", + artist_name: "jeyrain", + tags: [], + artist_commentary_title: "pink", + dtext_artist_commentary_desc: "" + ) end context "An ArtStation /projects/ URL" do @@ -43,94 +25,56 @@ module Sources tags: %w[gantz Reika], artist_commentary_title: "Reika ", dtext_artist_commentary_desc: "From Gantz.", - download_size: 210_899, + download_size: 210_899 ) end - context "The source site for a www.artstation.com/artwork/$slug page" do - setup do - @site = Source::Extractor.find("https://www.artstation.com/artwork/cody-from-sf") - end - - should "get the image url" do - url = "https://cdn.artstation.com/p/assets/images/images/000/144/922/4k/cassio-yoshiyaki-cody2backup2-yoshiyaki.jpg?1406314198" - assert_equal([url], @site.image_urls) - end - - should "get the tags" do - assert_equal(["Street Fighter", "Cody", "SF"].sort, @site.tags.map(&:first).sort) - assert_equal(["street_fighter", "cody", "sf"].sort, @site.normalized_tags.sort) - end + context "An ArtStation /artwork/$slug page" do + strategy_should_work( + "https://www.artstation.com/artwork/cody-from-sf", + image_urls: ["https://cdn.artstation.com/p/assets/images/images/000/144/922/4k/cassio-yoshiyaki-cody2backup2-yoshiyaki.jpg?1406314198"], + tags: ["Street Fighter", "Cody", "SF"] + ) end - context "The source site for a http://cdn.artstation.com/p/assets/... url" do - setup do - @url = "https://cdna.artstation.com/p/assets/images/images/006/029/978/large/amama-l-z.jpg" - @ref = "https://www.artstation.com/artwork/4BWW2" - end - - context "with a referer" do - should "work" do - site = Source::Extractor.find(@url, @ref) - - assert_equal(["https://cdn.artstation.com/p/assets/images/images/006/029/978/4k/amama-l-z.jpg"], site.image_urls) - assert_equal("https://amama.artstation.com/projects/4BWW2", site.page_url) - assert_equal("https://www.artstation.com/amama", site.profile_url) - assert_equal("amama", site.artist_name) - assert_nothing_raised { site.to_h } - end - end - - context "without a referer" do - should "work" do - site = Source::Extractor.find(@url) - - assert_equal(["https://cdn.artstation.com/p/assets/images/images/006/029/978/4k/amama-l-z.jpg"], site.image_urls) - assert_nil(site.page_url) - assert_nil(site.profile_url) - assert_nil(site.artist_name) - assert_equal([], site.tags) - assert_nothing_raised { site.to_h } - end - end + context "A http://cdn.artstation.com/p/assets/... url" do + strategy_should_work( + "https://cdna.artstation.com/p/assets/images/images/006/029/978/large/amama-l-z.jpg", + image_urls: ["https://cdn.artstation.com/p/assets/images/images/006/029/978/4k/amama-l-z.jpg"], + page_url: nil, + profile_url: nil + ) end - context "A 4k asset url" do - context "without a referer" do - should "work" do - site = Source::Extractor.find("https://cdna.artstation.com/p/assets/images/images/007/253/680/4k/ina-wong-demon-girl-done-ttd-comp.jpg?1504793833") - - assert_equal(["https://cdn.artstation.com/p/assets/images/images/007/253/680/4k/ina-wong-demon-girl-done-ttd-comp.jpg?1504793833"], site.image_urls) - assert_nothing_raised { site.to_h } - end - end + context "A http://cdn.artstation.com/p/assets/... url with referrer" do + strategy_should_work( + "https://cdna.artstation.com/p/assets/images/images/006/029/978/large/amama-l-z.jpg", + image_urls: ["https://cdn.artstation.com/p/assets/images/images/006/029/978/4k/amama-l-z.jpg"], + referer: "https://www.artstation.com/artwork/4BWW2", + page_url: "https://amama.artstation.com/projects/4BWW2", + profile_url: "https://www.artstation.com/amama", + artist_name: "amama" + ) end - context "A cover url" do - should "work" do - url = "https://cdna.artstation.com/p/assets/covers/images/007/262/828/large/monica-kyrie-1.jpg?1504865060" - site = Source::Extractor.find(url) - - assert_equal(["https://cdn.artstation.com/p/assets/covers/images/007/262/828/original/monica-kyrie-1.jpg?1504865060"], site.image_urls) - end + context "An ArtStation cover url" do + strategy_should_work( + "https://cdna.artstation.com/p/assets/covers/images/007/262/828/large/monica-kyrie-1.jpg?1504865060", + image_urls: ["https://cdn.artstation.com/p/assets/covers/images/007/262/828/original/monica-kyrie-1.jpg?1504865060"] + ) end - context "The source site for an ArtStation gallery" do - setup do - @site = Source::Extractor.find("https://www.artstation.com/artwork/BDxrA") - end - - should "get only image urls, not video urls" do - urls = %w[https://cdn.artstation.com/p/assets/images/images/006/037/253/4k/astri-lohne-sjursen-eva.jpg?1495573664] - assert_equal(urls, @site.image_urls) - end + context "An ArtStation post with images and youtube links" do + strategy_should_work( + "https://www.artstation.com/artwork/BDxrA", + image_urls: ["https://cdn.artstation.com/p/assets/images/images/006/037/253/4k/astri-lohne-sjursen-eva.jpg?1495573664"] + ) end - context "A work that includes video clips" do - should_eventually "include the video clips in the image urls" do - @source = Source::Extractor.find("https://www.artstation.com/artwork/0nP1e8") - - assert_equal(%w[ + context "An ArtStation post with images and videos" do + strategy_should_work( # XXX Broken by Cloudflare captcha + "https://www.artstation.com/artwork/0nP1e8", + image_urls: %w[ https://cdn.artstation.com/p/assets/images/images/040/979/418/original/yusuf-umar-workout-10mb.gif?1630425406 https://cdn.artstation.com/p/assets/images/images/040/979/435/4k/yusuf-umar-1.jpg?1630425420 https://cdn.artstation.com/p/assets/images/images/040/979/470/4k/yusuf-umar-2.jpg?1630425483 @@ -140,34 +84,33 @@ module Sources https://cdn.artstation.com/p/assets/images/images/040/980/932/4k/yusuf-umar-tpose.jpg?1630427748 https://cdn-animation.artstation.com/p/video_sources/000/466/622/workout.mp4 https://cdn-animation.artstation.com/p/video_sources/000/466/623/workout-clay.mp4 - ], @source.image_urls) - end - - should "work for the video itself" do - @source = Source::Extractor.find("https://cdn-animation.artstation.com/p/video_sources/000/466/622/workout.mp4") - - assert_equal(["https://cdn-animation.artstation.com/p/video_sources/000/466/622/workout.mp4"], @source.image_urls) - end + ] + ) end - context "A work that has been deleted" do - should "work" do - url = "https://fiship.artstation.com/projects/x8n8XT" - site = Source::Extractor.find(url) + context "An ArtStation video url" do + strategy_should_work( + "https://cdn-animation.artstation.com/p/video_sources/000/466/622/workout.mp4", + image_urls: ["https://cdn-animation.artstation.com/p/video_sources/000/466/622/workout.mp4"] + ) + end - assert_equal("fiship", site.artist_name) - assert_equal("https://www.artstation.com/fiship", site.profile_url) - assert_equal(url, site.page_url) - assert_equal([], site.image_urls) - assert_nothing_raised { site.to_h } - end + context "A deleted ArtStation url" do + strategy_should_work( + "https://fiship.artstation.com/projects/x8n8XT", + deleted: true, + image_urls: [], + artist_name: "fiship", + profile_url: "https://www.artstation.com/fiship", + page_url: "https://fiship.artstation.com/projects/x8n8XT" + ) end context "A /small/ ArtStation image URL" do strategy_should_work( "https://cdnb3.artstation.com/p/assets/images/images/003/716/071/small/aoi-ogata-hate-city.jpg?1476754974", image_urls: ["https://cdn.artstation.com/p/assets/images/images/003/716/071/4k/aoi-ogata-hate-city.jpg?1476754974"], - download_size: 1_816_628, + download_size: 1_816_628 ) end @@ -175,7 +118,7 @@ module Sources strategy_should_work( "https://cdnb.artstation.com/p/assets/images/images/003/716/071/large/aoi-ogata-hate-city.jpg?1476754974", image_urls: ["https://cdn.artstation.com/p/assets/images/images/003/716/071/4k/aoi-ogata-hate-city.jpg?1476754974"], - download_size: 1_816_628, + download_size: 1_816_628 ) end @@ -183,18 +126,22 @@ module Sources strategy_should_work( "https://cdna.artstation.com/p/assets/images/images/004/730/278/large/mendel-oh-dragonll.jpg", image_urls: ["https://cdn.artstation.com/p/assets/images/images/004/730/278/4k/mendel-oh-dragonll.jpg"], - download_size: 452_985, + download_size: 452_985 ) end - should "work for artists with underscores in their name" do - site = Source::Extractor.find("https://hosi_na.artstation.com/projects/3oEk3B") - assert_equal("hosi_na", site.artist_name) + context "An ArtStation url with underscores in the artist name" do + strategy_should_work( + "https://hosi_na.artstation.com/projects/3oEk3B", + artist_name: "hosi_na" + ) end - should "work for artists with dashes in their name" do - site = Source::Extractor.find("https://sa-dui.artstation.com/projects/DVERn") - assert_equal("sa-dui", site.artist_name) + context "An ArtStation url with dashes in the artist name" do + strategy_should_work( + "https://sa-dui.artstation.com/projects/DVERn", + artist_name: "sa-dui" + ) end should "Parse ArtStation URLs correctly" do @@ -212,9 +159,9 @@ module Sources assert(Source::URL.profile_url?("https://artstation.com/artist/sa-dui")) assert(Source::URL.profile_url?("https://anubis1982918.artstation.com")) - refute(Source::URL.profile_url?("https://anubis1982918.artstation.com/projects/qPVGP")) - refute(Source::URL.profile_url?("https://www.artstation.com")) - refute(Source::URL.profile_url?("https://artstation.com")) + assert_not(Source::URL.profile_url?("https://anubis1982918.artstation.com/projects/qPVGP")) + assert_not(Source::URL.profile_url?("https://www.artstation.com")) + assert_not(Source::URL.profile_url?("https://artstation.com")) end end end diff --git a/test/unit/sources/fanbox_test.rb b/test/unit/sources/fanbox_test.rb index d796c1774..294bd3fa9 100644 --- a/test/unit/sources/fanbox_test.rb +++ b/test/unit/sources/fanbox_test.rb @@ -3,145 +3,91 @@ require 'test_helper' module Sources class FanboxTest < ActiveSupport::TestCase context "A free Pixiv Fanbox post" do - setup do - @post1 = Source::Extractor.find("https://yanmi0308.fanbox.cc/posts/1141325") - @post2 = Source::Extractor.find("https://chanxco.fanbox.cc/posts/209386") - @post3 = Source::Extractor.find("https://downloads.fanbox.cc/images/post/209386/w/1200/8dRNHXkFqAwSt31W2Bg8fSdL.jpeg") - - assert_nothing_raised { @post1.to_h } - assert_nothing_raised { @post2.to_h } - assert_nothing_raised { @post3.to_h } - end - - should "get the image urls" do - # "images" in api response - images1 = %w[ + strategy_should_work( + "https://yanmi0308.fanbox.cc/posts/1141325", + image_urls: %w[ https://downloads.fanbox.cc/images/post/1141325/q7GaJ0A9J5Uz8kvEAUizHJoN.png https://downloads.fanbox.cc/images/post/1141325/LMJz0sAig5h9D3rPZGCEGniZ.png https://downloads.fanbox.cc/images/post/1141325/dRSz380Uf3N8s4pT2ADEXBco.png https://downloads.fanbox.cc/images/post/1141325/h48L2mbm39qqNUB1abLAvzvg.png - ] - assert_equal(images1, @post1.image_urls) + ], + artist_commentary_title: "栗山やんみ(デザイン)", + artist_commentary_desc: "˗ˋˏ Special Thanks ˎˊ˗ (敬称略)\n\n🎨キャラクターデザイン\n特急みかん https://twitter.com/tokkyuumikan\n\n🤖3Dモデリング\n(仮) https://twitter.com/Admiral_TMP\n\n⚙プログラミング\n神無月ユズカ https://twitter.com/Kannaduki_Yzk\n\n🎧OP・EDミュージック\n卓球少年 https://twitter.com/takkyuu_s\n\n📻BGM\nC https://twitter.com/nica2c\n\n🖌ロゴデザイン\nてづかもり https://twitter.com/tezkamori\n\n🎨SDキャラクター\nAZU。 https://twitter.com/tokitou_aaa", + page_url: "https://yanmi0308.fanbox.cc/posts/1141325", + profile_url: "https://yanmi0308.fanbox.cc", + download_size: 431_225, + tags: [ + ["栗山やんみ", "https://fanbox.cc/tags/栗山やんみ"], ["VTuber", "https://fanbox.cc/tags/VTuber"], ["三面図", "https://fanbox.cc/tags/三面図"], + ["イラスト", "https://fanbox.cc/tags/イラスト"], ["ロゴデザイン", "https://fanbox.cc/tags/ロゴデザイン"], ["モデリング", "https://fanbox.cc/tags/モデリング"], + ], + artist_name: "yanmi0308", + display_name: "栗山やんみ" + ) + end - # "imageMapi" in api response (embedded pics) - images2 = %w[ + context "A free Pixiv Fanbox post with embedded pics" do + strategy_should_work( + "https://chanxco.fanbox.cc/posts/209386", + image_urls: %w[ https://downloads.fanbox.cc/images/post/209386/Q8rZ0iMHpcmJDACEzNGjTj9E.jpeg https://downloads.fanbox.cc/images/post/209386/8dRNHXkFqAwSt31W2Bg8fSdL.jpeg https://downloads.fanbox.cc/images/post/209386/AGGWF0JxytFcNL2ybPKBaqp7.jpeg - ] - assert_equal(images2, @post2.image_urls) - end - - should "get the commentary" do - # Normal commentary - assert_equal("栗山やんみ(デザイン)", @post1.artist_commentary_title) - - body1 = "˗ˋˏ Special Thanks ˎˊ˗ (敬称略)\n\n🎨キャラクターデザイン\n特急みかん https://twitter.com/tokkyuumikan\n\n🤖3Dモデリング\n(仮) https://twitter.com/Admiral_TMP\n\n⚙プログラミング\n神無月ユズカ https://twitter.com/Kannaduki_Yzk\n\n🎧OP・EDミュージック\n卓球少年 https://twitter.com/takkyuu_s\n\n📻BGM\nC https://twitter.com/nica2c\n\n🖌ロゴデザイン\nてづかもり https://twitter.com/tezkamori\n\n🎨SDキャラクター\nAZU。 https://twitter.com/tokitou_aaa" - assert_equal(body1, @post1.artist_commentary_desc) - - # With embedded pics - assert_equal("水着BBちゃん+アラフィフ+ライダーさん", @post2.artist_commentary_title) - assert_equal("水着BBちゃん+アラフィフ+ライダーさん", @post3.artist_commentary_title) - - body2 = "今週のらくがきまとめ\n\nhttps://downloads.fanbox.cc/images/post/209386/Q8rZ0iMHpcmJDACEzNGjTj9E.jpeg\n水着BBちゃん\n第一再臨もなかなかセクシー\nhttps://downloads.fanbox.cc/images/post/209386/8dRNHXkFqAwSt31W2Bg8fSdL.jpeg\nアラフィフ\n男キャラも描いていこうと練習中\n新宿での軽いキャラも好き\nhttps://downloads.fanbox.cc/images/post/209386/AGGWF0JxytFcNL2ybPKBaqp7.jpeg\nライダーさん\nつい眼鏡も描いてしまう\n\n#FGO\n" - assert_equal(body2, @post2.artist_commentary_desc) - assert_equal(body2, @post3.artist_commentary_desc) - end - - should "get the right page url" do - assert_equal("https://yanmi0308.fanbox.cc/posts/1141325", @post1.page_url) - assert_equal("https://chanxco.fanbox.cc/posts/209386", @post2.page_url) - assert_equal("https://chanxco.fanbox.cc/posts/209386", @post3.page_url) - end - - should "correctly download the right image" do - assert_downloaded(431_225, @post1.image_urls[0]) - assert_downloaded(753_048, @post1.image_urls[1]) - assert_downloaded(589_327, @post1.image_urls[2]) - assert_downloaded(178_739, @post1.image_urls[3]) - - assert_downloaded(245_678, @post2.image_urls[0]) - assert_downloaded(320_056, @post2.image_urls[1]) - assert_downloaded(666_681, @post2.image_urls[2]) - - assert_downloaded(320_056, @post3.image_urls.sole) - end - - should "get the tags" do - tags = [ - ["栗山やんみ", "https://fanbox.cc/tags/栗山やんみ"], ["VTuber", "https://fanbox.cc/tags/VTuber"], ["三面図", "https://fanbox.cc/tags/三面図"], - ["イラスト", "https://fanbox.cc/tags/イラスト"], ["ロゴデザイン", "https://fanbox.cc/tags/ロゴデザイン"], ["モデリング", "https://fanbox.cc/tags/モデリング"] - ] - assert_equal(tags, @post1.tags) - end - - should "find the correct artist" do - @artist1 = FactoryBot.create(:artist, name: "yanmi", url_string: @post1.url) - @artist2 = FactoryBot.create(:artist, name: "chanxco", url_string: @post2.url) - assert_equal([@artist1], @post1.artists) - assert_equal([@artist2], @post2.artists) - assert_equal([@artist2], @post3.artists) - end - - should "find the right artist names" do - assert_equal("yanmi0308", @post1.artist_name) - assert_equal("栗山やんみ", @post1.display_name) - assert_equal("chanxco", @post2.artist_name) - assert_equal("CHANxCO", @post2.display_name) - assert_equal(@post2.artist_name, @post3.artist_name) - assert_equal(@post2.display_name, @post3.display_name) - end + ], + artist_commentary_title: "水着BBちゃん+アラフィフ+ライダーさん", + artist_commentary_desc: "今週のらくがきまとめ\n\nhttps://downloads.fanbox.cc/images/post/209386/Q8rZ0iMHpcmJDACEzNGjTj9E.jpeg\n水着BBちゃん\n第一再臨もなかなかセクシー\nhttps://downloads.fanbox.cc/images/post/209386/8dRNHXkFqAwSt31W2Bg8fSdL.jpeg\nアラフィフ\n男キャラも描いていこうと練習中\n新宿での軽いキャラも好き\nhttps://downloads.fanbox.cc/images/post/209386/AGGWF0JxytFcNL2ybPKBaqp7.jpeg\nライダーさん\nつい眼鏡も描いてしまう\n\n#FGO\n", + page_url: "https://chanxco.fanbox.cc/posts/209386", + profile_url: "https://chanxco.fanbox.cc", + download_size: 245_678, + artist_name: "chanxco", + display_name: "CHANxCO" + ) end - context "an age-restricted fanbox post" do - should "work" do - @source = Source::Extractor.find("https://mfr.fanbox.cc/posts/1306390") - - assert_nothing_raised { @source.to_h } - assert_equal("mfr", @source.artist_name) - assert_equal(["https://downloads.fanbox.cc/images/post/1306390/VOXblkyvltL5fRhMoR7RdSkk.png"], @source.image_urls) - end + context "A Pixiv Fanbox sample" do + strategy_should_work( + "https://downloads.fanbox.cc/images/post/209386/w/1200/8dRNHXkFqAwSt31W2Bg8fSdL.jpeg", + image_urls: ["https://downloads.fanbox.cc/images/post/209386/8dRNHXkFqAwSt31W2Bg8fSdL.jpeg"], + artist_commentary_title: "水着BBちゃん+アラフィフ+ライダーさん", + artist_commentary_desc: "今週のらくがきまとめ\n\nhttps://downloads.fanbox.cc/images/post/209386/Q8rZ0iMHpcmJDACEzNGjTj9E.jpeg\n水着BBちゃん\n第一再臨もなかなかセクシー\nhttps://downloads.fanbox.cc/images/post/209386/8dRNHXkFqAwSt31W2Bg8fSdL.jpeg\nアラフィフ\n男キャラも描いていこうと練習中\n新宿での軽いキャラも好き\nhttps://downloads.fanbox.cc/images/post/209386/AGGWF0JxytFcNL2ybPKBaqp7.jpeg\nライダーさん\nつい眼鏡も描いてしまう\n\n#FGO\n", + page_url: "https://chanxco.fanbox.cc/posts/209386", + profile_url: "https://chanxco.fanbox.cc", + download_size: 320_056, + artist_name: "chanxco", + display_name: "CHANxCO" + ) end - context "A link in the old format" do - should "still work" do - post = Source::Extractor.find("https://www.pixiv.net/fanbox/creator/1566167/post/39714") - assert_nothing_raised { post.to_h } - assert_equal("https://omu001.fanbox.cc", post.profile_url) - assert_equal("https://omu001.fanbox.cc/posts/39714", post.page_url) - artist = FactoryBot.create(:artist, name: "omu", url_string: "https://omu001.fanbox.cc") - assert_equal([artist], post.artists) - end + context "An age-restricted Fanbox post" do + strategy_should_work( + "https://mfr.fanbox.cc/posts/1306390", + image_urls: ["https://downloads.fanbox.cc/images/post/1306390/VOXblkyvltL5fRhMoR7RdSkk.png"], + artist_name: "mfr", + artist_commentary_desc: "これからセックスしまーす♪と言ってるシーン(・ω・`)\nhttps://downloads.fanbox.cc/images/post/1306390/VOXblkyvltL5fRhMoR7RdSkk.png\n※海苔強化して再アップしました( 'A`;)\n", + profile_url: "https://mfr.fanbox.cc" + ) end context "A cover image" do - should "still work" do - post = Source::Extractor.find("https://pixiv.pximg.net/c/1620x580_90_a2_g5/fanbox/public/images/creator/1566167/cover/QqxYtuWdy4XWQx1ZLIqr4wvA.jpeg") - assert_nothing_raised { post.to_h } - assert_downloaded(750_484, post.image_urls.sole) - assert_equal("https://omu001.fanbox.cc", post.profile_url) - assert_equal(post.profile_url, post.page_url) - artist = FactoryBot.create(:artist, name: "omu", url_string: "https://omu001.fanbox.cc") - assert_equal([artist], post.artists) - end + strategy_should_work( + "https://pixiv.pximg.net/c/1620x580_90_a2_g5/fanbox/public/images/creator/1566167/cover/QqxYtuWdy4XWQx1ZLIqr4wvA.jpeg", + download_size: 750_484, + profile_url: "https://omu001.fanbox.cc" + ) end - context "A dead profile picture from the old domain" do - should "still find the artist" do - post = Source::Extractor.find("https://pixiv.pximg.net/c/400x400_90_a2_g5/fanbox/public/images/creator/1566167/profile/Ix6bnJmTaOAFZhXHLbWyIY1e.jpeg") - assert_equal("https://omu001.fanbox.cc", post.profile_url) - artist = FactoryBot.create(:artist, name: "omu", url_string: "https://omu001.fanbox.cc") - assert_equal([artist], post.artists) - end + context "A post in the old pixiv format" do + strategy_should_work( + "https://www.pixiv.net/fanbox/creator/1566167/post/39714", + page_url: "https://omu001.fanbox.cc/posts/39714", + profile_url: "https://omu001.fanbox.cc" + ) 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 + context "A dead profile picture in the old pixiv format" do + strategy_should_work( + "https://pixiv.pximg.net/c/400x400_90_a2_g5/fanbox/public/images/creator/29999491/profile/Ew6fOhLGPvmUcwU6FyH8JAMX.jpeg", + profile_url: "https://deaver0211.fanbox.cc" + ) end should "Parse Fanbox URLs correctly" do @@ -158,8 +104,8 @@ module Sources assert(Source::URL.profile_url?("https://www.pixiv.net/fanbox/creator/1566167")) assert(Source::URL.profile_url?("https://www.pixiv.net/fanbox/member.php?user_id=3410642")) assert(Source::URL.profile_url?("https://omu001.fanbox.cc")) - refute(Source::URL.profile_url?("https://www.fanbox.cc")) - refute(Source::URL.profile_url?("https://fanbox.cc")) + assert_not(Source::URL.profile_url?("https://www.fanbox.cc")) + assert_not(Source::URL.profile_url?("https://fanbox.cc")) end end end diff --git a/test/unit/sources/hentai_foundry_test.rb b/test/unit/sources/hentai_foundry_test.rb index 858c9ff9b..6eff94243 100644 --- a/test/unit/sources/hentai_foundry_test.rb +++ b/test/unit/sources/hentai_foundry_test.rb @@ -2,99 +2,60 @@ require 'test_helper' module Sources class HentaiFoundryTest < ActiveSupport::TestCase - context "The source for a hentai foundry picture" do - setup do - @image_1 = Source::Extractor.find("https://www.hentai-foundry.com/pictures/user/Afrobull/795025/kuroeda") - @image_2 = Source::Extractor.find("https://pictures.hentai-foundry.com/a/Afrobull/795025/Afrobull-795025-kuroeda.png") - end - - should "get the illustration id" do - assert_equal("795025", @image_1.illust_id) - assert_equal("795025", @image_2.illust_id) - end - - should "get the artist name" do - assert_equal("Afrobull", @image_1.artist_name) - assert_equal("Afrobull", @image_2.artist_name) - end - - should "get the artist commentary title" do - assert_equal("kuroeda", @image_1.artist_commentary_title) - assert_equal("kuroeda", @image_2.artist_commentary_title) - end - - should "get profile url" do - assert_equal("https://www.hentai-foundry.com/user/Afrobull", @image_1.profile_url) - assert_equal("https://www.hentai-foundry.com/user/Afrobull", @image_2.profile_url) - end - - should "get the image url" do - assert_equal(["https://pictures.hentai-foundry.com/a/Afrobull/795025/Afrobull-795025-kuroeda.png"], @image_1.image_urls) - assert_equal(["https://pictures.hentai-foundry.com/a/Afrobull/795025/Afrobull-795025-kuroeda.png"], @image_2.image_urls) - end - - should "download an image" do - assert_downloaded(1_349_887, @image_1.image_urls.sole) - assert_downloaded(1_349_887, @image_2.image_urls.sole) - end - - should "get the tags" do - assert_equal([["elf", "https://www.hentai-foundry.com/pictures/tagged/elf"]], @image_1.tags) - assert_equal([["elf", "https://www.hentai-foundry.com/pictures/tagged/elf"]], @image_2.tags) - end - - should "find the correct artist" do - @artist = FactoryBot.create(:artist, name: "Afrobull", url_string: @image_1.url) - assert_equal([@artist], @image_1.artists) - assert_equal([@artist], @image_2.artists) - end + context "A hentai-foundry post" do + strategy_should_work( + "https://www.hentai-foundry.com/pictures/user/Afrobull/795025/kuroeda", + image_urls: ["https://pictures.hentai-foundry.com/a/Afrobull/795025/Afrobull-795025-kuroeda.png"], + artist_name: "Afrobull", + artist_commentary_title: "kuroeda", + profile_url: "https://www.hentai-foundry.com/user/Afrobull", + download_size: 1_349_887, + tags: [["elf", "https://www.hentai-foundry.com/pictures/tagged/elf"]] + ) end - context "An artist profile url" do - setup do - @site = Source::Extractor.find("https://www.hentai-foundry.com/user/Afrobull/profile") - end - - should "get the profile url" do - assert_equal("https://www.hentai-foundry.com/user/Afrobull", @site.profile_url) - end - - should "get the artist name" do - assert_equal("Afrobull", @site.artist_name) - end + context "A hentai-foundry picture" do + strategy_should_work( + "https://www.hentai-foundry.com/pictures/user/Afrobull/795025/kuroeda", + image_urls: ["https://pictures.hentai-foundry.com/a/Afrobull/795025/Afrobull-795025-kuroeda.png"], + artist_name: "Afrobull", + artist_commentary_title: "kuroeda", + profile_url: "https://www.hentai-foundry.com/user/Afrobull", + download_size: 1_349_887, + tags: [["elf", "https://www.hentai-foundry.com/pictures/tagged/elf"]] + ) end context "A deleted picture" do - setup do - @image = Source::Extractor.find("https://www.hentai-foundry.com/pictures/user/faustsketcher/279498") - @artist = FactoryBot.create(:artist, name: "faustsketcher", url_string: @image.url) - end - - should "still find the artist name" do - assert_equal("faustsketcher", @image.artist_name) - assert_equal("https://www.hentai-foundry.com/user/faustsketcher", @image.profile_url) - assert_equal([@artist], @image.artists) - end + strategy_should_work( + "https://www.hentai-foundry.com/pictures/user/faustsketcher/279498", + image_urls: [], + artist_name: "faustsketcher", + profile_url: "https://www.hentai-foundry.com/user/faustsketcher", + deleted: true + ) end - context "generating page urls" do - should "work" do - source1 = "http://pictures.hentai-foundry.com//a/AnimeFlux/219123.jpg" - source2 = "http://pictures.hentai-foundry.com/a/AnimeFlux/219123/Mobile-Suit-Equestria-rainbow-run.jpg" - source3 = "http://www.hentai-foundry.com/pictures/user/Ganassa/457176/LOL-Swimsuit---Caitlyn-reworked-nude-ver." - - assert_equal("https://www.hentai-foundry.com/pictures/user/AnimeFlux/219123", Source::URL.page_url(source1)) - assert_equal("https://www.hentai-foundry.com/pictures/user/AnimeFlux/219123", Source::URL.page_url(source2)) - assert_equal("https://www.hentai-foundry.com/pictures/user/Ganassa/457176", Source::URL.page_url(source3)) - assert_nil(Source::URL.page_url("https://pictures.hentai-foundry.com/a/AnimeFlux")) - end + context "An old image url" do + strategy_should_work( + "http://pictures.hentai-foundry.com//a/AnimeFlux/219123.jpg", + image_urls: ["https://pictures.hentai-foundry.com/a/AnimeFlux/219123/AnimeFlux-219123-Mobile_Suit_Equestria_rainbow_run.jpg"], + page_url: "https://www.hentai-foundry.com/pictures/user/AnimeFlux/219123", + profile_url: "https://www.hentai-foundry.com/user/AnimeFlux" + ) end - context "a post with a deeply nested commentary" do - should "work" do - @source = Source::Extractor.find("https://hentai-foundry.com/pictures/user/LumiNyu/867562/Mona-patreon-winner") - assert_nothing_raised { @source.to_h } - end + context "An image url without the extension" do + strategy_should_work( + "http://www.hentai-foundry.com/pictures/user/Ganassa/457176/LOL-Swimsuit---Caitlyn-reworked-nude-ver.", + image_urls: ["https://pictures.hentai-foundry.com/g/Ganassa/457176/Ganassa-457176-LOL_Swimsuit_-_Caitlyn_reworked_nude_ver..jpg"], + page_url: "https://www.hentai-foundry.com/pictures/user/Ganassa/457176", + profile_url: "https://www.hentai-foundry.com/user/Ganassa" + ) + end + + context "A post with deeply nested commentary" do + strategy_should_work("https://hentai-foundry.com/pictures/user/LumiNyu/867562/Mona-patreon-winner") end should "Parse HentaiFoundry URLs correctly" do diff --git a/test/unit/sources/mastodon_test.rb b/test/unit/sources/mastodon_test.rb index 9dfa6b53e..b573107ec 100644 --- a/test/unit/sources/mastodon_test.rb +++ b/test/unit/sources/mastodon_test.rb @@ -2,75 +2,23 @@ require 'test_helper' module Sources class MastodonTest < ActiveSupport::TestCase - context "The source site for a https://pawoo.net/web/status/$id url" do + context "For Pawoo," do setup do skip "Pawoo keys not set" unless Danbooru.config.pawoo_client_id - @site = Source::Extractor.find("https://pawoo.net/web/statuses/1202176") end - should "get the profile" do - assert_equal("https://pawoo.net/@9ed00e924818", @site.profile_url) + context "a https://pawoo.net/web/status/$id url" do + strategy_should_work( + "https://pawoo.net/web/statuses/1202176", + image_urls: ["https://img.pawoo.net/media_attachments/files/000/128/953/original/4c0a06087b03343f.png"], + profile_url: "https://pawoo.net/@9ed00e924818", + artist_name: "9ed00e924818", + dtext_artist_commentary_desc: "a mind forever voyaging through strange seas of thought alone" + ) end - should "get the artist name" do - assert_equal("9ed00e924818", @site.artist_name) - end - - should "get the image url" do - assert_equal(["https://img.pawoo.net/media_attachments/files/000/128/953/original/4c0a06087b03343f.png"], @site.image_urls) - end - - should "get the commentary" do - desc = '
a mind forever voyaging through strange seas of thought alone https://pawoo.net/media/9hJzXvwxVl1CezW0ecM
' - assert_equal(desc, @site.artist_commentary_desc) - end - - should "get the dtext-ified commentary" do - desc = 'a mind forever voyaging through strange seas of thought alone' - assert_equal(desc, @site.dtext_artist_commentary_desc) - end - end - - context "The source site for a https://pawoo.net/$user/$id url" do - setup do - skip "Pawoo keys not set" unless Danbooru.config.pawoo_client_id - @site = Source::Extractor.find("https://pawoo.net/@evazion/19451018") - end - - should "get the profile" do - profiles = %w[https://pawoo.net/@evazion https://pawoo.net/web/accounts/47806] - assert_equal(profiles.first, @site.profile_url) - assert_equal(profiles, @site.profile_urls) - end - - should "get the artist name" do - assert_equal("evazion", @site.artist_name) - end - - should "get the image urls" do - urls = %w[ - https://img.pawoo.net/media_attachments/files/001/297/997/original/c4272a09570757c2.png - https://img.pawoo.net/media_attachments/files/001/298/028/original/55a6fd252778454b.mp4 - https://img.pawoo.net/media_attachments/files/001/298/081/original/2588ee9ba808f38f.webm - https://img.pawoo.net/media_attachments/files/001/298/084/original/media.mp4 - ] - - assert_equal(urls, @site.image_urls) - end - - should "get the tags" do - assert_equal(%w[foo bar baz], @site.tags.map(&:first)) - end - - should "get the commentary" do - desc = "test post please ignore
blah blah blah
this is a test 🍕
" - - assert_nil(@site.artist_commentary_title) - assert_equal(desc, @site.artist_commentary_desc) - end - - should "get the dtext-ified commentary" do - desc = <<-DESC.strip_heredoc.chomp + context "a https://pawoo.net/$user/$id url" do + desc = <<~DESC.chomp test post please ignore blah blah blah @@ -80,45 +28,72 @@ module Sources "#foo":[https://pawoo.net/tags/foo] "#bar":[https://pawoo.net/tags/bar] "#baz":[https://pawoo.net/tags/baz] DESC - assert_equal(desc, @site.dtext_artist_commentary_desc) + strategy_should_work( + "https://pawoo.net/@evazion/19451018", + image_urls: %w[ + https://img.pawoo.net/media_attachments/files/001/297/997/original/c4272a09570757c2.png + https://img.pawoo.net/media_attachments/files/001/298/028/original/55a6fd252778454b.mp4 + https://img.pawoo.net/media_attachments/files/001/298/081/original/2588ee9ba808f38f.webm + https://img.pawoo.net/media_attachments/files/001/298/084/original/media.mp4 + ], + profile_urls: %w[https://pawoo.net/@evazion https://pawoo.net/web/accounts/47806], + artist_name: "evazion", + tags: %w[foo bar baz], + dtext_artist_commentary_desc: desc + ) + end + + context "a https://img.pawoo.net/ url" do + strategy_should_work( + "https://img.pawoo.net/media_attachments/files/001/298/028/original/55a6fd252778454b.mp4", + image_urls: ["https://img.pawoo.net/media_attachments/files/001/298/028/original/55a6fd252778454b.mp4"], + download_size: 59_950, + referer: "https://pawoo.net/@evazion/19451018", + page_url: "https://pawoo.net/@evazion/19451018" + ) + end + + context "a deleted or invalid source" do + strategy_should_work( + "https://pawoo.net/@nonamethankswashere/12345678901234567890", + profile_url: "https://pawoo.net/@nonamethankswashere", + artist_name: "nonamethankswashere", + deleted: true + ) end end - context "The source site for a https://img.pawoo.net/ url" do - setup do - skip "Pawoo keys not set" unless Danbooru.config.pawoo_client_id - @url = "https://img.pawoo.net/media_attachments/files/001/298/028/original/55a6fd252778454b.mp4" - @ref = "https://pawoo.net/@evazion/19451018" - @site = Source::Extractor.find(@url, @ref) - end - - should "fetch the source data" do - assert_equal("evazion", @site.artist_name) - end - - should "correctly get the page url" do - assert_equal(@ref, @site.page_url) - end - end - - context "A baraag url" do + context "For Baraag," do setup do skip "Baraag keys not set" unless Danbooru.config.baraag_client_id - @url = "https://baraag.net/@bardbot/105732813175612920" - @site1 = Source::Extractor.find(@url) - - @img = "https://baraag.net/system/media_attachments/files/105/803/948/862/719/091/original/54e1cb7ca33ec449.png" - @ref = "https://baraag.net/@Nakamura/105803949565505009" - @site2 = Source::Extractor.find(@img, @ref) end - should "work" do - assert_equal("https://baraag.net/@bardbot", @site1.profile_url) - assert_equal(["https://baraag.net/system/media_attachments/files/105/732/803/241/495/700/original/556e1eb7f5ca610f.png"], @site1.image_urls) - assert_equal("bardbot", @site1.artist_name) - assert_equal("🍌", @site1.dtext_artist_commentary_desc) + context "a baraag.net/$user/$id url" do + strategy_should_work( + "https://baraag.net/@bardbot/105732813175612920", + image_urls: ["https://baraag.net/system/media_attachments/files/105/732/803/241/495/700/original/556e1eb7f5ca610f.png"], + download_size: 573_353, + profile_url: "https://baraag.net/@bardbot", + artist_name: "bardbot", + dtext_artist_commentary_desc: "🍌" + ) + end - assert_equal([@img], @site2.image_urls) + context "a baraag image url" do + strategy_should_work( + "https://baraag.net/system/media_attachments/files/105/803/948/862/719/091/original/54e1cb7ca33ec449.png", + image_urls: ["https://baraag.net/system/media_attachments/files/105/803/948/862/719/091/original/54e1cb7ca33ec449.png"], + download_size: 363_261 + ) + end + + context "a deleted or invalid source" do + strategy_should_work( + "https://baraag.net/@nonamethankswashere/12345678901234567890", + profile_url: "https://baraag.net/@nonamethankswashere", + artist_name: "nonamethankswashere", + deleted: true + ) end end @@ -136,23 +111,6 @@ module Sources end end - context "A deleted or invalid source" do - setup do - skip "Pawoo keys not set" unless Danbooru.config.pawoo_client_id - - @site1 = Source::Extractor.find("https://pawoo.net/@nantokakun/105643037682139899") # 404 - @site2 = Source::Extractor.find("https://img.pawoo.net/media_attachments/files/001/297/997/original/c4272a09570757c2.png") - - assert_nothing_raised { @site1.to_h } - assert_nothing_raised { @site2.to_h } - end - - should "still find the artist" do - @artist = FactoryBot.create(:artist, name: "nantokakun", url_string: "https://pawoo.net/@nantokakun") - assert_equal([@artist], @site1.artists) - end - end - should "Parse Pawoo URLs correctly" do assert(Source::URL.image_url?("https://img.pawoo.net/media_attachments/files/001/297/997/small/c4272a09570757c2.png")) assert(Source::URL.image_url?("https://pawoo.net/media/lU2uV7C1MMQSb1czwvg")) diff --git a/test/unit/sources/moebooru_test.rb b/test/unit/sources/moebooru_test.rb index e4212ceed..fccfaa67b 100644 --- a/test/unit/sources/moebooru_test.rb +++ b/test/unit/sources/moebooru_test.rb @@ -2,111 +2,105 @@ require "test_helper" module Sources class MoebooruTest < ActiveSupport::TestCase - def assert_source_data_equals(url, referer = nil, site_name: nil, image_url: nil, page_url: nil, size: nil, tags: [], profile_url: nil, **params) - site = Source::Extractor.find(url, referer) - - assert_equal(site_name, site.site_name) - assert_equal([image_url], site.image_urls) - assert_equal(page_url, site.page_url) if page_url.present? - assert_equal(tags.sort, site.tags.map(&:first).sort) - assert_equal(profile_url.to_s, site.profile_url.to_s) - assert_nothing_raised { site.to_h } - end - - context "Yande.re:" do - context "A 'https://yande.re/jpeg/:hash/:file.jpg' jpeg sample url" do - should "download the original file" do - @source = "https://yande.re/jpeg/2c6876ac2317fce617e3c5f1a642123b/yande.re%20292092%20hatsune_miku%20tid%20vocaloid.jpg" - @rewrite = "https://files.yande.re/image/2c6876ac2317fce617e3c5f1a642123b/yande.re%20292092.png" - assert_rewritten(@rewrite, @source) - assert_downloaded(1_050_117, @source) - end + context "For Yande.re," do + context "a post" do + strategy_should_work( + "https://yande.re/post/show/482880", + image_urls: ["https://files.yande.re/image/7ecfdead705d7b956b26b1d37b98d089/yande.re%20482880.jpg"], + download_size: 362_554, + tags: ["bayashiko", "journey_to_the_west", "sun_wukong"], + page_url: "https://yande.re/post/show/482880", + profile_url: "https://twitter.com/apononori" + ) end - context "Fetching data for an active yande.re .jpg post" do - should "work" do - @samp = "https://files.yande.re/sample/7ecfdead705d7b956b26b1d37b98d089/yande.re%20482880%20sample%20bayashiko%20journey_to_the_west%20sun_wukong.jpg" - @full = "https://files.yande.re/image/7ecfdead705d7b956b26b1d37b98d089/yande.re%20482880.jpg" - @page = "https://yande.re/post/show/482880" - @tags = ["bayashiko", "journey_to_the_west", "sun_wukong"] - @size = 362_554 - @profile_url = "https://twitter.com/apononori" - @data = { site_name: "Yande.re", image_url: @full, page_url: @page, size: @size, tags: @tags, profile_url: @profile_url } - - assert_source_data_equals(@samp, **@data) - assert_source_data_equals(@full, **@data) - assert_source_data_equals(@page, **@data) - end + context "a https://yande.re/sample/:hash/:file.jpg" do + strategy_should_work( + "https://files.yande.re/sample/7ecfdead705d7b956b26b1d37b98d089/yande.re%20482880%20sample%20bayashiko%20journey_to_the_west%20sun_wukong.jpg", + image_urls: ["https://files.yande.re/image/7ecfdead705d7b956b26b1d37b98d089/yande.re%20482880.jpg"], + download_size: 362_554, + tags: ["bayashiko", "journey_to_the_west", "sun_wukong"], + page_url: "https://yande.re/post/show/482880", + profile_url: "https://twitter.com/apononori" + ) end - context "Fetching data for a deleted yande.re .png post with the post id" do - should "work" do - @samp = "https://files.yande.re/sample/fb27a7ea6c48b2ef76fe915e378b9098/yande.re%20398018%20detexted%20misaki_kurehito%20saenai_heroine_no_sodatekata%20sawamura_spencer_eriri%20thighhighs.jpg" - @jpeg = "https://files.yande.re/sample/fb27a7ea6c48b2ef76fe915e378b9098/yande.re%20398018%20detexted%20misaki_kurehito%20saenai_heroine_no_sodatekata%20sawamura_spencer_eriri%20thighhighs.jpg" - @full = "https://files.yande.re/image/fb27a7ea6c48b2ef76fe915e378b9098/yande.re%20398018.png" - @page = "https://yande.re/post/show/398018" - @tags = ["misaki_kurehito", "saenai_heroine_no_sodatekata", "sawamura_spencer_eriri", "detexted", "thighhighs"] - @size = 9_118_998 - @data = { site_name: "Yande.re", image_url: @full, page_url: @page, size: @size, tags: @tags, profile_url: nil } - - assert_source_data_equals(@samp, **@data) - assert_source_data_equals(@jpeg, **@data) - assert_source_data_equals(@full, **@data) - assert_source_data_equals(@page, **@data) - end + context "a 'https://yande.re/jpeg/:hash/:file.jpg' jpeg sample url" do + strategy_should_work( + "https://yande.re/jpeg/2c6876ac2317fce617e3c5f1a642123b/yande.re%20292092%20hatsune_miku%20tid%20vocaloid.jpg", + image_urls: ["https://files.yande.re/image/2c6876ac2317fce617e3c5f1a642123b/yande.re%20292092.png"], + download_size: 1_050_117 + ) end - context "Fetching data for a deleted yande.re .png post without the post id" do - should "work" do - @samp = "https://files.yande.re/sample/fb27a7ea6c48b2ef76fe915e378b9098.jpg" - @jpeg = "https://files.yande.re/jpeg/fb27a7ea6c48b2ef76fe915e378b9098.jpg" - @full = "https://files.yande.re/image/fb27a7ea6c48b2ef76fe915e378b9098.png" - @tags = [] - @size = 9_118_998 - @data = { site_name: "Yande.re", image_url: @full, page_url: @page, size: @size, tags: @tags, profile_url: nil } - - assert_source_data_equals(@samp, **@data) - assert_source_data_equals(@jpeg, **@data) - assert_source_data_equals(@full, **@data) - end + context "a deleted yande.re post with the post id" do + strategy_should_work( + "https://files.yande.re/sample/fb27a7ea6c48b2ef76fe915e378b9098/yande.re%20398018%20detexted%20misaki_kurehito%20saenai_heroine_no_sodatekata%20sawamura_spencer_eriri%20thighhighs.jpg", + image_urls: ["https://files.yande.re/image/fb27a7ea6c48b2ef76fe915e378b9098/yande.re%20398018.png"], + page_url: "https://yande.re/post/show/398018", + tags: ["misaki_kurehito", "saenai_heroine_no_sodatekata", "sawamura_spencer_eriri", "detexted", "thighhighs"], + download_size: 9_118_998 + ) end - context "When the referer URL is SauceNao" do - should "ignore the referer" do - @url = "https://yande.re/post/show/469929" - @ref = "https://saucenao.com/" + context "a deleted yande.re post without the post id" do + strategy_should_work( + "https://files.yande.re/jpeg/fb27a7ea6c48b2ef76fe915e378b9098.jpg", + image_urls: ["https://files.yande.re/image/fb27a7ea6c48b2ef76fe915e378b9098.png"], + download_size: 9_118_998 + ) + end - assert_source_data_equals(@url, @ref, - site_name: "Yande.re", - image_url: "https://files.yande.re/image/36b031b266605d89aed2b62d479e64b1/yande.re%20469929.jpg", - page_url: "https://yande.re/post/show/469929", - tags: %w[anchovy bandages darjeeling girls_und_panzer katyusha kay_(girls_und_panzer) mika_(girls_und_panzer) nishi_kinuyo nishizumi_maho nishizumi_miho shimada_arisu uniform], - ) - end + context "a yande.re post with a saucenao referer" do + strategy_should_work( + "https://yande.re/post/show/469929", + referer: "https://saucenao.com", + image_urls: ["https://files.yande.re/image/36b031b266605d89aed2b62d479e64b1/yande.re%20469929.jpg"], + page_url: "https://yande.re/post/show/469929", + tags: %w[anchovy bandages darjeeling girls_und_panzer katyusha kay_(girls_und_panzer) mika_(girls_und_panzer) nishi_kinuyo nishizumi_maho nishizumi_miho shimada_arisu uniform] + ) end end - context "Konachan.com:" do - context "Fetching data for an active konachan.com .png post" do - should "work" do - @samp = "https://konachan.com/sample/ca12cdb79a66d242e95a6f958341bf05/Konachan.com%20-%20270916%20sample.jpg" - @jpeg = "https://konachan.com/jpeg/ca12cdb79a66d242e95a6f958341bf05/Konachan.com%20-%20270916%20anthropomorphism%20bed%20blonde_hair%20bow%20brown_eyes%20doll%20girls_frontline%20hara_shoutarou%20hoodie%20long_hair%20pantyhose%20scar%20skirt%20twintails.jpg" - @full = "https://konachan.com/image/ca12cdb79a66d242e95a6f958341bf05/Konachan.com%20-%20270916.png" - @page = "https://konachan.com/post/show/270916" - @size = 8_167_593 - @tags = %w[ - anthropomorphism bed blonde_hair bow brown_eyes doll - girls_frontline hara_shoutarou hoodie long_hair pantyhose scar skirt - twintails ump-45_(girls_frontline) ump-9_(girls_frontline) - ] - @profile_url = "https://www.pixiv.net/users/22528152" + context "For konachan.com," do + context "a sample url" do + strategy_should_work( + "https://konachan.com/sample/ca12cdb79a66d242e95a6f958341bf05/Konachan.com%20-%20270916%20sample.jpg", + image_urls: ["https://konachan.com/image/ca12cdb79a66d242e95a6f958341bf05/Konachan.com%20-%20270916.png"], + download_size: 8_167_593, + tags: %w[anthropomorphism bed blonde_hair bow brown_eyes doll girls_frontline hara_shoutarou hoodie long_hair pantyhose scar skirt twintails ump-45_(girls_frontline) ump-9_(girls_frontline)], + profile_url: "https://www.pixiv.net/users/22528152" + ) + end - @data = { site_name: "Konachan", image_url: @full, page_url: @page, size: @size, tags: @tags, profile_url: @profile_url } - assert_source_data_equals(@samp, **@data) - assert_source_data_equals(@jpeg, **@data) - assert_source_data_equals(@full, **@data) - assert_source_data_equals(@page, **@data) - end + context "a jpeg url" do + strategy_should_work( + "https://konachan.com/jpeg/ca12cdb79a66d242e95a6f958341bf05/Konachan.com%20-%20270916%20anthropomorphism%20bed%20blonde_hair%20bow%20brown_eyes%20doll%20girls_frontline%20hara_shoutarou%20hoodie%20long_hair%20pantyhose%20scar%20skirt%20twintails.jpg", + image_urls: ["https://konachan.com/image/ca12cdb79a66d242e95a6f958341bf05/Konachan.com%20-%20270916.png"], + download_size: 8_167_593, + tags: %w[anthropomorphism bed blonde_hair bow brown_eyes doll girls_frontline hara_shoutarou hoodie long_hair pantyhose scar skirt twintails ump-45_(girls_frontline) ump-9_(girls_frontline)], + profile_url: "https://www.pixiv.net/users/22528152" + ) + end + + context "a full-size image url" do + strategy_should_work( + "https://konachan.com/image/ca12cdb79a66d242e95a6f958341bf05/Konachan.com%20-%20270916.png", + image_urls: ["https://konachan.com/image/ca12cdb79a66d242e95a6f958341bf05/Konachan.com%20-%20270916.png"], + download_size: 8_167_593, + tags: %w[anthropomorphism bed blonde_hair bow brown_eyes doll girls_frontline hara_shoutarou hoodie long_hair pantyhose scar skirt twintails ump-45_(girls_frontline) ump-9_(girls_frontline)], + profile_url: "https://www.pixiv.net/users/22528152" + ) + end + + context "a post url" do + strategy_should_work( + "https://konachan.com/post/show/270916", + image_urls: ["https://konachan.com/image/ca12cdb79a66d242e95a6f958341bf05/Konachan.com%20-%20270916.png"], + download_size: 8_167_593, + tags: %w[anthropomorphism bed blonde_hair bow brown_eyes doll girls_frontline hara_shoutarou hoodie long_hair pantyhose scar skirt twintails ump-45_(girls_frontline) ump-9_(girls_frontline)], + profile_url: "https://www.pixiv.net/users/22528152" + ) end end diff --git a/test/unit/sources/newgrounds_test.rb b/test/unit/sources/newgrounds_test.rb index 39eff763d..05535edb0 100644 --- a/test/unit/sources/newgrounds_test.rb +++ b/test/unit/sources/newgrounds_test.rb @@ -2,100 +2,55 @@ 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 = Source::Extractor.find(@url) - @image_2 = Source::Extractor.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 page url" do - assert_equal(@url, @image_1.page_url) - assert_equal(@url, @image_2.page_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 = [ + context "A newgrounds post url" do + strategy_should_work( + "https://www.newgrounds.com/art/view/hcnone/sephiroth", + image_urls: ["https://art.ngfiles.com/images/1539000/1539538_hcnone_sephiroth.png?f1607668234"], + page_url: "https://www.newgrounds.com/art/view/hcnone/sephiroth", + download_size: 4_224, + artist_name: "hcnone", + profile_url: "https://hcnone.newgrounds.com", + artist_commentary_title: "Sephiroth", + 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 = Source::Extractor.find("https://www.newgrounds.com/art/view/natthelich/weaver") - image_urls = [ + context "A newgrounds image url" do + strategy_should_work( + "https://art.ngfiles.com/images/1539000/1539538_hcnone_sephiroth.png?f1607668234", + image_urls: ["https://art.ngfiles.com/images/1539000/1539538_hcnone_sephiroth.png?f1607668234"], + page_url: "https://www.newgrounds.com/art/view/hcnone/sephiroth", + download_size: 4_224, + artist_name: "hcnone", + profile_url: "https://hcnone.newgrounds.com", + artist_commentary_title: "Sephiroth", + 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], + ] + ) + end + + context "A multi-image post" do + strategy_should_work( + "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 = Source::Extractor.find("https://www.newgrounds.com/art/view/ThisUser/DoesNotExist") - @artist_1 = create(:artist, name: "thisuser", url_string: "https://thisuser.newgrounds.com") - - @fake_2 = Source::Extractor.find("https://www.newgrounds.com/art/view/natthelich/nopicture") - @artist_2 = create(:artist, name: "natthelich", url_string: "https://natthelich.newgrounds.com") - - @fake_3 = Source::Extractor.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 + context "A deleted or non-existing post" do + strategy_should_work( + "https://www.newgrounds.com/art/view/natthelich/nopicture", + deleted: true, + profile_url: "https://natthelich.newgrounds.com", + artist_name: "natthelich" + ) end context "A www.newgrounds.com/dump/item URL" do @@ -103,23 +58,15 @@ module Sources "https://www.newgrounds.com/dump/item/a1f417d20f5eaef31e26ac3c4956b3d4", image_urls: [], artist_name: nil, - profile_url: nil, + profile_url: nil ) end - context "A post with links to other illustrations in the commentary" do - should "not include the links in the commentary" do - @source = Source::Extractor.find("https://www.newgrounds.com/art/view/boxofwant/annie-hughes-1") - - assert_equal(<<~EOS.chomp, @source.artist_commentary_desc) -Commission of Annie Hughes, the mom from The Iron Giant, for @ManStawberry.