diff --git a/lib/LANraragi/Controller/Batch.pm b/lib/LANraragi/Controller/Batch.pm index cd6f6fbd3..218084b5e 100644 --- a/lib/LANraragi/Controller/Batch.pm +++ b/lib/LANraragi/Controller/Batch.pm @@ -134,7 +134,7 @@ sub socket { $tags = redis_decode($tags); my @tagarray = split_tags_to_array($tags); - @tagarray = rewrite_tags(\@tagarray, $rules, $hash_replace_rules); + @tagarray = rewrite_tags( \@tagarray, $rules, $hash_replace_rules ); # Merge array with commas my $newtags = join( ', ', @tagarray ); @@ -200,12 +200,7 @@ sub batch_plugin { my ( $id, $plugin, @args ) = @_; # Run plugin with args on id - my %plugin_result; - eval { %plugin_result = LANraragi::Model::Plugins::exec_metadata_plugin( $plugin, $id, "", @args ); }; - - if ($@) { - $plugin_result{error} = $@; - } + my %plugin_result = LANraragi::Model::Plugins::exec_metadata_plugin( $plugin, $id, "", @args ); # If the plugin exec returned tags, add them unless ( exists $plugin_result{error} ) { diff --git a/lib/LANraragi/Model/Plugins.pm b/lib/LANraragi/Model/Plugins.pm index 2ffca3f39..ae98c6a87 100644 --- a/lib/LANraragi/Model/Plugins.pm +++ b/lib/LANraragi/Model/Plugins.pm @@ -1,5 +1,8 @@ package LANraragi::Model::Plugins; +use v5.36; +use experimental 'try'; + use strict; use warnings; use utf8; @@ -53,42 +56,36 @@ sub exec_enabled_plugins_on_file { my %pluginfo = $plugin->plugin_info(); - #Every plugin execution is eval'd separately - eval { %plugin_result = exec_metadata_plugin( $plugin, $id, "", @args ); }; + %plugin_result = exec_metadata_plugin( $plugin, $id, "", @args ); - if ($@) { - $failures++; - $logger->error("$@"); - } elsif ( exists $plugin_result{error} ) { + if ( exists $plugin_result{error} ) { $failures++; $logger->error( $plugin_result{error} ); - } else { - $successes++; + next; } - #If the plugin exec returned metadata, add it - unless ( exists $plugin_result{error} ) { - set_tags( $id, $plugin_result{new_tags}, 1 ); + $successes++; - # Sum up all the added tags for later reporting. - # This doesn't take into account tags that are added twice - # (e.g by different plugins), but since this is more meant to show - # if the plugins added any data at all it's fine. - my @added_tags = split( ',', $plugin_result{new_tags} ); - $addedtags += @added_tags; + #If the plugin exec returned metadata, add it + set_tags( $id, $plugin_result{new_tags}, 1 ); - if ( exists $plugin_result{title} ) { - set_title( $id, $plugin_result{title} ); + # Sum up all the added tags for later reporting. + # This doesn't take into account tags that are added twice + # (e.g by different plugins), but since this is more meant to show + # if the plugins added any data at all it's fine. + my @added_tags = split( ',', $plugin_result{new_tags} ); + $addedtags += @added_tags; - $newtitle = $plugin_result{title}; - $logger->debug("Changing title to $newtitle. (Will do nothing if title is blank)"); - } + if ( exists $plugin_result{title} ) { + set_title( $id, $plugin_result{title} ); - if ( exists $plugin_result{summary} ) { - set_summary( $id, $plugin_result{summary} ); - $logger->debug("Summary has been changed."); # don't put the new summary in logs, it can be huge - } + $newtitle = $plugin_result{title}; + $logger->debug("Changing title to $newtitle. (Will do nothing if title is blank)"); + } + if ( exists $plugin_result{summary} ) { + set_summary( $id, $plugin_result{summary} ); + $logger->debug("Summary has been changed."); # don't put the new summary in logs, it can be huge } } @@ -128,25 +125,29 @@ sub exec_login_plugin { sub exec_script_plugin { my ( $plugin, $input, @settings ) = @_; - my $logger = get_logger( "Plugin System", "lanraragi" ); + + no warnings 'experimental::try'; #If the plugin has the method "run_script", #catch all the required data and feed it to the plugin if ( $plugin->can('run_script') ) { - my %pluginfo = $plugin->plugin_info(); - my $ua = exec_login_plugin( $pluginfo{login_from} ); - - # Bundle all the potentially interesting info in a hash - my %infohash = ( - user_agent => $ua, - oneshot_param => $input - ); - - # Scripts don't have any predefined metadata in their spec so they're just ran as-is. - # They can return whatever the heck they want in their hash as well, they'll just be shown as-is in the API output. - my %result = $plugin->run_script( \%infohash, @settings ); - return %result; + try { + my %pluginfo = $plugin->plugin_info(); + my $ua = exec_login_plugin( $pluginfo{login_from} ); + + # Bundle all the potentially interesting info in a hash + my %infohash = ( + user_agent => $ua, + oneshot_param => $input + ); + + # Scripts don't have any predefined metadata in their spec so they're just ran as-is. + # They can return whatever the heck they want in their hash as well, they'll just be shown as-is in the API output. + return $plugin->run_script( \%infohash, @settings ); + } catch ($e) { + return ( error => $e ); + } } return ( error => "Plugin doesn't implement run_script despite having a 'script' type." ); } @@ -193,40 +194,44 @@ sub exec_download_plugin { sub exec_metadata_plugin { my ( $plugin, $id, $oneshotarg, @args ) = @_; + + no warnings 'experimental::try'; + my $logger = get_logger( "Plugin System", "lanraragi" ); - if ( $id eq 0 ) { + if ( !$id ) { return ( error => "Tried to call a metadata plugin without providing an id." ); } - #If the plugin has the method "get_tags", - #catch all the required data and feed it to the plugin - if ( $plugin->can('get_tags') ) { + if ( !$plugin->can('get_tags') ) { + return ( error => "Plugin doesn't implement get_tags despite having a 'metadata' type." ); + } - my $redis = LANraragi::Model::Config->get_redis; - my %hash = $redis->hgetall($id); + my $redis = LANraragi::Model::Config->get_redis; + my %hash = $redis->hgetall($id); - my ( $name, $title, $tags, $file, $thumbhash ) = @hash{qw(name title tags file thumbhash)}; + my ( $name, $title, $tags, $file, $thumbhash ) = @hash{qw(name title tags file thumbhash)}; - ( $_ = LANraragi::Utils::Database::redis_decode($_) ) for ( $name, $title, $tags ); + ( $_ = LANraragi::Utils::Database::redis_decode($_) ) for ( $name, $title, $tags ); - # If the thumbnail hash is empty or undefined, we'll generate it here. - unless ( length $thumbhash ) { - $logger->info("Thumbnail hash invalid, regenerating."); - my $thumbdir = LANraragi::Model::Config->get_thumbdir; + # If the thumbnail hash is empty or undefined, we'll generate it here. + unless ( length $thumbhash ) { + $logger->info("Thumbnail hash invalid, regenerating."); + my $thumbdir = LANraragi::Model::Config->get_thumbdir; + $thumbhash = ""; - # Eval the thumbnail extraction, as it can error out and die - eval { extract_thumbnail( $thumbdir, $id, 0, 1 ) }; - if ($@) { - $logger->warn("Error building thumbnail: $@"); - $thumbhash = ""; - } else { - $thumbhash = $redis->hget( $id, "thumbhash" ); - $thumbhash = LANraragi::Utils::Database::redis_decode($thumbhash); - } + try { + extract_thumbnail( $thumbdir, $id, 0, 1 ); + $thumbhash = $redis->hget( $id, "thumbhash" ); + $thumbhash = LANraragi::Utils::Database::redis_decode($thumbhash); + } catch ($e) { + $logger->warn("Error building thumbnail: $e"); } - $redis->quit(); + } + $redis->quit(); + my %returnhash; + try { # Hand it off to the plugin here. # If the plugin requires a login, execute that first to get a UserAgent my %pluginfo = $plugin->plugin_info(); @@ -243,8 +248,11 @@ sub exec_metadata_plugin { oneshot_param => $oneshotarg ); - my %newmetadata = $plugin->get_tags( \%infohash, @args ); + my %newmetadata; + %newmetadata = $plugin->get_tags( \%infohash, @args ); + + # TODO: remove this block after changing all the metadata plugins #Error checking if ( exists $newmetadata{error} ) { @@ -274,11 +282,10 @@ sub exec_metadata_plugin { # Strip last comma and return processed tags in a hash chop($newtags); - my %returnhash = ( new_tags => $newtags ); + %returnhash = ( new_tags => $newtags ); # Indicate a title change, if the plugin reports one if ( exists $newmetadata{title} && LANraragi::Model::Config->can_replacetitles ) { - my $newtitle = $newmetadata{title}; $newtitle = trim($newtitle); $returnhash{title} = $newtitle; @@ -289,9 +296,11 @@ sub exec_metadata_plugin { $returnhash{summary} = $newmetadata{summary}; } - return %returnhash; + } catch ($e) { + return ( error => $e ); } - return ( error => "Plugin doesn't implement get_tags despite having a 'metadata' type." ); + + return %returnhash; } 1; diff --git a/lib/LANraragi/Plugin/Metadata/Chaika.pm b/lib/LANraragi/Plugin/Metadata/Chaika.pm index bffbc1fe3..e7e0fa553 100644 --- a/lib/LANraragi/Plugin/Metadata/Chaika.pm +++ b/lib/LANraragi/Plugin/Metadata/Chaika.pm @@ -19,7 +19,7 @@ sub plugin_info { type => "metadata", namespace => "trabant", author => "Difegue", - version => "2.3.1", + version => "2.4", description => "Searches chaika.moe for tags matching your archive. This will try to use the thumbnail first, and fallback to a default text search.", icon => @@ -66,14 +66,18 @@ sub get_tags { # 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, $addsource, $jpntitle ); + ( $newtags, $newtitle ) = search_for_archive( + $lrr_info->{archive_title}, + $lrr_info->{existing_tags}, + $addextra, $addother, $addsource, $jpntitle + ); } } if ( $newtags eq "" ) { - $logger->info("No matching Chaika Archive Found!"); - return ( error => "No matching Chaika Archive Found!" ); + my $message = "No matching Chaika Archive Found!"; + $logger->info($message); + die "${message}\n"; } else { $logger->info("Sending the following tags to LRR: $newtags"); diff --git a/lib/LANraragi/Plugin/Metadata/ChaikaFile.pm b/lib/LANraragi/Plugin/Metadata/ChaikaFile.pm index effe94869..1135ac71b 100644 --- a/lib/LANraragi/Plugin/Metadata/ChaikaFile.pm +++ b/lib/LANraragi/Plugin/Metadata/ChaikaFile.pm @@ -16,7 +16,7 @@ sub plugin_info { type => "metadata", namespace => "chaikafileplugin", author => "Difegue & Plebs", - version => "0.2", + version => "0.3", description => "Collects metadata embedded into your archives as Chaika-style api.json files", icon => "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAAsTAAALEwEAmpwYAAAA\nB3RJTUUH4wYCFQocjU4r+QAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUH\nAAAEZElEQVQ4y42T3WtTdxzGn/M7J+fk5SRpTk7TxMZkXU84tTbVNrUT3YxO7HA4pdtQZDe7cgx2\ns8vBRvEPsOwFYTDYGJUpbDI2wV04cGXCGFLonIu1L2ptmtrmxeb1JDkvv121ZKVze66f74eH7/f5\nMmjRwMCAwrt4/9KDpflMJpPHvyiR2DPcJklJ3TRDDa0xk36cvrm8vDwHAAwAqKrqjjwXecPG205w\nHBuqa9rk77/d/qJYLD7cCht5deQIIczbgiAEKLVAKXWUiqVV06Tf35q8dYVJJBJem2A7Kwi2nQzD\nZig1CG93+PO5/KN6tf5NKpVqbsBUVVVFUUxwHJc1TXNBoxojS7IbhrnLMMx9pVJlBqFQKBKPxwcB\nkJYgjKIo3QCE1nSKoghbfJuKRqN2RVXexMaQzWaLezyeEUEQDjscjk78PxFFUYRkMsltJgGA3t7e\nyMLCwie6rr8iCILVbDbvMgwzYRjGxe0o4XC4s1AoHPP5fMP5/NNOyzLKAO6Ew+HrDADBbre/Ryk9\nnzx81FXJNlEpVpF+OqtpWu2MpmnXWmH9/f2umZmZi4cOHXnLbILLzOchhz1YerJAs9m1GwRAg2GY\nh7GYah488BJYzYW+2BD61AFBlmX/1nSNRqN9//792ujoaIPVRMjOKHoie3DytVGmp2fXCAEAjuMm\nu7u7Umosho6gjL/u/QHeEgvJZHJ2K/D+/fuL4+PjXyvPd5ldkShy1UXcmb4DnjgQj/fd5gDA6/XS\nYCAwTwh9oT3QzrS1+VDVi+vd3Tsy26yQVoFF3dAXJVmK96p9EJ0iLNOwKKU3CQCk0+lSOpP5WLDz\nF9Q9kZqyO0SloOs6gMfbHSU5NLRiUOuax2/HyZPHEOsLw2SbP83eu/fLxrkNp9P554XxCzVa16MC\n7+BPnTk9cfmH74KJE8nmga7Xy5JkZ8VKifGIHpoBb1VX8hNTd3/t/7lQ3OeXfFPvf/jBRw8ezD/a\n7M/aWq91cGgnJaZ2VcgSdnV1XRNNd3vAoBVVYusmnEQS65hfgSG6c+zy3Kre7nF/KrukcMW0Zg8O\nD08DoJutDxxOEb5IPUymwrq8ft1gLKfkFojkkRxemERCAQUACPFWRazYLJcrFGwQhyufbQQ7rFpy\nLMkCwGZC34qPIuwp+XPOjBFwazQ/txrdFS2GGS/Xuj+pUKLGk1Kjvlded3s72lyGW+PLbGVcmrAA\ngN0wTk1NWYODg9XOKltGtpazi5GigzroUnHN5nUHG1ylRsG7rDXHmnEpu4CeEtEKkqNc6QqlLc/M\n8uT5lLH5eq0aGxsju1O7GQB498a5s/0x9dRALPaQEDZnYwnhWJtMCCNrjeb0UP34Z6e/PW22zjPP\n+vwXBwfPvbw38XnXjk7GsiwKAIQQhjAMMrlsam45d+zLH6/8o6vkWcBcrXbVKQhf6bpucCwLjmUB\nSmmhXC419eblrbD/TAgAkUjE987xE0c7ZDmk66ajUCnq+cL63fErl25s5/8baQPaWLhx6goAAAAA\nSUVORK5CYII=", @@ -54,8 +54,9 @@ sub get_tags { } if ( $newtags eq "" ) { - $logger->info("No Chaika File Found!"); - return ( error => "No Chaika File Found!" ); + my $message = "No Chaika File Found!"; + $logger->info($message); + die "${message}\n"; } else { $logger->info("Sending the following tags to LRR: $newtags"); @@ -81,7 +82,7 @@ sub tags_from_file { my $stringjson = ""; open( my $fh, '<:encoding(UTF-8)', $filepath ) - or return ( error => "Could not open $filepath!" ); + or die "Could not open $filepath!\n"; while ( my $row = <$fh> ) { chomp $row; diff --git a/lib/LANraragi/Plugin/Metadata/ComicInfo.pm b/lib/LANraragi/Plugin/Metadata/ComicInfo.pm index 68316384a..585a2d0d5 100644 --- a/lib/LANraragi/Plugin/Metadata/ComicInfo.pm +++ b/lib/LANraragi/Plugin/Metadata/ComicInfo.pm @@ -9,19 +9,20 @@ use Mojo::DOM; use LANraragi::Model::Plugins; use LANraragi::Utils::Logging qw(get_plugin_logger); use LANraragi::Utils::Archive qw(is_file_in_archive extract_file_from_archive); +use LANraragi::Utils::String qw(trim); #Meta-information about your plugin. sub plugin_info { return ( #Standard metadata - name => "ComicInfo", - type => "metadata", - namespace => "comicinfo", - author => "Gin-no-kami", - version => "1.0", - description => "Parses metadata from ComicInfo.xml embedded in the archive", - parameters => [] + name => "ComicInfo", + type => "metadata", + namespace => "comicinfo", + author => "Gin-no-kami", + version => "1.1", + description => "Parses metadata from ComicInfo.xml embedded in the archive", + parameters => [] ); } @@ -30,91 +31,89 @@ sub plugin_info { sub get_tags { shift; - my $lrr_info = shift; # Global info hash, contains various metadata provided by LRR + my $lrr_info = shift; # Global info hash, contains various metadata provided by LRR #Use the logger to output status - they'll be passed to a specialized logfile and written to STDOUT. my $logger = get_plugin_logger(); - my $file = $lrr_info->{file_path}; + my $file = $lrr_info->{file_path}; my $path_in_archive = is_file_in_archive( $file, "ComicInfo.xml" ); - if ( $path_in_archive ) { - #Extract ComicInfo.xml - my $filepath = extract_file_from_archive( $file, $path_in_archive ); - - #Read file into string - my $stringxml = ""; - open( my $fh, '<:encoding(UTF-8)', $filepath ) - or return ( error => "Could not open $filepath!" ); - while ( my $line = <$fh> ) { - chomp $line; - $stringxml .= $line; - } - - #Parse file into DOM object and extract tags - my $genre; - my $calibretags; - my $group; - my $url; - my $artist; - my $lang; - my $result = Mojo::DOM->new->xml(1)->parse($stringxml)->at('Genre'); - if (defined $result) { - $genre = $result->text; - } - $result = Mojo::DOM->new->xml(1)->parse($stringxml)->at('Tags'); - if (defined $result) { - $calibretags = $result->text; - } - $result = Mojo::DOM->new->xml(1)->parse($stringxml)->at('Web'); - if (defined $result) { - $url = $result->text; - } - $result = Mojo::DOM->new->xml(1)->parse($stringxml)->at('Writer'); - if (defined $result) { - $group = $result->text; - } - $result = Mojo::DOM->new->xml(1)->parse($stringxml)->at('Penciller'); - if (defined $result) { - $artist = $result->text; - } - $result = Mojo::DOM->new->xml(1)->parse($stringxml)->at('LanguageISO'); - if (defined $result) { - $lang = $result->text; - } - - #Delete local file - unlink $filepath; - - - #Add prefix and concatenate - my @found_tags; - @found_tags = try_add_tags(\@found_tags, "group:", $group); - @found_tags = try_add_tags(\@found_tags, "artist:", $artist); - @found_tags = try_add_tags(\@found_tags, "source:", $url); - push( @found_tags, "language:" . $lang ) unless !$lang; - my @genres = split(',', $genre // ""); - if ($calibretags) { - push @genres, split(',', $calibretags); - } - foreach my $genre_tag (@genres){ - push(@found_tags, trim($genre_tag)); - } - my $tags = join( ", ", @found_tags ); - - $logger->info("Sending the following tags to LRR: $tags"); - return ( tags => $tags ); + die "No ComicInfo.xml file found in the archive\n" if ( !$path_in_archive ); + + #Extract ComicInfo.xml + my $filepath = extract_file_from_archive( $file, $path_in_archive ); + + #Read file into string + my $stringxml = ""; + open( my $fh, '<:encoding(UTF-8)', $filepath ) + or die "Could not open $filepath!\n"; + while ( my $line = <$fh> ) { + chomp $line; + $stringxml .= $line; + } + + #Parse file into DOM object and extract tags + my $genre; + my $calibretags; + my $group; + my $url; + my $artist; + my $lang; + my $result = Mojo::DOM->new->xml(1)->parse($stringxml)->at('Genre'); + + if ( defined $result ) { + $genre = $result->text; + } + $result = Mojo::DOM->new->xml(1)->parse($stringxml)->at('Tags'); + if ( defined $result ) { + $calibretags = $result->text; + } + $result = Mojo::DOM->new->xml(1)->parse($stringxml)->at('Web'); + if ( defined $result ) { + $url = $result->text; + } + $result = Mojo::DOM->new->xml(1)->parse($stringxml)->at('Writer'); + if ( defined $result ) { + $group = $result->text; + } + $result = Mojo::DOM->new->xml(1)->parse($stringxml)->at('Penciller'); + if ( defined $result ) { + $artist = $result->text; } - - return ( error => "No ComicInfo.xml file found in archive"); + $result = Mojo::DOM->new->xml(1)->parse($stringxml)->at('LanguageISO'); + if ( defined $result ) { + $lang = $result->text; + } + + #Delete local file + unlink $filepath; + + #Add prefix and concatenate + my @found_tags; + @found_tags = try_add_tags( \@found_tags, "group:", $group ); + @found_tags = try_add_tags( \@found_tags, "artist:", $artist ); + @found_tags = try_add_tags( \@found_tags, "source:", $url ); + push( @found_tags, "language:" . $lang ) unless !$lang; + my @genres = split( ',', $genre // "" ); + if ($calibretags) { + push @genres, split( ',', $calibretags ); + } + foreach my $genre_tag (@genres) { + push( @found_tags, trim($genre_tag) ); + } + my $tags = join( ", ", @found_tags ); + + $logger->info("Sending the following tags to LRR: $tags"); + return ( tags => $tags ); } sub try_add_tags { - my @found_tags = @{$_[0]}; - my $prefix = $_[1]; - my $tags = $_[2] // ""; - - my @tags_array = split(',', $tags); + my @found_tags = @{ $_[0] }; + my $prefix = $_[1]; + my $tags = $_[2] // ""; + + my @tags_array = split( ',', $tags ); foreach my $tag (@tags_array) { push( @found_tags, $prefix . trim($tag) ); @@ -122,6 +121,4 @@ sub try_add_tags { return @found_tags; } -sub trim { my $s = shift; $s =~ s/^\s+|\s+$//g; return $s }; - 1; \ No newline at end of file diff --git a/lib/LANraragi/Plugin/Metadata/CopyArchiveTags.pm b/lib/LANraragi/Plugin/Metadata/CopyArchiveTags.pm index 481339806..f17823fca 100644 --- a/lib/LANraragi/Plugin/Metadata/CopyArchiveTags.pm +++ b/lib/LANraragi/Plugin/Metadata/CopyArchiveTags.pm @@ -1,9 +1,5 @@ package LANraragi::Plugin::Metadata::CopyArchiveTags; -use v5.36; -use experimental 'try'; -no warnings 'experimental::try'; - use strict; use warnings; @@ -22,7 +18,7 @@ sub plugin_info { type => "metadata", namespace => "copy-archive-tags", author => "IceBreeze", - version => "1.0", + version => "1.1", description => "Copy tags from another LRR archive given either the URI or the ID.", parameters => [ { type => "bool", @@ -39,22 +35,6 @@ sub get_tags { my $params = read_params(@_); my $logger = get_plugin_logger(); - # should be handled in the caller - my %hashdata; - try { - %hashdata = internal_get_tags( $logger, $params ); - } catch ($e) { - $logger->error($e); - return ( error => $e ); - } - - $logger->info( "Sending the following tags to LRR: " . ( $hashdata{tags} || '-' ) ); - return %hashdata; -} - -sub internal_get_tags { - my ( $logger, $params ) = @_; - my $lrr_gid = extract_archive_id( $params->{'oneshot'} ); if ( !$lrr_gid ) { die "oneshot_param doesn't contain a valid archive ID\n"; @@ -66,16 +46,16 @@ sub internal_get_tags { $logger->info("Copying tags from archive \"${lrr_gid}\""); - my $tags = LANraragi::Utils::Database::get_tags($lrr_gid); + my $tags = LANraragi::Utils::Database::get_tags($lrr_gid) || ''; if ( !$params->{'copy_date_added'} ) { my @tags = split_tags_to_array($tags); $tags = join_tags_to_string( grep( !m/date_added/, @tags ) ); } - my %hashdata = ( tags => $tags ); + $logger->info( "Sending the following tags to LRR: " . ( $tags || '-' ) ); - return %hashdata; + return ( tags => $tags || '' ); } sub extract_archive_id { diff --git a/lib/LANraragi/Plugin/Metadata/EHentai.pm b/lib/LANraragi/Plugin/Metadata/EHentai.pm index 2775a043d..c62388beb 100644 --- a/lib/LANraragi/Plugin/Metadata/EHentai.pm +++ b/lib/LANraragi/Plugin/Metadata/EHentai.pm @@ -1,5 +1,8 @@ package LANraragi::Plugin::Metadata::EHentai; +use v5.36; +use experimental 'try'; + use strict; use warnings; no warnings 'uninitialized'; @@ -25,7 +28,7 @@ sub plugin_info { namespace => "ehplugin", login_from => "ehlogin", author => "Difegue and others", - version => "2.5.2", + version => "2.6", description => "Searches g.e-hentai for tags matching your archive.
This plugin will use the source: tag of the archive if it exists.", icon => @@ -51,6 +54,8 @@ sub plugin_info { #Mandatory function to be implemented by your plugin sub get_tags { + no warnings 'experimental::try'; + shift; my $lrr_info = shift; # Global info hash my $ua = $lrr_info->{user_agent}; @@ -78,26 +83,23 @@ sub get_tags { } else { # Craft URL for Text Search on EH if there's no user argument - ( $gID, $gToken ) = &lookup_gallery( - $lrr_info->{archive_title}, - $lrr_info->{existing_tags}, - $lrr_info->{thumbnail_hash}, - $ua, $domain, $lang, $usethumbs, $search_gid, $expunged - ); + try { + ( $gID, $gToken ) = &lookup_gallery( + $lrr_info->{archive_title}, + $lrr_info->{existing_tags}, + $lrr_info->{thumbnail_hash}, + $ua, $domain, $lang, $usethumbs, $search_gid, $expunged + ); + } catch ($e) { + $logger->error($e); + die $e; + } } - # If an error occured, return a hash containing an error message. - # LRR will display that error to the client. - # Using the GToken to store error codes - not the cleanest but it's convenient if ( $gID eq "" ) { - - if ( $gToken ne "" ) { - $logger->error($gToken); - return ( error => $gToken ); - } - - $logger->info("No matching EH Gallery Found!"); - return ( error => "No matching EH Gallery Found!" ); + my $message = "No matching EH Gallery Found!"; + $logger->info($message); + die "${message}\n"; } else { $logger->debug("EH API Tokens are $gID / $gToken"); } @@ -187,33 +189,22 @@ sub lookup_gallery { # ehentai_parse(URL, UA) # Performs a remote search on e- or exhentai, and returns the ID/token matching the found gallery. -sub ehentai_parse() { +sub ehentai_parse { my ( $url, $ua ) = @_; my $logger = get_plugin_logger(); - my ( $dom, $error ) = search_gallery( $url, $ua ); - if ($error) { - return ( "", $error ); - } + my $dom = search_gallery( $url, $ua ); - my $gID = ""; - my $gToken = ""; - - eval { - # Get the first row of the search results - # The "glink" class is parented by a tag containing the gallery link in href. - # This works in Minimal, Minimal+ and Compact modes, which should be enough. - my $firstgal = $dom->at(".glink")->parent->attr('href'); + # Get the first row of the search results + # The "glink" class is parented by a tag containing the gallery link in href. + # This works in Minimal, Minimal+ and Compact modes, which should be enough. + my $firstgal = $dom->at(".glink")->parent->attr('href'); - # A EH link looks like xhentai.org/g/{gallery id}/{gallery token} - my $url = ( split( 'hentai.org/g/', $firstgal ) )[1]; - my @values = ( split( '/', $url ) ); - - $gID = $values[0]; - $gToken = $values[1]; - }; + # A EH link looks like xhentai.org/g/{gallery id}/{gallery token} + my $url = ( split( 'hentai.org/g/', $firstgal ) )[1]; + my ( $gID, $gToken ) = ( split( '/', $url ) ); if ( index( $dom->to_string, "You are opening" ) != -1 ) { my $rand = 15 + int( rand( 51 - 15 ) ); @@ -233,10 +224,10 @@ sub search_gallery { my $res = $ua->max_redirects(5)->get($url)->result; if ( index( $res->body, "Your IP address has been" ) != -1 ) { - return ( "", "Temporarily banned from EH for excessive pageloads." ); + die "Temporarily banned from EH for excessive pageloads.\n"; } - return ( $res->dom, undef ); + return $res->dom; } # get_tags_from_EH(userAgent, gID, gToken, jpntitle, additionaltags) @@ -250,11 +241,6 @@ sub get_tags_from_EH { my $jsonresponse = get_json_from_EH( $ua, $gID, $gToken ); - #if an error occurs(no response) return empty strings. - if ( !$jsonresponse ) { - return ( "", "" ); - } - my $data = $jsonresponse->{"gmetadata"}; my @tags = @{ @$data[0]->{"tags"} }; my $ehtitle = @$data[0]->{ ( $jpntitle ? "title_jpn" : "title" ) }; @@ -301,7 +287,8 @@ sub get_json_from_EH { my $jsonresponse = $rep->json; if ( exists $jsonresponse->{"error"} ) { - return; + $logger->error( $jsonresponse->{"error"} ); + die "E-H API returned an error.\n"; } return $jsonresponse; diff --git a/lib/LANraragi/Plugin/Metadata/Eze.pm b/lib/LANraragi/Plugin/Metadata/Eze.pm index c6103dfab..b503643c2 100644 --- a/lib/LANraragi/Plugin/Metadata/Eze.pm +++ b/lib/LANraragi/Plugin/Metadata/Eze.pm @@ -25,7 +25,7 @@ sub plugin_info { type => "metadata", namespace => "ezeplugin", author => "Difegue", - version => "2.3.2", + version => "2.4", description => "Collects metadata from eze-style info.json files ({'gallery_info': {xxx} } syntax), either embedded in your archive or in the same folder with the same name. ({archive_name}.json)", icon => @@ -67,14 +67,14 @@ sub get_tags { $logger->debug("Found file nearby at $filepath"); $delete_after_parse = 0; } else { - return ( error => "No in-archive info.json or {archive_name}.json file found!" ); + die "No in-archive info.json or {archive_name}.json file found!\n"; } #Open it my $stringjson = ""; open( my $fh, '<:encoding(UTF-8)', $filepath ) - or return ( error => "Could not open $filepath!" ); + or die "Could not open $filepath!\n"; while ( my $row = <$fh> ) { chomp $row; diff --git a/lib/LANraragi/Plugin/Metadata/Fakku.pm b/lib/LANraragi/Plugin/Metadata/Fakku.pm index 7bd6e5ae9..742e84a17 100644 --- a/lib/LANraragi/Plugin/Metadata/Fakku.pm +++ b/lib/LANraragi/Plugin/Metadata/Fakku.pm @@ -25,9 +25,9 @@ sub plugin_info { namespace => "fakkumetadata", login_from => "fakkulogin", author => "Difegue, Nodja, Nixis198", - version => "0.97", + version => "0.98", description => - "Searches FAKKU for tags matching your archive. If you have an account, don't forget to enter the matching cookie in the login plugin to be able to access controversial content.

+ "Searches FAKKU for tags matching your archive. If you have an account, don't forget to enter the matching cookie in the login plugin to be able to access controversial content.

This plugin can and will return invalid results depending on what you're searching for!
The FAKKU search API isn't very precise and I recommend you use the Chaika.moe plugin when possible.", icon => "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAFiUAABYlAUlSJPAAAACZSURBVDhPlY+xDYQwDEWvZgRGYA22Y4frqJDSZhFugiuuo4cqPGT0iTjAYL3C+fGzktc3hEcsQvJq6HtjE2Jdv4viH4a4pWnL8q4A6g+ET9P8YhS2/kqwIZXWnwqChDxPfCFfD76wOzJ2IOR/0DSwnuRKYAKUW3gq2OsJTYM0jr7QVRVwlabJEaw3ARYBcmFXeomxphIeEMIMmh3lOLQR+QQAAAAASUVORK5CYII=", @@ -49,73 +49,29 @@ sub get_tags { # Work your magic here - You can create subs below to organize the code better - my $cookie_jar = $ua->cookie_jar; - my $cookies = $cookie_jar->all; - - $logger->debug("Checking Cookies"); - if (@$cookies) { - - my $neededCookie = 0; - - for my $cookie (@$cookies) { - if ( $cookie->name eq "fakku_sid" ) { - $neededCookie = 1; - $logger->debug("Found fakku_sid"); - last; # Exit the loop if the cookie is found - } - } - - if ($neededCookie) { - $logger->debug("The needed cookie was found."); - } else { - $logger->debug("The needed cookie was not found."); - return ( error => "Not logged in to FAKKU! Set your FAKKU SID in the plugin settings page!" ); - } - } else { - $logger->debug("No Cookies were found!"); - return ( error => "Not logged in to FAKKU! Set your FAKKU SID in the plugin settings page!" ); - } - - my $fakku_URL = ""; - - # Looks for the "source:" in the existing tags. - my @oldTags = split( ',', $lrr_info->{existing_tags} ); - my $pattern = qr/^source:(.+)$/; - - foreach my $oldtag (@oldTags) { - if ( $oldtag =~ $pattern ) { - my $foundURL = $1; - if ( $foundURL =~ /fakku\.net/ ) { #Makes sure the found url is a fakku.net url. - $fakku_URL = $foundURL; - } - } + if ( !fakku_cookie_exists( $ua->cookie_jar ) ) { + die "Not logged in to FAKKU! Set your FAKKU SID in the plugin settings page!\n"; } # If the user specified a oneshot argument, use it as-is. # We could stand to pre-check it to see if it really is a FAKKU URL but meh - if ( $lrr_info->{oneshot_param} ) { - $fakku_URL = $lrr_info->{oneshot_param}; - } - if ( $fakku_URL eq "" ) { + my $fakku_URL = $lrr_info->{oneshot_param}; - # Search for a FAKKU URL if the user didn't specify one or none found in the tags. - $fakku_URL = search_for_fakku_url( $lrr_info->{archive_title}, $ua ); - } + # No URL? Looks for the "source:" in the existing tags. + $fakku_URL = get_url_from_tags( $lrr_info->{existing_tags} ) if ( !$fakku_URL ); + + # Still nothing? Search for a FAKKU URL using the title + $fakku_URL = search_for_fakku_url( $lrr_info->{archive_title}, $ua ) if ( !$fakku_URL ); # Do we have a URL to grab data from? - if ( $fakku_URL ne "" ) { - $logger->debug("Detected FAKKU URL: $fakku_URL"); - } else { - $logger->info("No matching FAKKU Gallery Found!"); - return ( error => "No matching FAKKU Gallery Found!" ); + if ( !$fakku_URL ) { + my $message = "No matching FAKKU Gallery Found!"; + $logger->info($message); + die "${message}\n"; } + $logger->debug("Detected FAKKU URL: $fakku_URL"); - my ( $newtags, $newtitle, $newSummary ); - eval { ( $newtags, $newtitle, $newSummary ) = get_tags_from_fakku( $fakku_URL, $ua, $add_source ); }; - - if ($@) { - return ( error => $@ ); - } + my ( $newtags, $newtitle, $newSummary ) = get_tags_from_fakku( $fakku_URL, $ua, $add_source ); $logger->info("Sending the following tags to LRR: $newtags"); @@ -305,4 +261,46 @@ sub get_tags_from_fakku { } +sub fakku_cookie_exists { + my ($cookie_jar) = @_; + + my $logger = get_plugin_logger(); + $logger->debug("Checking Cookies"); + + my $cookies = $cookie_jar->all; + if (@$cookies) { + + for my $cookie (@$cookies) { + if ( $cookie->name eq "fakku_sid" ) { + $logger->debug("The needed cookie was found."); + return 1; + } + } + + $logger->debug("The needed cookie was not found."); + + } else { + $logger->debug("No Cookies were found!"); + } + return; +} + +sub get_url_from_tags { + my ($actual_tags) = @_; + if ($actual_tags) { + my @tags = split( ',', $actual_tags ); + my $pattern = qr/^source:(.+)$/; + + foreach my $tag (@tags) { + if ( $tag =~ $pattern ) { + my $foundURL = $1; + if ( $foundURL =~ /fakku\.net/ ) { #Makes sure the found url is a fakku.net url. + return $foundURL; + } + } + } + } + return; +} + 1; diff --git a/lib/LANraragi/Plugin/Metadata/HDoujin.pm b/lib/LANraragi/Plugin/Metadata/HDoujin.pm index 378058276..278290be7 100644 --- a/lib/LANraragi/Plugin/Metadata/HDoujin.pm +++ b/lib/LANraragi/Plugin/Metadata/HDoujin.pm @@ -11,7 +11,7 @@ use Mojo::JSON qw(from_json); use LANraragi::Model::Plugins; use LANraragi::Utils::Archive qw(is_file_in_archive extract_file_from_archive); use LANraragi::Utils::Logging qw(get_plugin_logger); -use LANraragi::Utils::String qw(trim_url); +use LANraragi::Utils::String qw(trim_url); # Meta-information about your plugin. sub plugin_info { @@ -24,7 +24,7 @@ sub plugin_info { author => "Pao, Squidy", version => "0.7", description => "Collects metadata embedded into your archives by HDoujin Downloader's JSON or TXT files.", - icon => + icon => "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAABmJLR0QA/wD/AP+gvaeTAAAACXBI\nWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4wYDFB0m9797jwAAAB1pVFh0Q29tbWVudAAAAAAAQ3Jl\nYXRlZCB3aXRoIEdJTVBkLmUHAAAEbklEQVQ4y1WUPW/TUBSGn3uvHdv5cBqSOrQJgQ4ghqhCAgQM\nIIRAjF2Y2JhA/Q0g8R9YmJAqNoZKTAwMSAwdQEQUypeQEBEkTdtUbdzYiW1sM1RY4m5Hunp1znmf\n94jnz5+nAGmakiQJu7u7KKWwbRspJWma0m63+fHjB9PpFM/z6Ha7FAoFDMNga2uLx48fkyQJ29vb\nyCRJSNMUz/PY2dnBtm0qlQpKKZIkIQgCer0eW1tbDIdDJpMJc3NzuK5Lt9tF13WWl5dJkoRyuYyU\nUrK3t0ccx9TrdQzD4F/HSilM08Q0TWzbplqtUqvVKBaLKKVoNpt8/vyZKIq4fv064/EY2ev1KBQK\n2LadCQkhEEJkteu6+L6P7/tMJhOm0ylKKarVKjdu3GA6nXL+/HmSJEHWajV0Xf9P7N8TQhDHMWEY\nIoRgOBzieR4At2/f5uTJk0RRRLFYZHZ2liNHjqBFUcRoNKJarSKlRAiRmfPr1y/SNMVxHI4dO8aF\nCxfI5/O4rotSirdv33L16lV+//7Nly9fUEqh5XI5dF0nTdPMaSEEtm3TaDSwLAvLstB1nd3dXUql\nEqZpYlkW6+vrdLtdHjx4wPb2NmEYHgpalkUQBBwcHLC2tsbx48cpFos4jkMQBIRhyGQyYTgcsrGx\nQavVot1uc+LECcbjMcPhkFKpRC6XQ0vTlDAMieOYQqGA4zhcu3YNwzDQdR3DMA4/ahpCCPL5fEbC\nvXv3WFlZ4c+fP7TbbZaWlpBRFGXjpmnK/Pw8QRAwnU6RUqJpGp7nMRqNcF0XwzCQUqKUolwus7y8\njO/7lMtlFhcX0YQQeJ6XMXfq1Cn29/epVCrouk4QBNi2TalUIoqizLg0TQEYjUbU63VmZmYOsdE0\nDd/3s5HH4zG6rtNsNrEsi0qlQqFQYH19nVevXjEej/8Tm0wmlMtlhBAMBgOkaZo0Gg329vbY2dkh\nCIJsZ0oplFK8efOGp0+fcvHiRfL5PAAHBweEYcj8/HxGydevX5FxHDMajajVanz69Ik4jkmSBF3X\n0TSNzc1N7t69S6vV4vXr10gp8X2f4XBIpVLJghDHMRsbG2jT6TRLxuLiIr1eDwBN09A0jYcPHyKE\n4OjRo8RxTBRF9Pt95ubmMud93+f79+80m03k/v4+UspDKDWNRqPBu3fvSNOUtbU16vU6ly5dwnEc\ncrkcrutimib5fD4zxzRNVldXWVpaQqysrKSdTofLly8zmUwoFAoIIfjXuW3bnD17NkuJlBLHcdA0\nDYAgCHj27BmO47C6uopM05RyucyLFy/QNA3XdRFCYBgGQRCwubnJhw8fGAwGANRqNTRNI0kSXr58\nyc2bN6nX64RhyP379xFPnjxJlVJIKTl37hydTocoiuh0OszOzmJZFv1+n8FgwJ07d7hy5Qrj8ZiP\nHz/S7/c5ffo0CwsL9Ho9ZmZmEI8ePUoNwyBJEs6cOcPCwgLfvn3j/fv35PN5bNtGKZUdjp8/f3Lr\n1q3svLVaLTzPI4oiLMviL7opJdyaltNwAAAAAElFTkSuQmCC", parameters => [] ); @@ -36,68 +36,66 @@ sub get_tags { shift; - my $lrr_info = shift; # Global info hash + my $lrr_info = shift; # Global info hash - my $logger = get_plugin_logger(); + my $logger = get_plugin_logger(); my $archive_file_path = $lrr_info->{file_path}; - my $path_in_archive = is_file_in_archive($archive_file_path, "info.json"); + my $path_in_archive = is_file_in_archive( $archive_file_path, "info.json" ); if ($path_in_archive) { - return get_tags_from_hdoujin_json_file($archive_file_path, $path_in_archive); + return get_tags_from_hdoujin_json_file( $archive_file_path, $path_in_archive ); } - $path_in_archive = is_file_in_archive($archive_file_path, "info.txt"); + $path_in_archive = is_file_in_archive( $archive_file_path, "info.txt" ); if ($path_in_archive) { - return get_tags_from_hdoujin_txt_file($archive_file_path, $path_in_archive); + return get_tags_from_hdoujin_txt_file( $archive_file_path, $path_in_archive ); } - return (error => "No HDoujin metadata files found in this archive!"); + die "No HDoujin metadata files found in this archive!\n"; } sub get_tags_from_hdoujin_txt_file { my $archive_file_path = $_[0]; - my $path_in_archive = $_[1]; + my $path_in_archive = $_[1]; my $logger = get_plugin_logger(); - my $file_path = extract_file_from_archive($archive_file_path, $path_in_archive); + my $file_path = extract_file_from_archive( $archive_file_path, $path_in_archive ); - open(my $file_handle, '<:encoding(UTF-8)', $file_path) - or return (error => "Could not open $file_path!"); + open( my $file_handle, '<:encoding(UTF-8)', $file_path ) + or die "Could not open $file_path!\n"; - my $title = ""; - my $tags = ""; + my $title = ""; + my $tags = ""; my $summary = ""; - while (my $line = <$file_handle>) { + while ( my $line = <$file_handle> ) { - if ($line =~ m/(?i)^(artist|author|circle|characters?|description|language|parody|series|tags|title|url): (.*)/) { + if ( $line =~ m/(?i)^(artist|author|circle|characters?|description|language|parody|series|tags|title|url): (.*)/ ) { my $namespace = normalize_namespace($1); - my $value = $2; + my $value = $2; $value =~ s/^\s+|\s+$//g; - if ($value eq "") { + if ( $value eq "" ) { next; } - if(lc($namespace) eq "source") { + if ( lc($namespace) eq "source" ) { $value = trim_url($value); } - if (lc($namespace) eq "description") { + if ( lc($namespace) eq "description" ) { $summary = $value; - } - elsif(lc($namespace) eq "title") { + } elsif ( lc($namespace) eq "title" ) { $title = $value; - } - else { - $tags = append_tags($tags, $namespace, $value); + } else { + $tags = append_tags( $tags, $namespace, $value ); } } @@ -106,30 +104,30 @@ sub get_tags_from_hdoujin_txt_file { unlink $file_path; - if ($tags eq "") { - return (error => "No tags were found in info.txt!"); + if ( $tags eq "" ) { + die "No tags were found in info.txt!\n"; } $logger->info("Sending the following tags to LRR: $tags"); - return (title => $title, tags => remove_duplicates($tags), summary => $summary); + return ( title => $title, tags => remove_duplicates($tags), summary => $summary ); } sub get_tags_from_hdoujin_json_file { my $archive_file_path = $_[0]; - my $path_in_archive = $_[1]; + my $path_in_archive = $_[1]; my $logger = get_plugin_logger(); - my $file_path = extract_file_from_archive($archive_file_path, $path_in_archive); - my $json_str = ""; + my $file_path = extract_file_from_archive( $archive_file_path, $path_in_archive ); + my $json_str = ""; - open(my $file_handle, '<:encoding(UTF-8)', $file_path) - or return (error => "Could not open $file_path!"); + open( my $file_handle, '<:encoding(UTF-8)', $file_path ) + or die "Could not open $file_path!\n"; - while (my $row = <$file_handle>) { + while ( my $row = <$file_handle> ) { chomp $row; $json_str .= $row; @@ -143,30 +141,30 @@ sub get_tags_from_hdoujin_json_file { # Note that fields may be encapsulated in an outer "manga_info" object if the user has enabled this. # Each field can contain either a single tag or an array of tags. - if (exists $json_hash->{"manga_info"}) { + if ( exists $json_hash->{"manga_info"} ) { $json_hash = $json_hash->{"manga_info"}; } - my $title = $json_hash->{"title"}; - my $tags = get_tags_from_hdoujin_json_file_hash($json_hash); + my $title = $json_hash->{"title"}; + my $tags = get_tags_from_hdoujin_json_file_hash($json_hash); my $summary = $json_hash->{"description"}; unlink $file_path; - if ($tags eq "") { - return (error => "No tags were found in info.json!"); + if ( $tags eq "" ) { + die "No tags were found in info.json!\n"; } $logger->info("Sending the following tags to LRR: $tags"); - return (title => $title, tags => remove_duplicates($tags), summary => $summary); + return ( title => $title, tags => remove_duplicates($tags), summary => $summary ); } sub get_tags_from_hdoujin_json_file_hash { my $json_obj = $_[0]; - my $tags = ""; + my $tags = ""; my $logger = get_plugin_logger(); @@ -175,41 +173,39 @@ sub get_tags_from_hdoujin_json_file_hash { foreach my $key (@filtered_keys) { my $namespace = normalize_namespace($key); - my $values = $json_obj->{$key}; + my $values = $json_obj->{$key}; - if(lc($namespace) eq "source") { + if ( lc($namespace) eq "source" ) { $values = trim_url($values); } - if (ref($values) eq 'ARRAY') { + if ( ref($values) eq 'ARRAY' ) { # We have an array of values (e.g. author, artist, language, and character fields). foreach my $tag (@$values) { - $tags = append_tags($tags, $namespace, $tag); + $tags = append_tags( $tags, $namespace, $tag ); } - } - elsif (ref($values) eq 'HASH') { + } elsif ( ref($values) eq 'HASH' ) { # We have a map of keyed values (e.g. tags with namespace arrays enabled). - foreach my $nestedNamespace (keys(%$values)) { + foreach my $nestedNamespace ( keys(%$values) ) { my $nestedValues = $values->{$nestedNamespace}; foreach my $tag (@$nestedValues) { - $tags = append_tags($tags, normalize_namespace($nestedNamespace), $tag); + $tags = append_tags( $tags, normalize_namespace($nestedNamespace), $tag ); } } - } - else { + } else { # We have a basic string value (e.g. series). - $tags = append_tags($tags, $namespace, $values); + $tags = append_tags( $tags, $namespace, $values ); } @@ -221,21 +217,21 @@ sub get_tags_from_hdoujin_json_file_hash { sub append_tags { - my $tags = $_[0]; + my $tags = $_[0]; my $namespace = $_[1]; - my $append = $_[2]; + my $append = $_[2]; - my @split_tags = split(/,/, $append); + my @split_tags = split( /,/, $append ); for my $tag (@split_tags) { $tag =~ s/^\s+|\s+$//g; - if ($tag eq "") { + if ( $tag eq "" ) { next; } - if ($namespace ne "") { + if ( $namespace ne "" ) { $tag = lc($namespace) . ":" . $tag; } @@ -252,8 +248,8 @@ sub remove_duplicates { # The tags list may contain tags duplicated in fields, so it's likely to encounter duplicates. - my $tags = $_[0]; - my @split_tags = split(/,/, $tags); + my $tags = $_[0]; + my @split_tags = split( /,/, $tags ); my %seenTags; my @uniqueTags; @@ -262,30 +258,27 @@ sub remove_duplicates { $tag =~ s/^\s+|\s+$//g; - next if $seenTags{lc($tag)}++; + next if $seenTags{ lc($tag) }++; - push(@uniqueTags, $tag); + push( @uniqueTags, $tag ); } - return join(", ", @uniqueTags); + return join( ", ", @uniqueTags ); } sub normalize_namespace { - my $namespace = lc($_[0]); - - if ($namespace eq "characters") { + my $namespace = lc( $_[0] ); + + if ( $namespace eq "characters" ) { return "character"; - } - elsif ($namespace eq "misc") { + } elsif ( $namespace eq "misc" ) { return "other"; - } - elsif ($namespace eq "tags") { + } elsif ( $namespace eq "tags" ) { return ""; - } - elsif ($namespace eq "url") { + } elsif ( $namespace eq "url" ) { return "source"; } diff --git a/lib/LANraragi/Plugin/Metadata/HatH.pm b/lib/LANraragi/Plugin/Metadata/HatH.pm index 0132dabef..5704346e2 100644 --- a/lib/LANraragi/Plugin/Metadata/HatH.pm +++ b/lib/LANraragi/Plugin/Metadata/HatH.pm @@ -20,7 +20,7 @@ sub plugin_info { type => "metadata", namespace => "hentaiathome", author => "lily", - version => "0.1", + version => "0.2", description => "Collects metadata embedded into your archives by HentaiAtHome Downloader's galleryinfo txt files.", icon => "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAABmJLR0QA/wD/AP+gvaeTAAAACXBI\nWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4wYDFB0m9797jwAAAB1pVFh0Q29tbWVudAAAAAAAQ3Jl\nYXRlZCB3aXRoIEdJTVBkLmUHAAAEbklEQVQ4y1WUPW/TUBSGn3uvHdv5cBqSOrQJgQ4ghqhCAgQM\nIIRAjF2Y2JhA/Q0g8R9YmJAqNoZKTAwMSAwdQEQUypeQEBEkTdtUbdzYiW1sM1RY4m5Hunp1znmf\n94jnz5+nAGmakiQJu7u7KKWwbRspJWma0m63+fHjB9PpFM/z6Ha7FAoFDMNga2uLx48fkyQJ29vb\nyCRJSNMUz/PY2dnBtm0qlQpKKZIkIQgCer0eW1tbDIdDJpMJc3NzuK5Lt9tF13WWl5dJkoRyuYyU\nUrK3t0ccx9TrdQzD4F/HSilM08Q0TWzbplqtUqvVKBaLKKVoNpt8/vyZKIq4fv064/EY2ev1KBQK\n2LadCQkhEEJkteu6+L6P7/tMJhOm0ylKKarVKjdu3GA6nXL+/HmSJEHWajV0Xf9P7N8TQhDHMWEY\nIoRgOBzieR4At2/f5uTJk0RRRLFYZHZ2liNHjqBFUcRoNKJarSKlRAiRmfPr1y/SNMVxHI4dO8aF\nCxfI5/O4rotSirdv33L16lV+//7Nly9fUEqh5XI5dF0nTdPMaSEEtm3TaDSwLAvLstB1nd3dXUql\nEqZpYlkW6+vrdLtdHjx4wPb2NmEYHgpalkUQBBwcHLC2tsbx48cpFos4jkMQBIRhyGQyYTgcsrGx\nQavVot1uc+LECcbjMcPhkFKpRC6XQ0vTlDAMieOYQqGA4zhcu3YNwzDQdR3DMA4/ahpCCPL5fEbC\nvXv3WFlZ4c+fP7TbbZaWlpBRFGXjpmnK/Pw8QRAwnU6RUqJpGp7nMRqNcF0XwzCQUqKUolwus7y8\njO/7lMtlFhcX0YQQeJ6XMXfq1Cn29/epVCrouk4QBNi2TalUIoqizLg0TQEYjUbU63VmZmYOsdE0\nDd/3s5HH4zG6rtNsNrEsi0qlQqFQYH19nVevXjEej/8Tm0wmlMtlhBAMBgOkaZo0Gg329vbY2dkh\nCIJsZ0oplFK8efOGp0+fcvHiRfL5PAAHBweEYcj8/HxGydevX5FxHDMajajVanz69Ik4jkmSBF3X\n0TSNzc1N7t69S6vV4vXr10gp8X2f4XBIpVLJghDHMRsbG2jT6TRLxuLiIr1eDwBN09A0jYcPHyKE\n4OjRo8RxTBRF9Pt95ubmMud93+f79+80m03k/v4+UspDKDWNRqPBu3fvSNOUtbU16vU6ly5dwnEc\ncrkcrutimib5fD4zxzRNVldXWVpaQqysrKSdTofLly8zmUwoFAoIIfjXuW3bnD17NkuJlBLHcdA0\nDYAgCHj27BmO47C6uopM05RyucyLFy/QNA3XdRFCYBgGQRCwubnJhw8fGAwGANRqNTRNI0kSXr58\nyc2bN6nX64RhyP379xFPnjxJlVJIKTl37hydTocoiuh0OszOzmJZFv1+n8FgwJ07d7hy5Qrj8ZiP\nHz/S7/c5ffo0CwsL9Ho9ZmZmEI8ePUoNwyBJEs6cOcPCwgLfvn3j/fv35PN5bNtGKZUdjp8/f3Lr\n1q3svLVaLTzPI4oiLMviL7opJdyaltNwAAAAAElFTkSuQmCC", @@ -39,44 +39,42 @@ sub get_tags { my $file = $lrr_info->{file_path}; my $path_in_archive = is_file_in_archive( $file, "galleryinfo.txt" ); - if ($path_in_archive) { - - # Extract galleryinfo.txt - my $filepath = extract_file_from_archive( $file, $path_in_archive ); - - # Open it - open( my $fh, '<:encoding(UTF-8)', $filepath ) - or return ( error => "Could not open $filepath!" ); - - my $tag = ""; - my $title = ""; - while ( my $line = <$fh> ) { - - # Check if the line starts with Title: - if ( $line =~ m/Title: (.*)/ ) { - $title = $1; - } - - # Check if the line starts with Uploaded By: - if ( $line =~ m/Uploaded By: (.*)/ ) { - $tag .= "uploader:$1, "; - } - - # Check if the line starts with Upload Time: - if ( $line =~ m/Upload Time: (.*)/ ) { - $tag .= "upload_time:$1, "; - } - - # Check if the line starts with TAGS: - if ( $line =~ m/Tags: (.*)/ ) { - $tag .= $1; - return ( tags => $tag, title => $title ); - } + + die "No galleryinfo.txt file found in this archive!\n" if ( !$path_in_archive ); + + # Extract galleryinfo.txt + my $filepath = extract_file_from_archive( $file, $path_in_archive ); + + # Open it + open( my $fh, '<:encoding(UTF-8)', $filepath ) + or die "Could not open $filepath!\n"; + + my $tag = ""; + my $title = ""; + while ( my $line = <$fh> ) { + + # Check if the line starts with Title: + if ( $line =~ m/Title: (.*)/ ) { + $title = $1; + } + + # Check if the line starts with Uploaded By: + if ( $line =~ m/Uploaded By: (.*)/ ) { + $tag .= "uploader:$1, "; + } + + # Check if the line starts with Upload Time: + if ( $line =~ m/Upload Time: (.*)/ ) { + $tag .= "upload_time:$1, "; + } + + # Check if the line starts with TAGS: + if ( $line =~ m/Tags: (.*)/ ) { + $tag .= $1; + return ( tags => $tag, title => $title ); } - return ( error => "No tags were found in galleryinfo.txt!" ); - } else { - return ( error => "No galleryinfo.txt file found in this archive!" ); } + die "No tags were found in galleryinfo.txt!\n"; } 1; diff --git a/lib/LANraragi/Plugin/Metadata/Hentag.pm b/lib/LANraragi/Plugin/Metadata/Hentag.pm index 51bb619a8..b2824e400 100644 --- a/lib/LANraragi/Plugin/Metadata/Hentag.pm +++ b/lib/LANraragi/Plugin/Metadata/Hentag.pm @@ -20,7 +20,7 @@ sub plugin_info { type => "metadata", namespace => "hentagplugin", author => "siliconfeces", - version => "0.2", + version => "0.3", description => "Parses Hentag info.json files embedded in archives. Achtung, no API calls!", parameters => [], icon => @@ -40,44 +40,41 @@ sub get_tags { my $path_in_archive = is_file_in_archive( $file, "info.json" ); - if ($path_in_archive) { + die "No hentag info.json file found in this archive!\n" if ( !$path_in_archive ); - #Extract info.json - my $filepath = extract_file_from_archive( $file, $path_in_archive ); + #Extract info.json + my $filepath = extract_file_from_archive( $file, $path_in_archive ); - #Open it - my $stringjson = ""; + #Open it + my $stringjson = ""; - open( my $fh, '<:encoding(UTF-8)', $filepath ) - or return ( error => "Could not open $filepath!" ); + open( my $fh, '<:encoding(UTF-8)', $filepath ) + or die "Could not open $filepath!\n"; - while ( my $row = <$fh> ) { - chomp $row; - $stringjson .= $row; - } + while ( my $row = <$fh> ) { + chomp $row; + $stringjson .= $row; + } - #Use Mojo::JSON to decode the string into a hash - my $hashjson = from_json $stringjson; + #Use Mojo::JSON to decode the string into a hash + my $hashjson = from_json $stringjson; - $logger->debug("Found and loaded the following JSON: $stringjson"); + $logger->debug("Found and loaded the following JSON: $stringjson"); - #Parse it - my ( $tags, $title ) = tags_from_hentag_json($hashjson); + #Parse it + my ( $tags, $title ) = tags_from_hentag_json($hashjson); - #Delete it - unlink $filepath; + #Delete it + unlink $filepath; - #Return tags - $logger->info("Sending the following tags to LRR: $tags"); - if ($title) { - $logger->info("Parsed title is $title"); - return ( tags => $tags, title => $title ); - } elsif ( $tags ne "" ) { - return ( tags => $tags ); - } + #Return tags + $logger->info("Sending the following tags to LRR: $tags"); + if ($title) { + $logger->info("Parsed title is $title"); + return ( tags => $tags, title => $title ); + } elsif ( $tags ne "" ) { + return ( tags => $tags ); } - - return ( error => "No hentag info.json file found in this archive!" ); } #tags_from_hentag_json(decodedjson) diff --git a/lib/LANraragi/Plugin/Metadata/HentagOnline.pm b/lib/LANraragi/Plugin/Metadata/HentagOnline.pm index 1198a0d80..fdc539a29 100644 --- a/lib/LANraragi/Plugin/Metadata/HentagOnline.pm +++ b/lib/LANraragi/Plugin/Metadata/HentagOnline.pm @@ -27,7 +27,7 @@ sub plugin_info { type => "metadata", namespace => "hentagonlineplugin", author => "siliconfeces", - version => "0.2", + version => "0.3", description => "Searches hentag.com for tags matching your archive", parameters => [ { type => "string", @@ -79,7 +79,7 @@ sub get_tags { $logger->info("Failed parsing URL $oneshot_param"); # Don't continue execution if the oneshot URL was invalid. Raise an error instead to avoid surprises - return ( error => "Did not recognize the URL, is it a proper vault URL?" ); + die "Did not recognize the URL, is it a proper vault URL?\n"; } } elsif ( my $hentag_source = get_existing_hentag_source_url(@existing_tags) ) { @@ -133,7 +133,7 @@ sub get_tags { } } - return ( error => "No matching Hentag Archive Found!" ); + die "No matching Hentag Archive Found!\n"; } # Returns the ID from a hentag URL, or undef if invalid. diff --git a/lib/LANraragi/Plugin/Metadata/Hitomi.pm b/lib/LANraragi/Plugin/Metadata/Hitomi.pm index 47bd9659a..77ee65428 100644 --- a/lib/LANraragi/Plugin/Metadata/Hitomi.pm +++ b/lib/LANraragi/Plugin/Metadata/Hitomi.pm @@ -23,8 +23,8 @@ sub plugin_info { type => "metadata", namespace => "hitomiplugin", author => "doublewelp", - version => "0.2", - description => "Searches Hitomi.la for tags matching your archive. + version => "0.3", + description => "Searches Hitomi.la for tags matching your archive.
Supports reading the ID from files formatted as \"{Id} Title\" (curly brackets optional)
This plugin will use the source: tag of the archive if it exists (ex.: source:https://hitomi.la/XXXXX/XXXXX).", parameters => [], @@ -62,19 +62,12 @@ sub get_tags { } # Did we detect a Hitomi gallery? - if ( defined $galleryID ) { - $logger->debug("Detected Hitomi gallery id is $galleryID"); - } else { - $logger->info("No matching Hitomi Gallery Found!"); - return ( error => "No matching Hitomi Gallery Found!" ); - } - - #If no tokens were found, return a hash containing an error message. - #LRR will display that error to the client. - if ( $galleryID eq "" ) { - $logger->info("No matching Hitomi Gallery Found!"); - return ( error => "No matching Hitomi Gallery Found!" ); + if ( !$galleryID ) { + my $message = "No matching Hitomi Gallery Found!"; + $logger->info($message); + die "${message}\n"; } + $logger->debug("Detected Hitomi gallery id is $galleryID"); my %hashdata = get_tags_from_Hitomi($galleryID); @@ -91,7 +84,7 @@ sub get_tags { sub get_gallery_id_from_title { my $file = shift; - my ( $title, $filepath, $suffix ) = fileparse( $file, qr/\.[^.]*/ ); + my ( $title, $filepath, $suffix ) = fileparse( $file, qr/\.[^.]*/ ); my $logger = get_plugin_logger(); @@ -222,9 +215,9 @@ sub get_tags_from_Hitomi { my $logger = get_plugin_logger(); my $json = get_js_from_hitomi($gID); - $logger->debug("Got fully formed JS from Hitomi"); if ($json) { + $logger->debug("Got fully formed JS from Hitomi"); my @tags = get_tags_from_taglist($json); push( @tags, "source:https://hitomi.la/galleries/$gID.html" ) if ( @tags > 0 ); $hashdata{tags} = join( ', ', @tags ); diff --git a/lib/LANraragi/Plugin/Metadata/Koromo.pm b/lib/LANraragi/Plugin/Metadata/Koromo.pm index c51acd541..85239f96b 100644 --- a/lib/LANraragi/Plugin/Metadata/Koromo.pm +++ b/lib/LANraragi/Plugin/Metadata/Koromo.pm @@ -21,7 +21,7 @@ sub plugin_info { type => "metadata", namespace => "koromoplugin", author => "CirnoT, Difegue", - version => "2.1", + version => "2.2", description => "Collects metadata embedded into your archives as Koromo-style Info.json files. ( {'Tags': [xxx] } syntax)", icon => "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABhGlDQ1BJQ0MgcHJvZmlsZQAAKJF9kT1Iw1AUhU9TpVoqDmYQcchQnSyIijhKFYtgobQVWnUweekfNDEkKS6OgmvBwZ/FqoOLs64OroIg+APi4uqk6CIl3pcUWsR44fE+zrvn8N59gNCoMs3qGgc03TbTibiUy69IoVeE0QsRAYgys4xkZiEL3/q6pz6quxjP8u/7s/rUgsWAgEQ8ywzTJl4nnt60Dc77xCIryyrxOfGYSRckfuS64vEb55LLAs8UzWx6jlgklkodrHQwK5sa8RRxVNV0yhdyHquctzhr1Rpr3ZO/MFLQlzNcpzWMBBaRRAoSFNRQQRU2YrTrpFhI03ncxz/k+lPkUshVASPHPDagQXb94H/we7ZWcXLCS4rEge4Xx/kYAUK7QLPuON/HjtM8AYLPwJXe9m80gJlP0uttLXoE9G8DF9dtTdkDLneAwSdDNmVXCtISikXg/Yy+KQ8M3ALhVW9urXOcPgBZmtXSDXBwCIyWKHvN5909nXP7t6c1vx8dzXKFeWpUawAAAAZiS0dEAOwAEABqpSa6lwAAAAlwSFlzAAAuIwAALiMBeKU/dgAAAAd0SU1FB+MKCRQBJSKMeg0AAAGVSURBVDjLpZMxa9tQFIXPeaiyhxiZzKFjBme1JFfYgYAe9Bd0yA8JIaQhkJLBP6T/wh3qpZYzm2I8dyilJJMTW7yTIVGRFasE8uAt93K+d+5991IS8Ybj1SVIer24ty8Jk2wyl5S/GkDSi+O4s9PaOYOQh91wSHK2DeLViVut1pmkTwAQtAPUQcz/xCRBEpKOg3ZwEnbDDklvK6AQ+77fds4tSJbBcM7Nm83GbhXiVcXj8fiHpO/WWgfgHAAkXYxGoy8k/UG/nzxDnsqRxF7cO0iS5AhAQxKLm6bpVZqmn8sxAI3kQ2KjKDqQ9GRFEqDNfpQcukrMkDRF3ADAJJvM1+v1n0G/n5D0AcBaew3gFMCFtfbyuVT/cHCYrFarX1mWLQCgsAWSXtgNO81mY/ed7380xpyUn3XOXefr/Ntyufw9vZn+LL7zn21J+fRmOru/f/hrjNmThFLOGWPeV8UvBklSTnIWdsNh0A4g6RiAI/n17vZuWBVvncQNSBAYEK5OvNGDbSMdRdE+AJdl2aJumfjWdX4EIwDvDt7UjSEAAAAASUVORK5CYII=", @@ -47,45 +47,40 @@ sub get_tags { $path_in_archive = is_file_in_archive( $file, "info.json" ); } - if ($path_in_archive) { + if ( !$path_in_archive ) { die "No koromo info.json file found in this archive!\n"; } - #Extract info.json - my $filepath = extract_file_from_archive( $file, $path_in_archive ); + #Extract info.json + my $filepath = extract_file_from_archive( $file, $path_in_archive ); - #Open it - my $stringjson = ""; + #Open it + my $stringjson = ""; - open( my $fh, '<:encoding(UTF-8)', $filepath ) - or return ( error => "Could not open $filepath!" ); + open( my $fh, '<:encoding(UTF-8)', $filepath ) + or die "Could not open $filepath!\n"; - while ( my $row = <$fh> ) { - chomp $row; - $stringjson .= $row; - } + while ( my $row = <$fh> ) { + chomp $row; + $stringjson .= $row; + } - #Use Mojo::JSON to decode the string into a hash - my $hashjson = from_json $stringjson; + #Use Mojo::JSON to decode the string into a hash + my $hashjson = from_json $stringjson; - $logger->debug("Found and loaded the following JSON: $stringjson"); + $logger->debug("Found and loaded the following JSON: $stringjson"); - #Parse it - my ( $tags, $title ) = tags_from_koromo_json($hashjson); + #Parse it + my ( $tags, $title ) = tags_from_koromo_json($hashjson); - #Delete it - unlink $filepath; - - #Return tags - $logger->info("Sending the following tags to LRR: $tags"); - if ($title) { - $logger->info("Parsed title is $title"); - return ( tags => $tags, title => $title ); - } else { - return ( tags => $tags ); - } + #Delete it + unlink $filepath; + #Return tags + $logger->info("Sending the following tags to LRR: $tags"); + if ($title) { + $logger->info("Parsed title is $title"); + return ( tags => $tags, title => $title ); } else { - - return ( error => "No koromo info.json file found in this archive!" ); + return ( tags => $tags ); } } diff --git a/lib/LANraragi/Plugin/Metadata/Ksk.pm b/lib/LANraragi/Plugin/Metadata/Ksk.pm index 071f128d6..a876cad77 100644 --- a/lib/LANraragi/Plugin/Metadata/Ksk.pm +++ b/lib/LANraragi/Plugin/Metadata/Ksk.pm @@ -16,7 +16,7 @@ sub plugin_info { type => "metadata", namespace => "kskyamlmeta", author => "siliconfeces, Nixis198", - version => "0.004", + version => "0.004.1", description => "Collects metadata embedded into your archives as koushoku.yaml/info.yaml files.", icon => "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAFiUAABYlAUlSJPAAAANkSURBVDhPJZJpU5NXGIbf/9Ev0g+MM7Udp9WWDsVOsRYQKEVZQ4BsZnt9sy9shgShBbTYaTVCKY1B1pBEQggGFOogKEvYdOoXfszVQ/rhmTkz59zXc9/PeaRO12163DZCbgc+8y06HTJ+h5UOp4xLvoXdoOFBf5Auu4LS3obc0oJDp8VhNtLlcyN1uRWcZj13vS5cBi1+mwWPYiLY6cYjG+lxKoR8LgHpw9BQz+OBAbS1tch6DR1uO1Kox4dWVcfdDg9uswGnVSc66wn47QJmwtreTEPFVZxCoKosJ3hbRmlpRt8kNEIrdfscNN+o4tfeHhz6VhHBgqG1nsHeDpxGDV6zDkWjIvxLH25tK2+WUkzcG8JrNdJ/x4803NuJrr4G7Y/X8+UWIl1TDUGfgsfUjl2nwm/WMjrUh72tEXXFNYoKP+b74ks4FQOStuEnVNVlWBtv8kBYcmhVBJwWLOo6vKY2fvbaSD0ZxdnWxKWCj1CVXiEyPIBVuAz6bUiySc0dj0zAbsZtaM1fRH4fwm/RMDYYYCP2lNnfBsn89ZghxcIjMfmxng5GQ92ExIwkj6Kn5UYF6uofhMUG2mvLycYi7GaTnKwvk0vH+XctzXE6weupCFvRCP9MjLMx+Tfdulak4s8KqSr5kppvLmNT3WRQWN5Oz7ObibObnmMnMSXECxwtxdidi7L+Z5jlP0bYnJnEKX5PUpeVshqdINzl475dZnN+kqPsIocrApCa5fVchP3kDAeLc3nQ1vQTNqcjbCZncbQ3It1XZLLhR7wUtTMZZWd2Ugj+f3yYjpFLzbC/OM1BZoHcygJ7KeFEuHu7lsJmViN5G+o4jsd5+fAhKyMjecDJUoK9xDTH4uG753E+bCxxtJpkX5xzmQS5FyniU2MYNCKCsbo8b/84GWf7aZSt2Wi+81kdPU+wPj1OOOAhIHbi3Yu0GGqS07evqCv7llCXA+n6VxcpKTzHwsgwH1bTvBf0g7NOwu7J6jPGQn4iQ4H8XPZErNPNdYIWPZfPn6OvUwDUlVe59vknfHe+gLGAn9PtNQ7XnpHLJjgUdQZ6vy4iCMDxaiq/D8WFBXx9oZCA+DFJI3agougiVV9cyEOqij6l32UkFr6Xz7yfibG3PM/eSoLs1Di2+loaS0uovFIkFlDhPxYUixj0Cgg3AAAAAElFTkSuQmCC", @@ -38,9 +38,7 @@ sub get_tags { $path_in_archive = is_file_in_archive( $file, "info.yaml" ); } - if ( !$path_in_archive ) { - return ( error => "No KSK metadata file found in archive" ); - } + if ( !$path_in_archive ) { die "No KSK metadata file found in archive\n"; } my $filepath = extract_file_from_archive( $file, $path_in_archive ); @@ -81,7 +79,7 @@ sub tags_from_ksk_yaml { handle_tag_yaml( "magazine:", $magazine, \@found_tags ); # Koharu-version tags. Uses namespaces, and keys are lowercase - if (!defined($title)) { + if ( !defined($title) ) { $title = $hash->{"title"}; } handle_tag_yaml( "", $hash->{"general"}, \@found_tags ); diff --git a/lib/LANraragi/Plugin/Metadata/MEMS.pm b/lib/LANraragi/Plugin/Metadata/MEMS.pm index 702c3f5d3..6ce55abcb 100644 --- a/lib/LANraragi/Plugin/Metadata/MEMS.pm +++ b/lib/LANraragi/Plugin/Metadata/MEMS.pm @@ -14,7 +14,7 @@ sub plugin_info { namespace => 'memsplugin', login_from => "ehlogin", author => 'Mayriad', - version => '1.1.1', + version => '1.2', description => 'Accurately retrieves metadata from e-hentai.org using the identifiers appeneded to the ' . 'filenames of archives downloaded by Mayriad\'s EH Master Script.', icon => 'data:image/png;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAQAAAAAAAAAAAAAA' @@ -75,7 +75,7 @@ sub get_tags { my $file_error = 'Skipping archive without connecting to EH, because the archive title does not have valid ' . 'gallery identifiers from Mayriad\'s EH Master Script.'; $logger->error($file_error); - return ( error => $file_error ); + die "${file_error}\n"; } } @@ -101,7 +101,7 @@ sub get_tags { } else { my $source_error = 'No matching EH gallery found. The archive title may have incorrect gallery identifiers.'; $logger->error($source_error); - return ( error => $source_error ); + die "${source_error}\n"; } } diff --git a/lib/LANraragi/Plugin/Metadata/Pixiv.pm b/lib/LANraragi/Plugin/Metadata/Pixiv.pm index 2effd65f6..cb9ec02e5 100644 --- a/lib/LANraragi/Plugin/Metadata/Pixiv.pm +++ b/lib/LANraragi/Plugin/Metadata/Pixiv.pm @@ -3,7 +3,7 @@ package LANraragi::Plugin::Metadata::Pixiv; use strict; use warnings; -# Plugins can freely use all Perl packages already installed on the system +# Plugins can freely use all Perl packages already installed on the system # Try however to restrain yourself to the ones already installed for LRR (see tools/cpanfile) to avoid extra installations by the end-user. use Mojo::DOM; use Mojo::JSON qw(decode_json); @@ -28,20 +28,25 @@ sub plugin_info { namespace => "pixivmetadata", login_from => "pixivlogin", author => "psilabs-dev", - version => "0.1", + version => "0.2", description => "Retrieve metadata of a Pixiv artwork by its artwork ID.
Supports ID extraction from these file formats: \"{Id} Title\" or \"pixiv_{Id} Title\".

Pixiv enforces a rate limit on API requests, and may suspend/ban your account for overuse. ", - icon => "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/2wBDAQMDAwQDBAgEBAgQCwkLEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBD/wAARCAAUABQDAREAAhEBAxEB/8QAGQAAAgMBAAAAAAAAAAAAAAAAAwYABAUH/8QAJBAAAgICAgICAgMAAAAAAAAAAQIDBAUGABESIQcxImETQVH/xAAZAQACAwEAAAAAAAAAAAAAAAADBgACCAX/xAAoEQABBAEDAgYDAQAAAAAAAAABAgMEEQAFITESUQYTFEFhkTJxocH/2gAMAwEAAhEDEQA/ANfRvi3MbpRvZxrlfG4jHQT2J7U35PIsKB5FhjHuRgpHf0B5DsjvmrNV15nTHERwkrcWQABwOo0Co8AE/smjQzIWkeHn9VbXIKghpAUSTyekWQkckgV2G4s4fXNI1LeMkuuarsuQizVgEUYsnTSKG5IB2Iw6SN/GzdevIEE+uxwc3VJmlteqltJLQ/IoUSUjvRAsD3rf4wkHSYOru+khOqDp/ELSAFHtYUaJ9rFfOJFmtPTsy07UTRTQO0ciMOirKeiD+wRzvIWl1IWg2DuP1i642ppZbWKINEfIzpnwOHt5LaaE14QVzqeV/OTyMcRaNQXIUE/0O+gT64q+LKbajuJTZ85virNE7b/6cbvB9uPSWlKpPkO83QsDfa/4MFoFHVNE2jH7tsm54u5Dhplu16OLaSaxbmT2ie0VY18gO2Yj19A8Jq7szVYi4EVhSS4OkqXQSkHk8kk1wB95XRmYWjzG9QlyEqDZ6glFlSiNwOAAL5JPHtiHn8vNsGdyOesoqS5G3LbdV+laRyxA/XvjBEjJhx0R07hAA+hWLU2SqbJckrFFair7N4ClksjjWkbHX7NUzRmKQwyshdD9qej7B/w8u6y09QdSDW4sXR74Np91iy0opsUaJFjtt7ZX4XBZOTJn/9k=", - #If your plugin uses/needs custom arguments, input their name here. - #This name will be displayed in plugin configuration next to an input box for global arguments, and in archive edition for one-shot arguments. + icon => + "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/2wBDAQMDAwQDBAgEBAgQCwkLEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBD/wAARCAAUABQDAREAAhEBAxEB/8QAGQAAAgMBAAAAAAAAAAAAAAAAAwYABAUH/8QAJBAAAgICAgICAgMAAAAAAAAAAQIDBAUGABESIQcxImETQVH/xAAZAQACAwEAAAAAAAAAAAAAAAADBgACCAX/xAAoEQABBAEDAgYDAQAAAAAAAAABAgMEEQAFITESUQYTFEFhkTJxocH/2gAMAwEAAhEDEQA/ANfRvi3MbpRvZxrlfG4jHQT2J7U35PIsKB5FhjHuRgpHf0B5DsjvmrNV15nTHERwkrcWQABwOo0Co8AE/smjQzIWkeHn9VbXIKghpAUSTyekWQkckgV2G4s4fXNI1LeMkuuarsuQizVgEUYsnTSKG5IB2Iw6SN/GzdevIEE+uxwc3VJmlteqltJLQ/IoUSUjvRAsD3rf4wkHSYOru+khOqDp/ELSAFHtYUaJ9rFfOJFmtPTsy07UTRTQO0ciMOirKeiD+wRzvIWl1IWg2DuP1i642ppZbWKINEfIzpnwOHt5LaaE14QVzqeV/OTyMcRaNQXIUE/0O+gT64q+LKbajuJTZ85virNE7b/6cbvB9uPSWlKpPkO83QsDfa/4MFoFHVNE2jH7tsm54u5Dhplu16OLaSaxbmT2ie0VY18gO2Yj19A8Jq7szVYi4EVhSS4OkqXQSkHk8kk1wB95XRmYWjzG9QlyEqDZ6glFlSiNwOAAL5JPHtiHn8vNsGdyOesoqS5G3LbdV+laRyxA/XvjBEjJhx0R07hAA+hWLU2SqbJckrFFair7N4ClksjjWkbHX7NUzRmKQwyshdD9qej7B/w8u6y09QdSDW4sXR74Np91iy0opsUaJFjtt7ZX4XBZOTJn/9k=", + +#If your plugin uses/needs custom arguments, input their name here. +#This name will be displayed in plugin configuration next to an input box for global arguments, and in archive edition for one-shot arguments. oneshot_arg => "Pixiv artwork URL or illustration ID (e.g. pixiv.net/en/artworks/123456 or 123456.)", parameters => [ - { type => 'string', desc => 'Comma-separated list of languages to support. Options: jp, en. Empty string defaults to original tags (jp) only.' } + { type => 'string', + desc => + 'Comma-separated list of languages to support. Options: jp, en. Empty string defaults to original tags (jp) only.' + } ], - cooldown => 1 + cooldown => 1 ); } @@ -50,24 +55,26 @@ sub plugin_info { sub get_tags { shift; - my $lrr_info = shift; # Global info hash, contains various metadata provided by LRR - my $ua = $lrr_info -> {user_agent}; - my $logger = get_plugin_logger(); - my ( $tag_languages_str ) = @_; + my $lrr_info = shift; # Global info hash, contains various metadata provided by LRR + my $ua = $lrr_info->{user_agent}; + my $logger = get_plugin_logger(); + my ($tag_languages_str) = @_; + + my $illust_id = find_illust_id($lrr_info); + if ( !$illust_id ) { + my $message = "Failed to extract Pixiv ID!"; + $logger->error($message); + die "${message}\n"; + } - my $illust_id = find_illust_id( $lrr_info ); - if ($illust_id ne '') { - $logger -> debug("Retrieved Pixiv illustration ID = $illust_id"); + $logger->debug("Retrieved Pixiv illustration ID = $illust_id"); - #Work your magic here - You can create subroutines below to organize the code better - my %metadata = get_metadata_from_illust_id( $illust_id, $ua , $tag_languages_str ); + #Work your magic here - You can create subroutines below to organize the code better + my %metadata = get_metadata_from_illust_id( $illust_id, $ua, $tag_languages_str ); - #Otherwise, return the tags you've harvested. - $logger -> info( "Sending the following tags to LRR: " . $metadata{tags} ); - return %metadata; - } else { - $logger -> error( "Failed to extract Pixiv ID!" ); - } + #Otherwise, return the tags you've harvested. + $logger->info( "Sending the following tags to LRR: " . $metadata{tags} ); + return %metadata; } @@ -77,17 +84,17 @@ sub get_tags { # convert formatted date to epoch time in seconds. sub _convert_epoch_seconds { - my ( $formattedDate ) = @_; + my ($formattedDate) = @_; - $formattedDate =~ s/(\+\d{2}:\d{2})$//; - my $epoch_seconds = Time::Piece -> strptime( $formattedDate, "%Y-%m-%dT%H:%M:%S" ) -> epoch; + $formattedDate =~ s/(\+\d{2}:\d{2})$//; + my $epoch_seconds = Time::Piece->strptime( $formattedDate, "%Y-%m-%dT%H:%M:%S" )->epoch; return $epoch_seconds; } # sanitize the text according to the search syntax: https://sugoi.gitbook.io/lanraragi/basic-operations/searching sub sanitize { - my ( $text ) = @_; + my ($text) = @_; my $sanitized_text = $text; # replace nonseparator characters with empty str. @@ -101,7 +108,7 @@ sub sanitize { if ( $sanitized_text ne $text ) { my $logger = get_plugin_logger(); - $logger -> info("\"$text\" was sanitized."); + $logger->info("\"$text\" was sanitized."); } return $sanitized_text; @@ -110,32 +117,35 @@ sub sanitize { sub find_illust_id { - my ( $lrr_info ) = @_; + my ($lrr_info) = @_; - my $oneshot_param = $lrr_info -> {"oneshot_param"}; - my $archive_title = $lrr_info -> {"archive_title"}; - my $logger = get_plugin_logger(); + my $oneshot_param = $lrr_info->{"oneshot_param"}; + my $archive_title = $lrr_info->{"archive_title"}; + my $logger = get_plugin_logger(); + + if ( defined $oneshot_param ) { - if (defined $oneshot_param) { # case 1: "$illust_id" i.e. string of digits. - if ($oneshot_param =~ /^\d+$/) { + if ( $oneshot_param =~ /^\d+$/ ) { return $oneshot_param; } + # case 2: URL-based embedding - if ($oneshot_param =~ m{.*pixiv\.net/.*artworks/(\d+)}) { + if ( $oneshot_param =~ m{.*pixiv\.net/.*artworks/(\d+)} ) { return $1; } } - if (defined $archive_title) { - # case 3: archive title extraction (strong pattern matching) - # use strong pattern matching if using multiple metadata plugins and archive title needs to exclusively call the pixiv plugin. - if ($archive_title =~ /pixiv_\{(\d*)\}.*$/) { + if ( defined $archive_title ) { + + # case 3: archive title extraction (strong pattern matching) + # use strong pattern matching if using multiple metadata plugins and archive title needs to exclusively call the pixiv plugin. + if ( $archive_title =~ /pixiv_\{(\d*)\}.*$/ ) { return $1; } # case 4: archive title extraction (weak pattern matching) - if ($archive_title =~ /^\{(\d*)\}.*$/) { + if ( $archive_title =~ /^\{(\d*)\}.*$/ ) { return $1; } } @@ -145,30 +155,29 @@ sub find_illust_id { } sub get_illustration_dto_from_json { + # retrieve relevant data obj from json obj my ( $json, $illust_id ) = @_; - return %{$json -> {'illust'} -> { $illust_id }}; + return %{ $json->{'illust'}->{$illust_id} }; } sub get_manga_data_from_dto { + # get manga-based data and return as an array. - my ( $dto ) = @_; + my ($dto) = @_; my @manga_data; - if ( exists $dto -> {"seriesNavData"} && defined $dto -> {"seriesNavData"} ) { - my %series_nav_data = %{ $dto -> {"seriesNavData"} }; + if ( exists $dto->{"seriesNavData"} && defined $dto->{"seriesNavData"} ) { + my %series_nav_data = %{ $dto->{"seriesNavData"} }; - my $series_id = $series_nav_data{"seriesId"}; + my $series_id = $series_nav_data{"seriesId"}; my $series_title = $series_nav_data{"title"}; my $series_order = $series_nav_data{"order"}; if ( defined $series_id && defined $series_title && defined $series_order ) { $series_title = sanitize($series_title); - push @manga_data, ( - "pixiv_series_id:$series_id", - "pixiv_series_title:$series_title", - "pixiv_series_order:$series_order", - ) + push @manga_data, + ( "pixiv_series_id:$series_id", "pixiv_series_title:$series_title", "pixiv_series_order:$series_order", ); } } @@ -185,31 +194,32 @@ sub get_pixiv_tags_from_dto { if ( $tag_languages_str eq "" ) { push @tag_languages, "jp"; } else { - @tag_languages = split(/,/, $tag_languages_str); + @tag_languages = split( /,/, $tag_languages_str ); for (@tag_languages) { s/^\s+//; s/\s+$//; } } - foreach my $item ( @{$dto -> {"tags"} -> {"tags"}} ) { - + foreach my $item ( @{ $dto->{"tags"}->{"tags"} } ) { + # iterate over tagging language. - foreach my $tag_language ( @tag_languages ) { + foreach my $tag_language (@tag_languages) { + + if ( $tag_language eq 'jp' ) { - if ($tag_language eq 'jp') { # add original/jp tags. - my $orig_tag = $item -> {"tag"}; - if (defined $orig_tag) { + my $orig_tag = $item->{"tag"}; + if ( defined $orig_tag ) { $orig_tag = sanitize($orig_tag); push @tags, $orig_tag; } - } - else { + } else { + # add translated tags. - my $translated_tag = $item -> {"translation"} -> { $tag_language }; - if (defined $translated_tag) { + my $translated_tag = $item->{"translation"}->{$tag_language}; + if ( defined $translated_tag ) { $translated_tag = sanitize($translated_tag); push @tags, $translated_tag; } @@ -221,9 +231,9 @@ sub get_pixiv_tags_from_dto { } sub get_user_id_from_dto { - my ( $dto ) = @_; + my ($dto) = @_; my @tags; - my $user_id = $dto -> {"userId"}; + my $user_id = $dto->{"userId"}; if ( defined $user_id ) { push @tags, "pixiv_user_id:$user_id"; @@ -233,9 +243,9 @@ sub get_user_id_from_dto { } sub get_artist_from_dto { - my ( $dto ) = @_; + my ($dto) = @_; my @tags; - my $user_name = $dto -> {"userName"}; + my $user_name = $dto->{"userName"}; if ( defined $user_name ) { $user_name = sanitize($user_name); @@ -246,10 +256,10 @@ sub get_artist_from_dto { } sub get_create_date_from_dto { - my ( $dto ) = @_; + my ($dto) = @_; my @tags; - my $formattedDate = $dto -> {"createDate"}; + my $formattedDate = $dto->{"createDate"}; my $epoch_seconds = _convert_epoch_seconds($formattedDate); if ( defined $epoch_seconds ) { push @tags, "date_created:$epoch_seconds"; @@ -258,10 +268,10 @@ sub get_create_date_from_dto { } sub get_upload_date_from_dto { - my ( $dto ) = @_; + my ($dto) = @_; my @tags; - my $formattedDate = $dto -> {"uploadDate"}; + my $formattedDate = $dto->{"uploadDate"}; my $epoch_seconds = _convert_epoch_seconds($formattedDate); if ( defined $epoch_seconds ) { push @tags, "date_uploaded:$epoch_seconds"; @@ -276,39 +286,39 @@ sub get_hash_metadata_from_json { my %hashdata; # get illustration metadata. - my %illust_dto = get_illustration_dto_from_json($json, $illust_id); + my %illust_dto = get_illustration_dto_from_json( $json, $illust_id ); my @lrr_tags; my @manga_data = get_manga_data_from_dto( \%illust_dto ); my @pixiv_tags = get_pixiv_tags_from_dto( \%illust_dto, $tag_languages_str ); - push (@lrr_tags, @manga_data); - push (@lrr_tags, @pixiv_tags); + push( @lrr_tags, @manga_data ); + push( @lrr_tags, @pixiv_tags ); # add source my $source = "https://pixiv.net/artworks/$illust_id"; push @lrr_tags, "source:$source"; # add general metadata. - my @user_id_data = get_user_id_from_dto( \%illust_dto ); + my @user_id_data = get_user_id_from_dto( \%illust_dto ); my @user_name_data = get_artist_from_dto( \%illust_dto ); - push (@lrr_tags, @user_id_data); - push (@lrr_tags, @user_name_data); + push( @lrr_tags, @user_id_data ); + push( @lrr_tags, @user_name_data ); # add time-based metadata. my @create_date_epoch_data = get_create_date_from_dto( \%illust_dto ); my @upload_date_epoch_data = get_upload_date_from_dto( \%illust_dto ); - push (@lrr_tags, @create_date_epoch_data); - push (@lrr_tags, @upload_date_epoch_data); + push( @lrr_tags, @create_date_epoch_data ); + push( @lrr_tags, @upload_date_epoch_data ); $hashdata{tags} = join( ', ', @lrr_tags ); # change title. my $illust_title = $illust_dto{"illustTitle"}; - if (defined $illust_title) { + if ( defined $illust_title ) { $illust_title = sanitize($illust_title); $hashdata{title} = $illust_title; } else { - $logger -> error("Failed to extract illustration title from json file: " . Dumper($json)); + $logger->error( "Failed to extract illustration title from json file: " . Dumper($json) ); } return %hashdata; @@ -317,14 +327,14 @@ sub get_hash_metadata_from_json { sub get_json_from_html { - my ( $html ) = @_; + my ($html) = @_; my $logger = get_plugin_logger(); # get 'content' body. - my $dom = Mojo::DOM -> new($html); - my $jsonstring = $dom -> at('meta#meta-preload-data') -> attr('content'); - - $logger -> debug("Tentative JSON: $jsonstring"); + my $dom = Mojo::DOM->new($html); + my $jsonstring = $dom->at('meta#meta-preload-data')->attr('content'); + + $logger->debug("Tentative JSON: $jsonstring"); my $json = decode_json $jsonstring; return $json; @@ -340,35 +350,31 @@ sub get_html_from_illust_id { while (1) { - my $res = $ua -> get ( - $URL => { - Referer => "https://www.pixiv.net" - } - ) -> result; - my $code = $res -> code; - $logger -> debug("Received code $code."); + my $res = $ua->get( $URL => { Referer => "https://www.pixiv.net" } )->result; + my $code = $res->code; + $logger->debug("Received code $code."); # handle 3xx. if ( $code == 301 ) { - $URL = $res -> headers -> location; - $logger -> debug("Redirecting to $URL"); + $URL = $res->headers->location; + $logger->debug("Redirecting to $URL"); next; } if ( $code == 302 ) { - my $location = $res -> headers -> location; + my $location = $res->headers->location; $URL = "pixiv.net$location"; - $logger -> debug("Redirecting to $URL"); + $logger->debug("Redirecting to $URL"); next; } # handle 4xx. - if ( $res -> is_error ) { - my $code = $res -> code; + if ( $res->is_error ) { + my $code = $res->code; return "error ($code) "; } # handle 2xx. - return $res -> body; + return $res->body; } @@ -384,10 +390,10 @@ sub get_metadata_from_illust_id { my $html = get_html_from_illust_id( $illust_id, $ua ); if ( $html =~ /^error/ ) { - return ( error => "Error retrieving HTML from Pixiv Illustration: $html"); + die "Error retrieving HTML from Pixiv Illustration: $html\n"; } - my $json = get_json_from_html( $html ); + my $json = get_json_from_html($html); if ($json) { %hashdata = get_hash_metadata_from_json( $json, $illust_id, $tag_languages_str ); } diff --git a/lib/LANraragi/Plugin/Metadata/nHentai.pm b/lib/LANraragi/Plugin/Metadata/nHentai.pm index eef510d14..ecf99ca6c 100644 --- a/lib/LANraragi/Plugin/Metadata/nHentai.pm +++ b/lib/LANraragi/Plugin/Metadata/nHentai.pm @@ -24,8 +24,8 @@ sub plugin_info { namespace => "nhplugin", login_from => "nhentaicfbypass", author => "Difegue and others", - version => "1.8.0", - description => "Searches nHentai for tags matching your archive. + version => "1.9", + description => "Searches nHentai for tags matching your archive.
Supports reading the ID from files formatted as \"{Id} Title\" and if not, tries to search for a matching gallery.
This plugin will use the source: tag of the archive if it exists.", icon => @@ -58,25 +58,20 @@ sub get_tags { $galleryID = $1; $logger->debug("Skipping search and using gallery $galleryID from source tag"); } else { + $logger->debug("Searching gallery by title (filename)"); #Get Gallery ID by hand if the user didn't specify a URL $galleryID = get_gallery_id_from_title( $lrr_info->{file_path}, $ua ); } # Did we detect a nHentai gallery? - if ( defined $galleryID ) { - $logger->debug("Detected nHentai gallery id is $galleryID"); - } else { - $logger->info("No matching nHentai Gallery Found!"); - return ( error => "No matching nHentai Gallery Found!" ); + if ( !$galleryID ) { + my $message = "No matching nHentai Gallery Found!"; + $logger->info($message); + die "${message}\n"; } - #If no tokens were found, return a hash containing an error message. - #LRR will display that error to the client. - if ( $galleryID eq "" ) { - $logger->info("No matching nHentai Gallery Found!"); - return ( error => "No matching nHentai Gallery Found!" ); - } + $logger->debug("Detected nHentai gallery ID is $galleryID"); my %hashdata = get_tags_from_NH( $galleryID, $ua ); @@ -108,7 +103,8 @@ sub get_gallery_dom_by_title { $logger->debug( "Got response " . $res->body ); if ( $res->is_error ) { - return; + my $code = $res->code; + die "Search gallery by title failed! (Code: $code)\n"; } return $res->dom; @@ -117,7 +113,7 @@ sub get_gallery_dom_by_title { sub get_gallery_id_from_title { my ( $file, $ua ) = @_; - my ( $title, $filepath, $suffix ) = fileparse( $file, qr/\.[^.]*/ ); + my ( $title, $filepath, $suffix ) = fileparse( $file, qr/\.[^.]*/ ); my $logger = get_plugin_logger(); @@ -156,7 +152,7 @@ sub get_html_from_NH { if ( $res->is_error ) { my $code = $res->code; - return "error ($code)"; + die "Error retrieving gallery from nHentai! (Code: $code)\n"; } return $res->body; @@ -220,12 +216,6 @@ sub get_tags_from_NH { my %hashdata = ( tags => "" ); my $html = get_html_from_NH( $gID, $ua ); - - # If the string starts with "error", we couldn't retrieve data from NH. - if ( $html =~ /^error/ ) { - return ( error => "Error retrieving gallery from nHentai! ($html)" ); - } - my $json = get_json_from_html($html); if ($json) { diff --git a/lib/LANraragi/Utils/Plugins.pm b/lib/LANraragi/Utils/Plugins.pm index 69f4509cd..6fa470f55 100644 --- a/lib/LANraragi/Utils/Plugins.pm +++ b/lib/LANraragi/Utils/Plugins.pm @@ -4,9 +4,9 @@ use strict; use warnings; use utf8; -use Mojo::JSON qw(decode_json); +use Mojo::JSON qw(decode_json); use LANraragi::Utils::Database qw(redis_decode); -use LANraragi::Utils::Logging qw(get_logger); +use LANraragi::Utils::Logging qw(get_logger); # Plugin system ahoy - this makes the LANraragi::Utils::Plugins::plugins method available # Don't call this method directly - Rely on LANraragi::Utils::Plugins::get_plugins instead @@ -95,7 +95,7 @@ sub get_plugin { return 0; } -# Get the parameters for thespecified plugin, either default values or input by the user in the settings page. +# Get the parameters for the specified plugin, either default values or input by the user in the settings page. # Returns an array of values. sub get_plugin_parameters { @@ -162,15 +162,9 @@ sub use_plugin { # Execute the plugin, appending the custom args at the end if ( $pluginfo{type} eq "script" ) { - eval { %plugin_result = LANraragi::Model::Plugins::exec_script_plugin( $plugin, $input, @settings ); }; - } - - if ( $pluginfo{type} eq "metadata" ) { - eval { %plugin_result = LANraragi::Model::Plugins::exec_metadata_plugin( $plugin, $id, $input, @settings ); }; - } - - if ($@) { - $plugin_result{error} = $@; + %plugin_result = LANraragi::Model::Plugins::exec_script_plugin( $plugin, $input, @settings ); + } elsif ( $pluginfo{type} eq "metadata" ) { + %plugin_result = LANraragi::Model::Plugins::exec_metadata_plugin( $plugin, $id, $input, @settings ); } # Decode the error value if there's one to avoid garbled characters diff --git a/tests/LANraragi/Model/Plugins.t b/tests/LANraragi/Model/Plugins.t new file mode 100644 index 000000000..5b4712f85 --- /dev/null +++ b/tests/LANraragi/Model/Plugins.t @@ -0,0 +1,131 @@ +use strict; +use warnings; +use utf8; +use Data::Dumper; + +use Cwd qw( getcwd ); + +use Test::More; +use Test::Deep; +use Test::Trap; +use Test::MockObject; + +my $cwd = getcwd(); +require "$cwd/tests/mocks.pl"; + +my $PKG = 'LANraragi::Model::Plugins'; + +require_ok($PKG); +use_ok($PKG); + +note('calling exec_metadata_plugin without providing an ID'); +{ + no warnings 'once', 'redefine'; + local *LANraragi::Model::Plugins::get_logger = sub { return get_logger_mock() }; + + my %rdata = LANraragi::Model::Plugins::exec_metadata_plugin( undef, undef, undef, undef ); + + cmp_deeply( \%rdata, { 'error' => re('without providing an id') }, 'returned error' ); + + %rdata = LANraragi::Model::Plugins::exec_metadata_plugin( undef, 0, undef, undef ); + + cmp_deeply( \%rdata, { 'error' => re('without providing an id') }, 'returned error' ); + + %rdata = LANraragi::Model::Plugins::exec_metadata_plugin( undef, '', undef, undef ); + + cmp_deeply( \%rdata, { 'error' => re('without providing an id') }, 'returned error' ); +} + +note('calling exec_metadata_plugin with a plugin without get_tags sub'); +{ + no warnings 'once', 'redefine'; + local *LANraragi::Model::Plugins::get_logger = sub { return get_logger_mock() }; + + my $plugin_mock = Test::MockObject->new(); + + my %rdata = LANraragi::Model::Plugins::exec_metadata_plugin( $plugin_mock, 'dummy', undef, undef ); + + cmp_deeply( \%rdata, { 'error' => re('get_tags') }, 'returned error' ); + +} + +note('exec_metadata_plugin doesn\'t die when get_tags fails'); +{ + my $plugin_mock = Test::MockObject->new(); + $plugin_mock->mock( 'plugin_info' => sub { return (); } ); + $plugin_mock->mock( 'get_tags' => sub { die "Ooops!\n"; } ); + + my $redis_mock = Test::MockObject->new(); + $redis_mock->mock( 'hgetall' => sub { return ( 'thumbhash' => 'dummy' ); } ); + $redis_mock->mock( 'quit' => sub { return 1; } ); + + no warnings 'once', 'redefine'; + local *LANraragi::Model::Plugins::get_logger = sub { return get_logger_mock() }; + local *LANraragi::Model::Config::get_redis = sub { return $redis_mock; }; + local *LANraragi::Model::Config::enable_tagrules = sub { return; }; + + # Act + my %rdata = LANraragi::Model::Plugins::exec_metadata_plugin( $plugin_mock, 'dummy', undef, undef ); + + cmp_deeply( \%rdata, { 'error' => re('Ooops!') }, 'returned error' ); +} + +note('exec_metadata_plugin returns the tags'); +{ + my $plugin_mock = Test::MockObject->new(); + $plugin_mock->mock( 'plugin_info' => sub { return (); } ); + $plugin_mock->mock( 'get_tags' => sub { return ( tags => 'tag1,tag2' ); } ); + + my $redis_mock = Test::MockObject->new(); + $redis_mock->mock( 'hgetall' => sub { return ( 'thumbhash' => 'dummy', 'tags' => '' ); } ); + $redis_mock->mock( 'quit' => sub { return 1; } ); + + no warnings 'once', 'redefine'; + local *LANraragi::Model::Plugins::get_logger = sub { return get_logger_mock() }; + local *LANraragi::Model::Config::get_redis = sub { return $redis_mock; }; + local *LANraragi::Model::Config::enable_tagrules = sub { return; }; + + # Act + my %rdata = LANraragi::Model::Plugins::exec_metadata_plugin( $plugin_mock, 'dummy', undef, undef ); + + cmp_deeply( \%rdata, { 'new_tags' => ' tag1, tag2' }, 'returned tags' ); +} + +note('exec_metadata_plugin returns the tags and the title'); +{ + my $plugin_mock = Test::MockObject->new(); + $plugin_mock->mock( 'plugin_info' => sub { return (); } ); + $plugin_mock->mock( 'get_tags' => sub { return ( tags => 'tag1,tag2', title => ' The Best Manga ' ); } ); + + my $redis_mock = Test::MockObject->new(); + $redis_mock->mock( 'hgetall' => sub { return ( 'thumbhash' => 'dummy', 'tags' => '' ); } ); + $redis_mock->mock( 'quit' => sub { return 1; } ); + + no warnings 'once', 'redefine'; + local *LANraragi::Model::Plugins::get_logger = sub { return get_logger_mock() }; + local *LANraragi::Model::Config::get_redis = sub { return $redis_mock; }; + local *LANraragi::Model::Config::enable_tagrules = sub { return; }; + local *LANraragi::Model::Config::can_replacetitles = sub { return 1; }; + + # Act + my %rdata = LANraragi::Model::Plugins::exec_metadata_plugin( $plugin_mock, 'dummy', undef, undef ); + + cmp_deeply( \%rdata, { 'new_tags' => ' tag1, tag2', title => 'The Best Manga' }, 'returned tags' ); +} + +note('exec_script_plugin doesn\'t die when run_script fails'); +{ + my $plugin_mock = Test::MockObject->new(); + $plugin_mock->mock( 'plugin_info' => sub { return (); } ); + $plugin_mock->mock( 'run_script' => sub { die "Ooops!\n"; } ); + + no warnings 'once', 'redefine'; + local *LANraragi::Model::Plugins::exec_login_plugin = sub { return; }; + + # Act + my %rdata = LANraragi::Model::Plugins::exec_script_plugin( $plugin_mock, 'dummy', undef ); + + cmp_deeply( \%rdata, { 'error' => re('Ooops!') }, 'returned error' ); +} + +done_testing(); \ No newline at end of file diff --git a/tests/LANraragi/Plugin/Metadata/CopyArchiveTags.t b/tests/LANraragi/Plugin/Metadata/CopyArchiveTags.t index 1975898fe..55640ba60 100644 --- a/tests/LANraragi/Plugin/Metadata/CopyArchiveTags.t +++ b/tests/LANraragi/Plugin/Metadata/CopyArchiveTags.t @@ -12,74 +12,90 @@ use Test::Trap; my $cwd = getcwd(); require "$cwd/tests/mocks.pl"; +require_ok('LANraragi::Plugin::Metadata::CopyArchiveTags'); use_ok('LANraragi::Plugin::Metadata::CopyArchiveTags'); +my @log_messages; +no warnings 'once', 'redefine'; +local *LANraragi::Plugin::Metadata::CopyArchiveTags::get_plugin_logger = sub { return get_logger_mock( \@log_messages ) }; + sub _random_archive_id { my $rnd = ''; $rnd .= sprintf( "%x", rand 16 ) for 1 .. 40; return $rnd; } -note('testing that get_tags doesn\'t die ...'); +note('get_tags when archive tags is undef returns an empty tag list ...'); { - - my $lrr_info = { 'oneshot_param' => _random_archive_id() }; - my @log_messages; - my $log_mock = get_logger_mock( \@log_messages ); + @log_messages = (); + my $archive_id = _random_archive_id(); + my $lrr_info = { 'oneshot_param' => $archive_id, 'archive_id' => 'dummy' }; no warnings 'once', 'redefine'; - local *LANraragi::Plugin::Metadata::CopyArchiveTags::get_plugin_logger = sub { return $log_mock }; - local *LANraragi::Plugin::Metadata::CopyArchiveTags::internal_get_tags = sub { die "Eep!\n"; }; + local *LANraragi::Utils::Database::get_tags = sub { return; }; # Act - my @error = LANraragi::Plugin::Metadata::CopyArchiveTags::get_tags( 'dummy', $lrr_info, 'dummy', 0, 'dummy' ); + my @rdata = LANraragi::Plugin::Metadata::CopyArchiveTags::get_tags( 'dummy', $lrr_info, 0 ); - cmp_deeply( \@error, [ 'error', "Eep!\n" ], 'returned error' ); + cmp_deeply( \@rdata, [ tags => '' ], 'returned data' ); - cmp_deeply( \@log_messages, [ [ 'error', $log_mock, "Eep!\n" ] ], 'log messages' ); + cmp_deeply( + \@log_messages, + [ [ 'info', ignore, "Copying tags from archive \"${archive_id}\"" ], + [ 'info', ignore, 'Sending the following tags to LRR: -' ] + ], + 'log messages' + ); } -note('testing get_tags returning an empty tag list ...'); +note('get_tags when archive tags is empty returns an empty tag list ...'); { - - my $lrr_info = { 'oneshot_param' => _random_archive_id() }; - my @log_messages; + @log_messages = (); + my $archive_id = _random_archive_id(); + my $lrr_info = { 'oneshot_param' => $archive_id, 'archive_id' => 'dummy' }; no warnings 'once', 'redefine'; - local *LANraragi::Plugin::Metadata::CopyArchiveTags::get_plugin_logger = sub { return get_logger_mock( \@log_messages ) }; - local *LANraragi::Plugin::Metadata::CopyArchiveTags::internal_get_tags = sub { return (); }; - local *LANraragi::Plugin::Metadata::CopyArchiveTags::read_params = sub { return {}; }; + local *LANraragi::Utils::Database::get_tags = sub { return ''; }; # Act - my @rdata = LANraragi::Plugin::Metadata::CopyArchiveTags::get_tags( 'dummy', $lrr_info, 'dummy', 0, 'dummy' ); + my @rdata = LANraragi::Plugin::Metadata::CopyArchiveTags::get_tags( 'dummy', $lrr_info, 0 ); - cmp_deeply( \@rdata, [], 'returned data' ); - - cmp_deeply( \@log_messages, [ [ 'info', ignore, 'Sending the following tags to LRR: -' ] ], 'log messages' ); + cmp_deeply( \@rdata, [ tags => '' ], 'returned data' ); + cmp_deeply( + \@log_messages, + [ [ 'info', ignore, "Copying tags from archive \"${archive_id}\"" ], + [ 'info', ignore, 'Sending the following tags to LRR: -' ] + ], + 'log messages' + ); } -note('testing get_tags returning a list of tags ...'); +note('get_tags when archive has tags returns the list of tags ...'); { - - my $lrr_info = { 'oneshot_param' => _random_archive_id() }; - my @log_messages; + @log_messages = (); + my $archive_id = _random_archive_id(); + my $lrr_info = { 'oneshot_param' => $archive_id, 'archive_id' => 'dummy' }; no warnings 'once', 'redefine'; - local *LANraragi::Plugin::Metadata::CopyArchiveTags::get_plugin_logger = sub { return get_logger_mock( \@log_messages ) }; - local *LANraragi::Plugin::Metadata::CopyArchiveTags::internal_get_tags = sub { return ( 'tags' => 'one, two' ); }; - local *LANraragi::Plugin::Metadata::CopyArchiveTags::read_params = sub { return {}; }; + local *LANraragi::Utils::Database::get_tags = sub { return ( 'tags' => 'one, two' ); }; # Act - my @rdata = LANraragi::Plugin::Metadata::CopyArchiveTags::get_tags( 'dummy', $lrr_info, 'dummy', 0, 'dummy' ); + my @rdata = LANraragi::Plugin::Metadata::CopyArchiveTags::get_tags( 'dummy', $lrr_info, 0 ); - cmp_deeply( \@rdata, [ tags => 'one, two' ], 'returned data' ); + cmp_deeply( \@rdata, [ tags => 'one,two' ], 'returned data' ); - cmp_deeply( \@log_messages, [ [ 'info', ignore, 'Sending the following tags to LRR: one, two' ] ], 'log messages' ); + cmp_deeply( + \@log_messages, + [ [ 'info', ignore, "Copying tags from archive \"${archive_id}\"" ], + [ 'info', ignore, 'Sending the following tags to LRR: one,two' ] + ], + 'log messages' + ); } -note('extract_archive_id returns undef if param doesn\'t contain a valid archive ID ...'); +note('extract_archive_id when param doesn\'t contain a valid archive ID returns undef ...'); { is( LANraragi::Plugin::Metadata::CopyArchiveTags::extract_archive_id(undef), undef, 'param was undef' ); @@ -94,7 +110,6 @@ note('extract_archive_id returns undef if param doesn\'t contain a valid archive is( LANraragi::Plugin::Metadata::CopyArchiveTags::extract_archive_id("http://127.0.0.1:3000/reader?id=${long_hex}"), undef, 'invalid id: too long hex number' ); - } note('extract_archive_id returns the ID in lowercase ...'); @@ -137,10 +152,9 @@ note('extract_archive_id parses oneshot_param ...'); } -note('internal_get_tags dies when oneshot_param is undefined ...'); +note('get_tags dies when oneshot_param is undefined ...'); { - - my $log_mock = get_logger_mock(); + my $lrr_info = { 'archive_id' => 'dummy' }; my $params = { 'oneshot' => undef, 'copy_date_added' => undef, @@ -148,7 +162,7 @@ note('internal_get_tags dies when oneshot_param is undefined ...'); }; # Act - trap { LANraragi::Plugin::Metadata::CopyArchiveTags::internal_get_tags( $log_mock, $params ); }; + trap { LANraragi::Plugin::Metadata::CopyArchiveTags::get_tags( 'dummy', $lrr_info, 0 ); }; is( $trap->exit, undef, 'no exit code' ); is( $trap->stdout, '', 'no STDOUT' ); @@ -156,18 +170,12 @@ note('internal_get_tags dies when oneshot_param is undefined ...'); like( $trap->die, qr/^oneshot_param doesn't contain a valid archive ID/, 'die message' ); } -note('internal_get_tags dies when oneshot_param is empty ...'); +note('get_tags dies when oneshot_param is empty ...'); { - - my $log_mock = get_logger_mock(); - my $params = { - 'oneshot' => '', - 'copy_date_added' => undef, - 'lrr_info' => { 'archive_id' => 'dummy' } - }; + my $lrr_info = { 'oneshot_param' => '', 'archive_id' => 'dummy' }; # Act - trap { LANraragi::Plugin::Metadata::CopyArchiveTags::internal_get_tags( $log_mock, $params ); }; + trap { LANraragi::Plugin::Metadata::CopyArchiveTags::get_tags( 'dummy', $lrr_info, 0 ); }; is( $trap->exit, undef, 'no exit code' ); is( $trap->stdout, '', 'no STDOUT' ); @@ -175,18 +183,12 @@ note('internal_get_tags dies when oneshot_param is empty ...'); like( $trap->die, qr/^oneshot_param doesn't contain a valid archive ID/, 'die message' ); } -note('internal_get_tags dies when oneshot_param doesn\'t contain a valid archive ID ...'); +note('get_tags dies when oneshot_param doesn\'t contain a valid archive ID ...'); { - - my $log_mock = get_logger_mock(); - my $params = { - 'oneshot' => 'xpto', - 'copy_date_added' => undef, - 'lrr_info' => { 'archive_id' => 'dummy' } - }; + my $lrr_info = { 'oneshot_param' => 'xpto', 'archive_id' => 'dummy' }; # Act - trap { LANraragi::Plugin::Metadata::CopyArchiveTags::internal_get_tags( $log_mock, $params ); }; + trap { LANraragi::Plugin::Metadata::CopyArchiveTags::get_tags( 'dummy', $lrr_info, 0 ); }; is( $trap->exit, undef, 'no exit code' ); is( $trap->stdout, '', 'no STDOUT' ); @@ -194,19 +196,13 @@ note('internal_get_tags dies when oneshot_param doesn\'t contain a valid archive like( $trap->die, qr/^oneshot_param doesn't contain a valid archive ID/, 'die message' ); } -note('internal_get_tags dies when search ID matches the current archive ID ...'); +note('get_tags dies when search ID matches the current archive ID ...'); { - - my $log_mock = get_logger_mock(); my $the_only_id = _random_archive_id(); - my $params = { - 'oneshot' => $the_only_id, - 'copy_date_added' => undef, - 'lrr_info' => { 'archive_id' => $the_only_id } - }; + my $lrr_info = { 'oneshot_param' => $the_only_id, 'archive_id' => $the_only_id }; # Act - trap { LANraragi::Plugin::Metadata::CopyArchiveTags::internal_get_tags( $log_mock, $params ); }; + trap { LANraragi::Plugin::Metadata::CopyArchiveTags::get_tags( 'dummy', $lrr_info, 0 ); }; is( $trap->exit, undef, 'no exit code' ); is( $trap->stdout, '', 'no STDOUT' ); @@ -214,15 +210,12 @@ note('internal_get_tags dies when search ID matches the current archive ID ...') like( $trap->die, qr/^You are using the current archive ID/, 'die message' ); } -note('internal_get_tags does not return date_added by default ...'); +note('get_tags does not return date_added by default ...'); { - my @log_messages; - my $log_mock = get_logger_mock( \@log_messages ); - my $input_id = _random_archive_id(); - my $params = { - 'oneshot' => $input_id, - 'lrr_info' => { 'archive_id' => _random_archive_id() } - }; + @log_messages = (); + my $input_id = _random_archive_id(); + my $lrr_info = { 'oneshot_param' => $input_id, 'archive_id' => 'dummy' }; + my $copy_date_added = undef; no warnings 'once', 'redefine'; local *LANraragi::Utils::Database::get_tags = sub { @@ -230,23 +223,25 @@ note('internal_get_tags does not return date_added by default ...'); }; # Act - my %data = LANraragi::Plugin::Metadata::CopyArchiveTags::internal_get_tags( $log_mock, $params ); + my %data = LANraragi::Plugin::Metadata::CopyArchiveTags::get_tags( 'dummy', $lrr_info, $copy_date_added ); cmp_deeply( \%data, { 'tags' => 'tag1,tag2' }, 'returned tags list' ); - cmp_deeply( \@log_messages, [ [ 'info', ignore, "Copying tags from archive \"${input_id}\"" ] ], 'log messages' ); + cmp_deeply( + \@log_messages, + [ [ 'info', ignore, "Copying tags from archive \"${input_id}\"" ], + [ 'info', ignore, 'Sending the following tags to LRR: tag1,tag2' ] + ], + 'log messages' + ); } -note('internal_get_tags returns date_added if asked ...'); +note('get_tags returns date_added if asked ...'); { - my @log_messages; - my $log_mock = get_logger_mock( \@log_messages ); - my $input_id = _random_archive_id(); - my $params = { - 'oneshot' => $input_id, - 'copy_date_added' => 1, - 'lrr_info' => { 'archive_id' => _random_archive_id() } - }; + @log_messages = (); + my $input_id = _random_archive_id(); + my $lrr_info = { 'oneshot_param' => $input_id, 'archive_id' => 'dummy' }; + my $copy_date_added = 1; no warnings 'once', 'redefine'; local *LANraragi::Utils::Database::get_tags = sub { @@ -254,11 +249,17 @@ note('internal_get_tags returns date_added if asked ...'); }; # Act - my %data = LANraragi::Plugin::Metadata::CopyArchiveTags::internal_get_tags( $log_mock, $params ); + my %data = LANraragi::Plugin::Metadata::CopyArchiveTags::get_tags( 'dummy', $lrr_info, $copy_date_added ); cmp_deeply( \%data, { 'tags' => 'date_added:321,tag3,tag4' }, 'returned tags list' ); - cmp_deeply( \@log_messages, [ [ 'info', ignore, "Copying tags from archive \"${input_id}\"" ] ], 'log messages' ); + cmp_deeply( + \@log_messages, + [ [ 'info', ignore, "Copying tags from archive \"${input_id}\"" ], + [ 'info', ignore, 'Sending the following tags to LRR: date_added:321,tag3,tag4' ] + ], + 'log messages' + ); } done_testing(); diff --git a/tests/LANraragi/Plugin/Metadata/Fakku.t b/tests/LANraragi/Plugin/Metadata/Fakku.t index 67d73d4b0..cf457ec84 100644 --- a/tests/LANraragi/Plugin/Metadata/Fakku.t +++ b/tests/LANraragi/Plugin/Metadata/Fakku.t @@ -9,6 +9,7 @@ use Mojo::File; use Test::More; use Test::Deep; +use Test::Trap; my $cwd = getcwd(); my $SAMPLES = "$cwd/tests/samples"; @@ -71,4 +72,141 @@ note("testing parsing gallery front page with source tag..."); is( $summary, 'Bold baby bold. This is a test summary. Hi mom.', "summary check" ); } +note("get_tags dies if fakku's cookie doesn't exists"); + +{ + my $ua_mock = Test::MockObject->new(); + $ua_mock->mock( 'cookie_jar' => sub { return; } ); + + my $lrr_info = { 'user_agent' => $ua_mock }; + + no warnings 'once', 'redefine'; + local *LANraragi::Plugin::Metadata::Fakku::fakku_cookie_exists = sub { return; }; + local *LANraragi::Plugin::Metadata::Fakku::get_plugin_logger = sub { return get_logger_mock(); }; + + # Act + trap { LANraragi::Plugin::Metadata::Fakku::get_tags( 'dummy', $lrr_info, 0 ); }; + + like( $trap->die, qr/^Not logged in to FAKKU/, 'die message' ); +} + +note("when oneshot_param is defined, use it to connect to fakku"); + +{ + my $ua_mock = Test::MockObject->new(); + $ua_mock->mock( 'cookie_jar' => sub { return; } ); + + my $oneshot_param = 'try-me'; + my $lrr_info = { + 'user_agent' => $ua_mock, + 'oneshot_param' => $oneshot_param, + 'archive_title' => 'invalid title', + 'existing_tags' => 'dummy, source:fakku.net/bla' + }; + my $fakku_URL; + + no warnings 'once', 'redefine'; + local *LANraragi::Plugin::Metadata::Fakku::fakku_cookie_exists = sub { return 1; }; + local *LANraragi::Plugin::Metadata::Fakku::get_plugin_logger = sub { return get_logger_mock(); }; + local *LANraragi::Plugin::Metadata::Fakku::get_tags_from_fakku = sub { + ( $fakku_URL, undef, undef ) = @_; + return ( 'the tags', 'the title', 'the summary' ); + }; + + # Act + my @rdata = LANraragi::Plugin::Metadata::Fakku::get_tags( 'dummy', $lrr_info, 0 ); + + is( $fakku_URL, $oneshot_param, 'used oneshot_param' ); +} + +note("when oneshot_param isn't defined, use tag 'source'"); + +{ + my $ua_mock = Test::MockObject->new(); + $ua_mock->mock( 'cookie_jar' => sub { return; } ); + + my $tag_source = 'fakku.net/bla'; + my $lrr_info = { + 'user_agent' => $ua_mock, + 'archive_title' => 'invalid title', + 'existing_tags' => "dummy,source:${tag_source}" + }; + my $fakku_URL; + + no warnings 'once', 'redefine'; + local *LANraragi::Plugin::Metadata::Fakku::fakku_cookie_exists = sub { return 1; }; + local *LANraragi::Plugin::Metadata::Fakku::get_plugin_logger = sub { return get_logger_mock(); }; + local *LANraragi::Plugin::Metadata::Fakku::get_tags_from_fakku = sub { + ( $fakku_URL, undef, undef ) = @_; + return ( 'the tags', 'the title', 'the summary' ); + }; + + # Act + my @rdata = LANraragi::Plugin::Metadata::Fakku::get_tags( 'dummy', $lrr_info, 0 ); + + is( $fakku_URL, $tag_source, 'found \'source:*\' tag' ); +} + +note("when oneshot_param and tag 'source' aren't useful, search by title"); + +{ + my $ua_mock = Test::MockObject->new(); + $ua_mock->mock( 'cookie_jar' => sub { return; } ); + + my $archive_title = 'current title'; + my $found_url = 'url from title'; + my $lrr_info = { + 'user_agent' => $ua_mock, + 'archive_title' => $archive_title, + 'existing_tags' => "dummy,source:hsite.com/bla" + }; + my $fakku_URL; + my $used_title; + + no warnings 'once', 'redefine'; + local *LANraragi::Plugin::Metadata::Fakku::fakku_cookie_exists = sub { return 1; }; + local *LANraragi::Plugin::Metadata::Fakku::get_plugin_logger = sub { return get_logger_mock(); }; + local *LANraragi::Plugin::Metadata::Fakku::search_for_fakku_url = sub { + ( $used_title, undef ) = @_; + return $found_url; + }; + local *LANraragi::Plugin::Metadata::Fakku::get_tags_from_fakku = sub { + ( $fakku_URL, undef, undef ) = @_; + return ( 'the tags', 'the title', 'the summary' ); + }; + + # Act + my @rdata = LANraragi::Plugin::Metadata::Fakku::get_tags( 'dummy', $lrr_info, 0 ); + + is( $used_title, $archive_title, 'title to search' ); + is( $fakku_URL, $found_url, 'found URL' ); +} + +note("when no URL is found, die with error"); + +{ + my $ua_mock = Test::MockObject->new(); + $ua_mock->mock( 'cookie_jar' => sub { return; } ); + + my $archive_title = 'current title'; + my $found_url = 'url from title'; + my $lrr_info = { + 'user_agent' => $ua_mock, + 'archive_title' => $archive_title, + 'existing_tags' => "dummy,source:hsite.com/bla" + }; + my $fakku_URL; + my $used_title; + + no warnings 'once', 'redefine'; + local *LANraragi::Plugin::Metadata::Fakku::fakku_cookie_exists = sub { return 1; }; + local *LANraragi::Plugin::Metadata::Fakku::get_plugin_logger = sub { return get_logger_mock(); }; + local *LANraragi::Plugin::Metadata::Fakku::search_for_fakku_url = sub { return; }; + + # Act + trap { LANraragi::Plugin::Metadata::Fakku::get_tags( 'dummy', $lrr_info, 0 ); }; + + like( $trap->die, qr/^No matching FAKKU Gallery Found/, 'die message' ); +} + done_testing(); diff --git a/tests/LANraragi/Plugin/Metadata/HentagOnline.t b/tests/LANraragi/Plugin/Metadata/HentagOnline.t index 870c25105..256f2ac9c 100644 --- a/tests/LANraragi/Plugin/Metadata/HentagOnline.t +++ b/tests/LANraragi/Plugin/Metadata/HentagOnline.t @@ -178,8 +178,13 @@ note("07 - no allowed language"); my %get_tags_params = ( archive_title => $archive_title ); - my %response = LANraragi::Plugin::Metadata::HentagOnline::get_tags( "", \%get_tags_params, "florp, flarp" ); - ok( exists( $response{error} ), "got an error" ); + # Act + trap { LANraragi::Plugin::Metadata::HentagOnline::get_tags( "", \%get_tags_params, "florp, flarp" ); }; + + is( $trap->exit, undef, 'no exit code' ); + is( $trap->stdout, '', 'no STDOUT' ); + is( $trap->stderr, '', 'no STDERR' ); + like( $trap->die, qr/^No matching Hentag Archive Found!/, 'die message' ); } note("08 - multiple hits in same language"); diff --git a/tools/Documentation/plugin-docs/code-examples.md b/tools/Documentation/plugin-docs/code-examples.md index 32579911a..d67684607 100644 --- a/tools/Documentation/plugin-docs/code-examples.md +++ b/tools/Documentation/plugin-docs/code-examples.md @@ -72,7 +72,7 @@ if ($info_path) { #Do whatever you need with the extracted file open( my $fh, '<:encoding(UTF-8)', $filepath ) - or return ( error => "Could not open $filepath!" ); + or die "Could not open $filepath!\n"; while ( my $row = <$fh> ) { #... @@ -82,4 +82,3 @@ if ($info_path) { unlink $filepath; } ``` - diff --git a/tools/Documentation/plugin-docs/download.md b/tools/Documentation/plugin-docs/download.md index 11b45e3df..d5a42dfd0 100644 --- a/tools/Documentation/plugin-docs/download.md +++ b/tools/Documentation/plugin-docs/download.md @@ -1,21 +1,21 @@ -# Downloader Plugins +# Downloader Plugins -Downloader Plugins are used as part of LANraragi's built-in downloading feature. +Downloader Plugins are used as part of LANraragi's built-in downloading feature. ## Required subroutines -Only one subroutine needs to be implemented for the module to be recognized: `provide_url`, which contains your working code. You're free to implement other subroutines for cleaner code, of course. +Only one subroutine needs to be implemented for the module to be recognized: `provide_url`, which contains your working code. You're free to implement other subroutines for cleaner code, of course. -Your plugin also needs an extra field in its metadata: `url_regex`, which contains a Regular Expression that'll be used by LANraragi to know if your Downloader should be used. -For example, if your regex is `https?:\/\/example.com.*`, LANraragi will invoke your plugin if the user wants to download an URL that comes from `example.com`. +Your plugin also needs an extra field in its metadata: `url_regex`, which contains a Regular Expression that'll be used by LANraragi to know if your Downloader should be used. +For example, if your regex is `https?:\/\/example.com.*`, LANraragi will invoke your plugin if the user wants to download an URL that comes from `example.com`. {% hint style="info" %} -In case of multiple Downloaders matching the given URL, the server will invoke the first plugin that matches. +In case of multiple Downloaders matching the given URL, the server will invoke the first plugin that matches. {% endhint %} ### Expected Input -The following section deals with writing the `provide_url` subroutine. +The following section deals with writing the `provide_url` subroutine. When executing your Plugin, LRR will call this subroutine and pass it the following variables: ```perl @@ -36,12 +36,16 @@ The `$lrr_info` hash contains two variables you can use in your plugin: ### Expected Output -LRR expects Downloaders to return a hash, containing a new URL that can be downloaded directly. -Said URL should **directly** point to a file -- Any form of HTML will trigger a failed download. +LRR expects Downloaders to return a hash, containing a new URL that can be downloaded directly. +Said URL should **directly** point to a file -- Any form of HTML will trigger a failed download. `return ( download_url => "http://my.remote.service/download/secret-archive.zip" );` -If your script errored out, you can tell LRR that an error occurred by returning a hash containing an "error" field: +If your script errored out, you can immediately stop the plugin execution and tell LRR that an error occurred by throwing an exception: + +`die "my error :(\n";` + +or by returning a hash containing an "error" field (**this method is deprecated**): `return ( error => "my error :(" );` @@ -55,7 +59,7 @@ package LANraragi::Plugin::Download::MyNewDownloader; use strict; use warnings; -# Plugins can freely use all Perl packages already installed on the system +# Plugins can freely use all Perl packages already installed on the system # Try however to restrain yourself to the ones already installed for LRR (see tools/cpanfile) to avoid extra installations by the end-user. use Mojo::UserAgent; @@ -77,7 +81,7 @@ sub plugin_info { version => "0.1", description => "This is base boilerplate for writing LRR downloaders. Returns a static URL if you try to download a URL from http://example.com.", icon => "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAIAAAAC64paAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAABZSURBVDhPzY5JCgAhDATzSl+e/2irOUjQSFzQog5hhqIl3uBEHPxIXK7oFXwVE+Hj5IYX4lYVtN6MUW4tGw5jNdjdt5bLkwX1q2rFU0/EIJ9OUEm8xquYOQFEhr9vvu2U8gAAAABJRU5ErkJggg==", - + # Downloader-specific metadata url_regex => "https?:\/\/example.com.*" ); @@ -106,12 +110,11 @@ sub run_script { if ($res->is_success) { return ( download_url => $reply ); } - elsif ($res->is_error) { - return ( error => "Dingus! ".$res->message ); + elsif ($res->is_error) { + die "Dingus! ".$res->message; } } 1; ``` - diff --git a/tools/Documentation/plugin-docs/metadata.md b/tools/Documentation/plugin-docs/metadata.md index 41a53966a..8ee4931dd 100644 --- a/tools/Documentation/plugin-docs/metadata.md +++ b/tools/Documentation/plugin-docs/metadata.md @@ -8,7 +8,7 @@ Only one subroutine needs to be implemented for the module to be recognized: `ge ### Expected Input -The following section deals with writing the `get_tags` subroutine. +The following section deals with writing the `get_tags` subroutine. When executing your Plugin, LRR will call this subroutine and pass it the following variables: ```perl @@ -25,7 +25,7 @@ The variables match the parameters you've entered in the `plugin_info` subroutin The `$lrr_info` hash contains various variables you can use in your plugin: * _$lrr\_info->{archive\_id}_: The internal ID of the archive. -* _$lrr\_info->{archive\_title}_: The title of the archive, as entered by the User. +* _$lrr\_info->{archive\_title}_: The title of the archive, as entered by the User. * _$lrr\_info->{existing\_tags}_: The tags that are already in LRR for this archive, if there are any. * _$lrr\_info->{thumbnail\_hash}_: A SHA-1 hash of the first image of the archive. * _$lrr\_info->{file\_path}_: The filesystem path to the archive. @@ -34,8 +34,8 @@ The `$lrr_info` hash contains various variables you can use in your plugin: #### One-Shot Arguments -The **One-Shot Argument** can be set by the user every time he uses your Plugin on a specific file, through LRR's Edit Menu. -It's more meant for special overrides you'd want to use for this specific file: +The **One-Shot Argument** can be set by the user every time he uses your Plugin on a specific file, through LRR's Edit Menu. +It's more meant for special overrides you'd want to use for this specific file: For example, in E-Hentai and nHentai plugins, it can be used to set a specific Gallery URL you want to pull tags from. If you want the user to be able to enter this override, the `oneshot_arg` field must be present in `plugin_info`, and contain a brief description of what your argument is for. @@ -44,17 +44,21 @@ One-Shot Arguments can only be strings. ### Expected Output -Once you're done and obtained your tags, all that's needed for LRR to handle them is to return a hash containg said tags. +Once you're done and obtained your tags, all that's needed for LRR to handle them is to return a hash containg said tags. Tags are expected to be separated by commas, like this: `return ( tags => "my:new, tags:here, look ma no namespace" );` -Plugins can also modify the title or the summary of the archive: -`return ( tags => "some:tags", title=>"My new epic archive title", summary=>"Phenomenal! Wheres that David Lynch clip where he says phenomenal" );` +Plugins can also modify the title or the summary of the archive: +`return ( tags => "some:tags", title=>"My new epic archive title", summary=>"Phenomenal! Wheres that David Lynch clip where he says phenomenal" );` Those two parameters are completely optional. \(The tags one isn't, but it can very well be empty.\) If the Plugin's user has disabled "Plugins can modify archive titles" in their settings, you can still pass a new title - It'll simply do nothing. -If you couldn't obtain tags for some reason, you can tell LRR that an error occurred by returning a hash containing an "error" field: +If you couldn't obtain tags for some reason, you can tell LRR that an error occurred by throwing an exception: + +`die "my error :(\n";` + +The old error handling still exists, but it's **deprecated**: `return ( error => "my error :(" );` @@ -68,7 +72,7 @@ package LANraragi::Plugin::Metadata::MyNewPlugin; use strict; use warnings; -# Plugins can freely use all Perl packages already installed on the system +# Plugins can freely use all Perl packages already installed on the system # Try however to restrain yourself to the ones already installed for LRR (see tools/cpanfile) to avoid extra installations by the end-user. use Mojo::UserAgent; @@ -91,7 +95,7 @@ sub plugin_info { version => "0.001", description => "This is base boilerplate for writing LRR plugins.", icon => "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAABmJLR0QAAAAAAAD5Q7t/AAAACXBI\nWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4wYDFCYzptBwXAAAAB1pVFh0Q29tbWVudAAAAAAAQ3Jl\nYXRlZCB3aXRoIEdJTVBkLmUHAAAAjUlEQVQ4y82UwQ7AIAhDqeH/f7k7kRgmiozDPKppyisAkpTG\nM6T5vAQBCIAeQQBCUkiWRTV68KJZ1FuG5vY/oazYGdcWh7diy1Bml5We1yiMW4dmQr+W65mPjFjU\n5PMg2P9jKKvUdxWMU8neqYUW4cBpffnxi8TsXk/Qs8GkGGaWhmes1ZmNmr8kuMPwAJzzZSoHwxbF\nAAAAAElFTkSuQmCC", - #If your plugin uses/needs custom arguments, input their name here. + #If your plugin uses/needs custom arguments, input their name here. #This name will be displayed in plugin configuration next to an input box for global arguments, and in archive edition for one-shot arguments. oneshot_arg => "This is a one-shot argument that can be entered by the user when executing this plugin on a file", parameters => [ @@ -110,25 +114,21 @@ sub get_tags { my ($doomsday, $iterations) = @_; # Plugin parameters if ($lrr_info->{oneshot_param}) { - return ( error => "Yaaaaaaaaa gomen gomen the oneshot argument isn't implemented -- You entered ".$lrr_info->{oneshot_param}.", right ?"); + die "Yaaaaaaaaa gomen gomen the oneshot argument isn't implemented -- You entered ".$lrr_info->{oneshot_param}.", right ?\n"; } #Use the logger to output status - they'll be passed to a specialized logfile and written to STDOUT. my $logger = get_logger("My Cool Plugin","plugins"); - if ($doomsday) { - return ( error => "You fools! You've messed with the natural order!"); - } - #Work your magic here - You can create subroutines below to organize the code better $logger->info("Gettin' tags"); - my $newtags = get_tags_from_somewhere($iterations); #To be implemented + my $newtags = get_tags_from_somewhere($iterations, $doomsday); #To be implemented my $error = 0; #Something went wrong? Return an error. if ($error) { $logger->error("Oh no"); - return ( error => "Error Text Here"); + die "Error Text Here\n"; } #Otherwise, return the tags you've harvested. @@ -137,15 +137,18 @@ sub get_tags { sub get_tags_from_somewhere { - my $iterations = shift; + my ($iterations, $doomsday) = @_; my $logger = get_logger("My Cool Plugin","plugins"); + if ($doomsday) { + die "You fools! You've messed with the natural order!\n"; + } + $logger->info("I'm supposed to be iterating $iterations times but I don't give a damn my man"); #Tags are expected to be submitted as a single string, containing tags split up by commas. Namespaces are optional. - return "my:new, tags:here, look ma no namespace"; + return "my:new, tags:here, look ma no namespace"; } 1; ``` -