From 57a92ad336a786c5dd5feba9d01488f9957d0755 Mon Sep 17 00:00:00 2001 From: evazion Date: Fri, 22 Apr 2022 01:27:17 -0500 Subject: [PATCH] Fix #5072: Fandom source normalization is wrong --- app/logical/source/url.rb | 1 + app/logical/source/url/fandom.rb | 50 ++++++++++++++++++++++++++++++++ app/logical/source/url/null.rb | 8 ----- test/unit/sources/fandom_test.rb | 15 ++++++++++ test/unit/sources/null_test.rb | 5 ---- 5 files changed, 66 insertions(+), 13 deletions(-) create mode 100644 app/logical/source/url/fandom.rb create mode 100644 test/unit/sources/fandom_test.rb diff --git a/app/logical/source/url.rb b/app/logical/source/url.rb index ccac2582f..f8adb9cf9 100644 --- a/app/logical/source/url.rb +++ b/app/logical/source/url.rb @@ -28,6 +28,7 @@ module Source Source::URL::Booth, Source::URL::DeviantArt, Source::URL::Fanbox, + Source::URL::Fandom, Source::URL::Fantia, Source::URL::Fc2, Source::URL::Foundation, diff --git a/app/logical/source/url/fandom.rb b/app/logical/source/url/fandom.rb new file mode 100644 index 000000000..8ece8936a --- /dev/null +++ b/app/logical/source/url/fandom.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +class Source::URL::Fandom < Source::URL + WIKI_DB_NAMES = { + "age-of-ishtaria": "ishtaria", + "atelierseries": "atelier", + "b-dapedia": "bdaman", + "dragalialost_gamepedia_en": "dragalialost", + "dungeon-ni-deai-o-motomeru": "danmachi", + "gensin-impact": "genshin-impact", + "guilty-gear": "guiltygear", + "honkaiimpact3_gamepedia_en": "honkaiimpact3", + "kagura": "senrankagura", + "langrisser_gamepedia_en": "langrisser", + "magic-school-lussid": "sid-story", + "masterofeternity_gamepedia_en": "masterofeternity", + "rockman_x_dive": "rockman-x-dive", + "strikewitches": "worldwitches", + "sword-art-online": "swordartonline", + "talesofseries-the-tales-of": "tales-of", + "to-aru-majutsu-no-index": "toarumajutsunoindex", + }.with_indifferent_access + + attr_reader :wiki_db_name, :file, :page_url, :full_image_url + + def self.match?(url) + url.domain.in?(%w[nocookie.net fandom.com]) + end + + def parse + case [subdomain, domain, *path_segments] + + # https://vignette.wikia.nocookie.net/queensblade/images/3/33/WGAIRI1.jpg/ + # https://vignette1.wikia.nocookie.net/valkyriecrusade/images/b/bf/Joan_Of_Arc_H.png/revision/latest?cb=20170801081004 + # https://static.wikia.nocookie.net/valkyriecrusade/images/3/3f/Joan_Of_Arc.png/revision/latest/scale-to-width-down/270?cb=20170801081000 + in _, "nocookie.net", wiki_db_name, "images", /^\h$/ => subdir1, /^\h\h$/ => subdir2, file, *rest + @wiki_db_name = wiki_db_name + @file = file + @full_image_url = "https://static.wikia.nocookie.net/#{wiki_db_name}/images/#{subdir1}/#{subdir2}/#{file}" + @page_url = "https://#{wiki}.fandom.com/wiki/Gallery?file=#{file}" + + else + nil + end + end + + def wiki + WIKI_DB_NAMES.fetch(wiki_db_name, wiki_db_name) + end +end diff --git a/app/logical/source/url/null.rb b/app/logical/source/url/null.rb index 322d57554..4af80a5af 100644 --- a/app/logical/source/url/null.rb +++ b/app/logical/source/url/null.rb @@ -227,14 +227,6 @@ class Source::URL::Null < Source::URL @work_id = work_id @page_url = "http://p.twipple.jp/#{work_id}" - # https://vignette.wikia.nocookie.net/queensblade/images/3/33/WGAIRI1.jpg/ - # https://vignette1.wikia.nocookie.net/valkyriecrusade/images/b/bf/Joan_Of_Arc_H.png/revision/latest?cb=20170801081004 - # https://static.wikia.nocookie.net/valkyriecrusade/images/3/3f/Joan_Of_Arc.png/revision/latest/scale-to-width-down/270?cb=20170801081000 - in _, "nocookie.net", wiki, "images", /^\h$/, /^\h\h$/, file, *rest - @wiki = wiki - @file = file - @page_url = "https://#{wiki}.fandom.com/wiki/Gallery?file=#{file}" - # https://static.zerochan.net/Fullmetal.Alchemist.full.2831797.png # https://s1.zerochan.net/Cocoa.Cookie.600.2957938.jpg # http://static.zerochan.net/full/24/13/90674.jpg diff --git a/test/unit/sources/fandom_test.rb b/test/unit/sources/fandom_test.rb new file mode 100644 index 000000000..0b7233eb7 --- /dev/null +++ b/test/unit/sources/fandom_test.rb @@ -0,0 +1,15 @@ +require 'test_helper' + +module Sources + class FandomTest < ActiveSupport::TestCase + context "Fandom" do + should "convert fandom image urls to page urls" do + assert_equal("https://valkyriecrusade.fandom.com/wiki/Gallery?file=Crimson_Hatsune_H.png", Source::URL.page_url("https://vignette.wikia.nocookie.net/valkyriecrusade/images/c/c5/Crimson_Hatsune_H.png/revision/latest?cb=20180702031954")) + assert_equal("https://ishtaria.fandom.com/wiki/Gallery?file=Union-List.png", Source::URL.page_url("https://static.wikia.nocookie.net/age-of-ishtaria/images/f/f9/Union-List.png/revision/latest/scale-to-width-down/670?cb=20141219153314")) + assert_equal("https://atelier.fandom.com/wiki/Gallery?file=Marie_(Front_Page_Art).jpg", Source::URL.page_url("https://static.wikia.nocookie.net/atelierseries/images/2/22/Marie_%28Front_Page_Art%29.jpg/revision/latest/scale-to-width-down/670?cb=20130129113100")) + assert_equal("https://senrankagura.fandom.com/wiki/Gallery?file=Kagurapedia_Header.png", Source::URL.page_url("https://static.wikia.nocookie.net/kagura/images/9/9f/Kagurapedia_Header.png/revision/latest/scale-to-width-down/650?cb=20171016220119")) + assert_equal("https://genshin-impact.fandom.com/wiki/Gallery?file=Character_Kamisato_Ayato_Card.png", Source::URL.page_url("https://static.wikia.nocookie.net/gensin-impact/images/2/22/Character_Kamisato_Ayato_Card.png/revision/latest/scale-to-width-down/281?cb=20220204094446")) + end + end + end +end diff --git a/test/unit/sources/null_test.rb b/test/unit/sources/null_test.rb index 0c0f06bf2..b663b9486 100644 --- a/test/unit/sources/null_test.rb +++ b/test/unit/sources/null_test.rb @@ -82,11 +82,6 @@ module Sources assert_equal("https://gelbooru.com/index.php?page=post&s=list&tags=md5:0b3ae5e225072b8e391c827cb470d29c", Source::URL.page_url(source3)) end - should "normalize wikia links" do - source = "https://vignette.wikia.nocookie.net/valkyriecrusade/images/c/c5/Crimson_Hatsune_H.png/revision/latest?cb=20180702031954" - assert_equal("https://valkyriecrusade.fandom.com/wiki/Gallery?file=Crimson_Hatsune_H.png", Source::URL.page_url(source)) - end - should "normalize e-shuushuu links" do source = "http://e-shuushuu.net/images/2014-07-22-662472.png" assert_equal("https://e-shuushuu.net/image/662472", Source::URL.page_url(source))