Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kagoromo committed Feb 7, 2023
1 parent 871f5d4 commit 58aac3e
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 25 deletions.
27 changes: 14 additions & 13 deletions lib/LANraragi/Plugin/Metadata/Chaika.pm
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,24 @@ 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 );
}
}

if ( $newtags eq "" ) {
$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 ); }
Expand All @@ -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);
Expand All @@ -109,33 +106,33 @@ 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 );
}

# 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();

# The jsearch API immediately returns a JSON.
# 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
Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
42 changes: 39 additions & 3 deletions tests/LANraragi/Plugin/Metadata/Chaika.t
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ 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');
Expand Down Expand Up @@ -58,4 +64,34 @@ note ( 'testing retrieving tags by SHA1 ...' );
cmp_deeply( \@type_params, [ 'sha1' ], 'API call sequence');
}

note ( 'testing retrieving tags by SHA1 with additional tags ...' );

{
my $json_by_sha1 = decode_json( Mojo::File->new("$SAMPLES/chaika/002_sha1_response.json")->slurp );
my $json = decode_json( Mojo::File->new("$SAMPLES/chaika/001_gid_27240.json")->slurp );
my @type_params = ();

no warnings 'once', 'redefine';
local *LANraragi::Plugin::Metadata::Chaika::get_plugin_logger = sub { return get_logger_mock(); };
local *LANraragi::Plugin::Metadata::Chaika::get_json_from_chaika = sub {
my ( $type, $value ) = @_;
push( @type_params, $type );
return ( $type eq 'sha1' ) ? $json_by_sha1 : $json;
};

my $addextra = 1;
my $addother = 1;
my $addsource = 'chaika';
my ( $tags, $title ) = LANraragi::Plugin::Metadata::Chaika::tags_from_sha1(
"my-hash",
$addextra,
$addother,
$addsource
);

is($title, $json->{title}, 'gallery title');
cmp_bag( [ split( ', ', $tags ) ] , \@tags_list_extra, 'gallery tag list');
cmp_deeply( \@type_params, [ 'sha1' ], 'API call sequence');
}

done_testing();
11 changes: 7 additions & 4 deletions tests/samples/chaika/001_gid_27240.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@
"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"
],
"title": "[Kemuri Haku] Zettai Seikou Keikaku | Absolute Intercourse Plan (COMIC Shitsurakuten 2016-03) [English] [Redlantern]",
"title_jpn": "[煙ハク] 絶対セイコウ計画 (COMIC 失楽天 2016年3月号) [英訳]",
"uploader": "dankestdungeon"
}
}
13 changes: 8 additions & 5 deletions tests/samples/chaika/002_sha1_response.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
[{
{
"category": "Manga",
"download": "/archive/27240/download/",
"expunged": false,
"filecount": 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"
],
"title": "[Kemuri Haku] Zettai Seikou Keikaku | Absolute Intercourse Plan (COMIC Shitsurakuten 2016-03) [English] [Redlantern]",
"title_jpn": "[煙ハク] 絶対セイコウ計画 (COMIC 失楽天 2016年3月号) [英訳]",
"uploader": "dankestdungeon"
}]
}

0 comments on commit 58aac3e

Please sign in to comment.