From fbb7675051b77c9e7ae10075694de7ae243062cf Mon Sep 17 00:00:00 2001 From: kagoromo Date: Wed, 8 Feb 2023 00:20:31 +0900 Subject: [PATCH] Fix tests --- lib/LANraragi/Plugin/Metadata/Chaika.pm | 27 +++++------ tests/LANraragi/Plugin/Metadata/Chaika.t | 50 +++++++++++++++++++-- tests/samples/chaika/001_gid_27240.json | 11 +++-- tests/samples/chaika/002_sha1_response.json | 13 +++--- 4 files changed, 76 insertions(+), 25 deletions(-) diff --git a/lib/LANraragi/Plugin/Metadata/Chaika.pm b/lib/LANraragi/Plugin/Metadata/Chaika.pm index 4b1fbb289..4e7f010fc 100644 --- a/lib/LANraragi/Plugin/Metadata/Chaika.pm +++ b/lib/LANraragi/Plugin/Metadata/Chaika.pm @@ -48,17 +48,17 @@ sub get_tags { # Parse the given link to see if we can extract type and ID my $oneshotarg = $lrr_info->{oneshot_param}; if ( $oneshotarg =~ /https?:\/\/panda\.chaika\.moe\/(gallery|archive)\/([0-9]*)\/?.*/ ) { - ( $newtags, $newtitle ) = tags_from_chaika_id( $1, $2, $addextra, $addother ); + ( $newtags, $newtitle ) = tags_from_chaika_id( $1, $2, $addextra, $addother, $addsource ); } else { # Try SHA-1 reverse search first $logger->info("Using thumbnail hash " . $lrr_info->{thumbnail_hash}); - ( $newtags, $newtitle ) = tags_from_sha1( $lrr_info->{thumbnail_hash}, $addextra, $addother ); + ( $newtags, $newtitle ) = tags_from_sha1( $lrr_info->{thumbnail_hash}, $addextra, $addother, $addsource ); # Try text search if it fails if ( $newtags eq "" ) { $logger->info("No results, falling back to text search."); - ( $newtags, $newtitle ) = search_for_archive( $lrr_info->{archive_title}, $lrr_info->{existing_tags}, $addextra, $addother ); + ( $newtags, $newtitle ) = search_for_archive( $lrr_info->{archive_title}, $lrr_info->{existing_tags}, $addextra, $addother, $addsource ); } } @@ -66,9 +66,6 @@ sub get_tags { $logger->info("No matching Chaika Archive Found!"); return ( error => "No matching Chaika Archive Found!" ); } else { - if ( $addsource ne "" ) { - $newtags .= ", source:" . $addsource; - } $logger->info("Sending the following tags to LRR: $newtags"); #Return a hash containing the new metadata if ( $savetitle && $newtags ne "" ) { return ( tags => $newtags, title => $newtitle ); } @@ -86,7 +83,7 @@ sub get_tags { sub search_for_archive { my $logger = get_plugin_logger(); - my ( $title, $tags, $addextra, $addother ) = @_; + my ( $title, $tags, $addextra, $addother, $addsource ) = @_; #Auto-lowercase the title for better results $title = lc($title); @@ -109,7 +106,7 @@ sub search_for_archive { my $textrep = $res->body; $logger->debug("Chaika API returned this JSON: $textrep"); - my ( $chaitags, $chaititle ) = parse_chaika_json( $res->json->{"galleries"}->[0], $addextra, $addother ); + my ( $chaitags, $chaititle ) = parse_chaika_json( $res->json->{"galleries"}->[0], $addextra, $addother, $addsource ); return ( $chaitags, $chaititle ); } @@ -117,17 +114,17 @@ sub search_for_archive { # Uses the jsearch API to get the best json for a file. sub tags_from_chaika_id { - my ( $type, $ID, $addextra, $addother ) = @_; + my ( $type, $ID, $addextra, $addother, $addsource ) = @_; my $json = get_json_from_chaika( $type, $ID ); - return parse_chaika_json( $json, $addextra, $addother ); + return parse_chaika_json( $json, $addextra, $addother, $addsource ); } # tags_from_sha1 # Uses chaika's SHA-1 search with the first page hash we have. sub tags_from_sha1 { - my ( $sha1, $addextra, $addother ) = @_; + my ( $sha1, $addextra, $addother, $addsource ) = @_; my $logger = get_plugin_logger(); @@ -135,7 +132,7 @@ sub tags_from_sha1 { # Said JSON is an array containing multiple archive objects. # We just take the first one. my $json_by_sha1 = get_json_from_chaika( 'sha1', $sha1 ); - return parse_chaika_json( $json_by_sha1->[0], $addextra, $addother ); + return parse_chaika_json( $json_by_sha1->[0], $addextra, $addother, $addsource ); } # Calls chaika's API @@ -160,7 +157,7 @@ sub get_json_from_chaika { # Parses the JSON obtained from the Chaika API to get the tags. sub parse_chaika_json { - my ( $json, $addextra, $addother ) = @_; + my ( $json, $addextra, $addother, $addsource ) = @_; my $tags = $json->{"tags"} || (); foreach my $tag (@$tags) { @@ -191,6 +188,10 @@ sub parse_chaika_json { push(@$tags, "timestamp:" . $timestamp); } } + # add custom source, but only if having found tags + if ($tags && $addsource ne "") { + push(@$tags, "source:" . $addsource); + } if ($gallery && $gallery ne "") { return ( join( ', ', @$tags ), $json->{"title"} ); } else { diff --git a/tests/LANraragi/Plugin/Metadata/Chaika.t b/tests/LANraragi/Plugin/Metadata/Chaika.t index 65e103d83..73b149b50 100644 --- a/tests/LANraragi/Plugin/Metadata/Chaika.t +++ b/tests/LANraragi/Plugin/Metadata/Chaika.t @@ -15,13 +15,57 @@ my $SAMPLES = "$cwd/tests/samples"; require "$cwd/tests/mocks.pl"; my @tags_list= ( - 'female:sole female', 'male:sole male', 'artist:kemuri haku', 'full censorship', - 'male:shotacon', 'female:defloration', 'female:nakadashi', 'female:big breasts', - 'language:translated', 'language:english' + 'full censorship', 'female:sole female', 'male:sole male', 'artist:kemuri haku', 'female:tall girl', + 'female:cunnilingus', 'male:shotacon', 'female:defloration', 'female:nakadashi', 'female:x-ray', + 'female:big breasts', 'language:translated', 'language:english' +); +my @tags_list_extra= ( + 'full censorship', 'female:sole female', 'male:sole male', 'artist:kemuri haku', 'female:tall girl', + 'female:cunnilingus', 'male:shotacon', 'female:defloration', 'female:nakadashi', 'female:x-ray', + 'female:big breasts', 'language:translated', 'language:english', 'category:manga', 'download:/archive/27240/download/', + 'gallery:23532', 'timestamp:1521357552', 'source:chaika' ); use_ok('LANraragi::Plugin::Metadata::Chaika'); +note ( 'testing retrieving tags by title search without additional tags...' ); + +{ + my $json = decode_json( Mojo::File->new("$SAMPLES/chaika/001_gid_27240.json")->slurp ); + + no warnings 'once', 'redefine'; + local *LANraragi::Plugin::Metadata::Chaika::get_json_from_chaika = sub { return $json; }; + + my ( $tags, $title ) = LANraragi::Plugin::Metadata::Chaika::search_for_archive( + "[Kemuri Haku] Zettai Seikou Keikaku" + ); + + is($title, $json->{title}, 'gallery title'); + cmp_bag( [ split( ', ', $tags ) ] , \@tags_list, 'gallery tag list'); +} + +note ( 'testing retrieving tags by title search with additional tags...' ); + +{ + my $json = decode_json( Mojo::File->new("$SAMPLES/chaika/001_gid_27240.json")->slurp ); + + no warnings 'once', 'redefine'; + local *LANraragi::Plugin::Metadata::Chaika::get_json_from_chaika = sub { return $json; }; + + my $addextra = 1; + my $addother = 1; + my $addsource = 'chaika'; + my ( $tags, $title ) = LANraragi::Plugin::Metadata::Chaika::search_for_archive( + "[Kemuri Haku] Zettai Seikou Keikaku", + $addextra, + $addother, + $addsource + ); + + is($title, $json->{title}, 'gallery title'); + cmp_bag( [ split( ', ', $tags ) ] , \@tags_list, 'gallery tag list'); +} + note ( 'testing retrieving tags by ID ...' ); { diff --git a/tests/samples/chaika/001_gid_27240.json b/tests/samples/chaika/001_gid_27240.json index c9524524d..bdff12b94 100644 --- a/tests/samples/chaika/001_gid_27240.json +++ b/tests/samples/chaika/001_gid_27240.json @@ -6,16 +6,19 @@ "filesize": 63418139, "fjord": true, "gallery": 23532, - "posted": 1521353952, - "rating": 4.45, + "posted": 1521357552, + "rating": 4.49, "tags": [ + "full_censorship", "female:sole_female", "male:sole_male", "artist:kemuri_haku", - "full_censorship", + "female:tall_girl", + "female:cunnilingus", "male:shotacon", "female:defloration", "female:nakadashi", + "female:x-ray", "female:big_breasts", "language:translated", "language:english" @@ -23,4 +26,4 @@ "title": "[Kemuri Haku] Zettai Seikou Keikaku | Absolute Intercourse Plan (COMIC Shitsurakuten 2016-03) [English] [Redlantern]", "title_jpn": "[煙ハク] 絶対セイコウ計画 (COMIC 失楽天 2016年3月号) [英訳]", "uploader": "dankestdungeon" -} \ No newline at end of file +} diff --git a/tests/samples/chaika/002_sha1_response.json b/tests/samples/chaika/002_sha1_response.json index ab66d1d7a..bdff12b94 100644 --- a/tests/samples/chaika/002_sha1_response.json +++ b/tests/samples/chaika/002_sha1_response.json @@ -1,4 +1,4 @@ -[{ +{ "category": "Manga", "download": "/archive/27240/download/", "expunged": false, @@ -6,16 +6,19 @@ "filesize": 63418139, "fjord": true, "gallery": 23532, - "posted": 1521353952, - "rating": 4.45, + "posted": 1521357552, + "rating": 4.49, "tags": [ + "full_censorship", "female:sole_female", "male:sole_male", "artist:kemuri_haku", - "full_censorship", + "female:tall_girl", + "female:cunnilingus", "male:shotacon", "female:defloration", "female:nakadashi", + "female:x-ray", "female:big_breasts", "language:translated", "language:english" @@ -23,4 +26,4 @@ "title": "[Kemuri Haku] Zettai Seikou Keikaku | Absolute Intercourse Plan (COMIC Shitsurakuten 2016-03) [English] [Redlantern]", "title_jpn": "[煙ハク] 絶対セイコウ計画 (COMIC 失楽天 2016年3月号) [英訳]", "uploader": "dankestdungeon" -}] \ No newline at end of file +}