Skip to content

Commit

Permalink
(#789) Make "save archive title" a global preference
Browse files Browse the repository at this point in the history
  • Loading branch information
Difegue committed Oct 26, 2023
1 parent 7ba8425 commit d6da3f0
Show file tree
Hide file tree
Showing 23 changed files with 385 additions and 400 deletions.
2 changes: 1 addition & 1 deletion lib/LANraragi.pm
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ sub startup {
}

# Use the hostname alongside the random secret
$self->secrets( $secret . hostname() );
$self->secrets( [ $secret . hostname() ] );
$self->plugin('RenderFile');

# Set Template::Toolkit as default renderer so we can use the LRR templates
Expand Down
27 changes: 17 additions & 10 deletions lib/LANraragi/Controller/Plugins.pm
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ sub index {
my @downloadplugins = get_plugins("download");

$self->render(
template => "plugins",
title => $self->LRR_CONF->get_htmltitle,
descstr => $self->LRR_DESC,
metadata => craft_plugin_array(@metaplugins),
downloaders => craft_plugin_array(@downloadplugins),
logins => craft_plugin_array(@loginplugins),
scripts => craft_plugin_array(@scriptplugins),
csshead => generate_themes_header($self),
version => $self->LRR_VERSION
template => "plugins",
title => $self->LRR_CONF->get_htmltitle,
descstr => $self->LRR_DESC,
replacetitles => $self->LRR_CONF->can_replacetitles,
metadata => craft_plugin_array(@metaplugins),
downloaders => craft_plugin_array(@downloadplugins),
logins => craft_plugin_array(@loginplugins),
scripts => craft_plugin_array(@scriptplugins),
csshead => generate_themes_header($self),
version => $self->LRR_VERSION
);

}
Expand Down Expand Up @@ -81,6 +82,12 @@ sub save_config {
my $errormess = "";

eval {

# Save title preference first
my $replacetitles = ( scalar $self->req->param('replacetitles') ? '1' : '0' );
$redis->hset( "LRR_CONFIG", "replacetitles", $replacetitles );

# Save each plugin's settings
foreach my $pluginfo (@plugins) {

my $namespace = $pluginfo->{namespace};
Expand Down Expand Up @@ -172,7 +179,7 @@ sub process_upload {
return;
}

my $dir = getcwd() . ("/lib/LANraragi/Plugin/$plugintype/");
my $dir = getcwd() . ("/lib/LANraragi/Plugin/$plugintype/");
my $output_file = $dir . $filename;

$logger->info("Uploading new plugin $filename to $output_file ...");
Expand Down
3 changes: 2 additions & 1 deletion lib/LANraragi/Model/Config.pm
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ my $home = Mojo::Home->new;
$home->detect;

my $config = Mojolicious::Plugin::Config->register( Mojolicious->new, { file => $home . '/lrr.conf' } );
if ($ENV{LRR_REDIS_ADDRESS}) {
if ( $ENV{LRR_REDIS_ADDRESS} ) {
$config->{redis_address} = $ENV{LRR_REDIS_ADDRESS};
}

Expand Down Expand Up @@ -187,5 +187,6 @@ sub enable_cryptofs { return &get_redis_conf( "enablecryptofs", "0" ) }
sub get_hqthumbpages { return &get_redis_conf( "hqthumbpages", "0" ) }
sub get_jxlthumbpages { return &get_redis_conf( "jxlthumbpages", "0" ) }
sub get_replacedupe { return &get_redis_conf( "replacedupe", "0" ) }
sub can_replacetitles { return &get_redis_conf( "replacetitles", "1" ) }

1;
12 changes: 6 additions & 6 deletions lib/LANraragi/Model/Plugins.pm
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ use Mojo::JSON qw(decode_json encode_json);
use Mojo::UserAgent;
use Data::Dumper;

use LANraragi::Utils::String qw(trim);
use LANraragi::Utils::String qw(trim);
use LANraragi::Utils::Database qw(set_tags set_title);
use LANraragi::Utils::Archive qw(extract_thumbnail);
use LANraragi::Utils::Logging qw(get_logger);
use LANraragi::Utils::Tags qw(rewrite_tags split_tags_to_array);
use LANraragi::Utils::Archive qw(extract_thumbnail);
use LANraragi::Utils::Logging qw(get_logger);
use LANraragi::Utils::Tags qw(rewrite_tags split_tags_to_array);

# Sub used by Auto-Plugin.
sub exec_enabled_plugins_on_file {
Expand Down Expand Up @@ -80,7 +80,7 @@ sub exec_enabled_plugins_on_file {
set_title( $id, $plugin_result{title} );

$newtitle = $plugin_result{title};
$logger->debug("Changing title to $newtitle.");
$logger->debug("Changing title to $newtitle. (Will do nothing if title is blank)");
}
}
}
Expand Down Expand Up @@ -270,7 +270,7 @@ sub exec_metadata_plugin {
my %returnhash = ( new_tags => $newtags );

# Indicate a title change, if the plugin reports one
if ( exists $newmetadata{title} ) {
if ( exists $newmetadata{title} && LANraragi::Model::Config->can_replacetitles ) {

my $newtitle = $newmetadata{title};
$newtitle = trim($newtitle);
Expand Down
68 changes: 38 additions & 30 deletions lib/LANraragi/Plugin/Metadata/Chaika.pm
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,20 @@ sub plugin_info {
type => "metadata",
namespace => "trabant",
author => "Difegue",
version => "2.3",
description => "Searches chaika.moe for tags matching your archive. This will try to use the thumbnail first, and fallback to a default text search.",
version => "2.3.1",
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 =>
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAAsTAAALEwEAmpwYAAAA\nB3RJTUUH4wYCFQocjU4r+QAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUH\nAAAEZElEQVQ4y42T3WtTdxzGn/M7J+fk5SRpTk7TxMZkXU84tTbVNrUT3YxO7HA4pdtQZDe7cgx2\ns8vBRvEPsOwFYTDYGJUpbDI2wV04cGXCGFLonIu1L2ptmtrmxeb1JDkvv121ZKVze66f74eH7/f5\nMmjRwMCAwrt4/9KDpflMJpPHvyiR2DPcJklJ3TRDDa0xk36cvrm8vDwHAAwAqKrqjjwXecPG205w\nHBuqa9rk77/d/qJYLD7cCht5deQIIczbgiAEKLVAKXWUiqVV06Tf35q8dYVJJBJem2A7Kwi2nQzD\nZig1CG93+PO5/KN6tf5NKpVqbsBUVVVFUUxwHJc1TXNBoxojS7IbhrnLMMx9pVJlBqFQKBKPxwcB\nkJYgjKIo3QCE1nSKoghbfJuKRqN2RVXexMaQzWaLezyeEUEQDjscjk78PxFFUYRkMsltJgGA3t7e\nyMLCwie6rr8iC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K5CYII=",
parameters => [
{ type => "bool", desc => "Save archive title" },
parameters => [
{ type => "bool", desc => "Add the following tags if available: download URL, gallery ID, category, timestamp" },
{ type => "bool", desc => "Add tags without a namespace to the 'other:' namespace instead, mirroring E-H's behavior of namespacing everything" },
{ type => "string", desc => "Add a custom 'source:' tag to your archive. Example: chaika. Will NOT add a tag if blank" }
{ type => "bool",
desc =>
"Add tags without a namespace to the 'other:' namespace instead, mirroring E-H's behavior of namespacing everything"
},
{ type => "string",
desc => "Add a custom 'source:' tag to your archive. Example: chaika. Will NOT add a tag if blank"
}
],
oneshot_arg => "Chaika Gallery or Archive URL (Will attach matching tags to your archive)"
);
Expand All @@ -38,8 +43,8 @@ sub plugin_info {
sub get_tags {

shift;
my $lrr_info = shift; # Global info hash
my ( $savetitle, $addextra, $addother, $addsource ) = @_; # Plugin parameters
my $lrr_info = shift; # Global info hash
my ( $addextra, $addother, $addsource ) = @_; # Plugin parameters

my $logger = get_plugin_logger();
my $newtags = "";
Expand All @@ -52,13 +57,14 @@ sub get_tags {
} else {

# Try SHA-1 reverse search first
$logger->info("Using thumbnail hash " . $lrr_info->{thumbnail_hash});
$logger->info( "Using thumbnail hash " . $lrr_info->{thumbnail_hash} );
( $newtags, $newtitle ) = tags_from_sha1( $lrr_info->{thumbnail_hash}, $addextra, $addother, $addsource );

# Try text search if it fails
if ( $newtags eq "" ) {
$logger->info("No results, falling back to text search.");
( $newtags, $newtitle ) = search_for_archive( $lrr_info->{archive_title}, $lrr_info->{existing_tags}, $addextra, $addother, $addsource );
( $newtags, $newtitle ) =
search_for_archive( $lrr_info->{archive_title}, $lrr_info->{existing_tags}, $addextra, $addother, $addsource );
}
}

Expand All @@ -67,9 +73,9 @@ sub get_tags {
return ( error => "No matching Chaika Archive Found!" );
} else {
$logger->info("Sending the following tags to LRR: $newtags");

#Return a hash containing the new metadata
if ( $savetitle && $newtags ne "" ) { return ( tags => $newtags, title => $newtitle ); }
else { return ( tags => $newtags ); }
return ( tags => $newtags, title => $newtitle );
}

}
Expand Down Expand Up @@ -145,7 +151,7 @@ sub get_json_from_chaika {
my $ua = Mojo::UserAgent->new;
my $res = $ua->get($URL)->result;

if ($res->is_error) {
if ( $res->is_error ) {
return;
}
my $textrep = $res->body;
Expand All @@ -161,38 +167,40 @@ sub parse_chaika_json {

my $tags = $json->{"tags"} || ();
foreach my $tag (@$tags) {

#Replace underscores with spaces
$tag =~ s/_/ /g;

#Add 'other' namespace if none
if ($addother && index($tag, ":") == -1) {
if ( $addother && index( $tag, ":" ) == -1 ) {
$tag = "other:" . $tag;
}
}

my $category = lc $json->{"category"};
my $download = $json->{"download"} ? $json->{"download"} : $json->{"archives"}->[0]->{"link"};
my $gallery = $json->{"gallery"} ? $json->{"gallery"} : $json->{"id"};
my $category = lc $json->{"category"};
my $download = $json->{"download"} ? $json->{"download"} : $json->{"archives"}->[0]->{"link"};
my $gallery = $json->{"gallery"} ? $json->{"gallery"} : $json->{"id"};
my $timestamp = $json->{"posted"};
if ($tags && $addextra) {
if ($category ne "") {
push(@$tags, "category:" . $category);
if ( $tags && $addextra ) {
if ( $category ne "" ) {
push( @$tags, "category:" . $category );
}
if ($download ne "") {
push(@$tags, "download:" . $download);
if ( $download ne "" ) {
push( @$tags, "download:" . $download );
}
if ($gallery ne "") {
push(@$tags, "gallery:" . $gallery);
if ( $gallery ne "" ) {
push( @$tags, "gallery:" . $gallery );
}
if ($timestamp ne "") {
push(@$tags, "timestamp:" . $timestamp);
if ( $timestamp ne "" ) {
push( @$tags, "timestamp:" . $timestamp );
}
}

if ($gallery && $gallery ne "") {
if ( $gallery && $gallery ne "" ) {

# add custom source, but only if having found gallery
if ($addsource && $addsource ne "") {
push(@$tags, "source:" . $addsource);
if ( $addsource && $addsource ne "" ) {
push( @$tags, "source:" . $addsource );
}
return ( join( ', ', @$tags ), $json->{"title"} );
} else {
Expand Down
68 changes: 37 additions & 31 deletions lib/LANraragi/Plugin/Metadata/ChaikaFile.pm
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@ sub plugin_info {
type => "metadata",
namespace => "chaikafileplugin",
author => "Difegue & Plebs",
version => "0.1",
version => "0.2",
description => "Collects metadata embedded into your archives as Chaika-style api.json files",
icon =>
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=",
parameters => [
{ type => "bool", desc => "Save archive title" },
parameters => [
{ type => "bool", desc => "Add the following tags if available: download URL, gallery ID, category, timestamp" },
{ type => "bool", desc => "Add tags without a namespace to the 'other:' namespace instead, mirroring E-H's behavior of namespacing everything" },
{ type => "string", desc => "Add a custom 'source:' tag to your archive. Example: chaika. Will NOT add a tag if blank" }
{ type => "bool",
desc =>
"Add tags without a namespace to the 'other:' namespace instead, mirroring E-H's behavior of namespacing everything"
},
{ type => "string",
desc => "Add a custom 'source:' tag to your archive. Example: chaika. Will NOT add a tag if blank"
}
],
);
}
Expand All @@ -33,8 +37,8 @@ sub plugin_info {
sub get_tags {

shift;
my $lrr_info = shift; # Global info hash
my ($savetitle, $addextra, $addother, $addsource) = @_; # Plugin parameters
my $lrr_info = shift; # Global info hash
my ( $addextra, $addother, $addsource ) = @_; # Plugin parameters

my $logger = get_plugin_logger();
my $newtags = "";
Expand All @@ -43,6 +47,7 @@ sub get_tags {
# Try reading any embedded api.json file
my $path_in_archive = is_file_in_archive( $lrr_info->{file_path}, "api.json" );
if ($path_in_archive) {

#Extract api.json
my $filepath = extract_file_from_archive( $lrr_info->{file_path}, $path_in_archive );
( $newtags, $newtitle ) = tags_from_file( $filepath, $addextra, $addother, $addsource );
Expand All @@ -54,9 +59,9 @@ sub get_tags {
} else {

$logger->info("Sending the following tags to LRR: $newtags");

#Return a hash containing the new metadata
if ( $savetitle && $newtags ne "" ) { return ( tags => $newtags, title => $newtitle ); }
else { return ( tags => $newtags ); }
return ( tags => $newtags, title => $newtitle );
}

}
Expand All @@ -68,24 +73,24 @@ sub get_tags {
# tags_from_file
sub tags_from_file {

my ( $filepath, $addextra, $addother, $addsource ) = @_;
my ( $filepath, $addextra, $addother, $addsource ) = @_;

my $logger = get_plugin_logger();

#Open it
my $stringjson = "";

open( my $fh, '<:encoding(UTF-8)', $filepath )
or return ( error => "Could not open $filepath!" );
or return ( error => "Could not open $filepath!" );

while ( my $row = <$fh> ) {
chomp $row;
$stringjson .= $row;
}
unlink( $fh );
unlink($fh);

#Use Mojo::JSON to decode the string into a hash
my $hashjson = from_json( $stringjson );
my $hashjson = from_json($stringjson);

return parse_chaika_json( $hashjson, $addextra, $addother, $addsource );
}
Expand All @@ -99,44 +104,45 @@ sub parse_chaika_json {

my $tags = $json->{"tags"} || ();
foreach my $tag (@$tags) {

#Replace underscores with spaces
$tag =~ s/_/ /g;

#Add 'other' namespace if none
if ($addother && index($tag, ":") == -1) {
if ( $addother && index( $tag, ":" ) == -1 ) {
$tag = "other:" . $tag;
}
}

my $category = lc $json->{"category"};
my $download = $json->{"download"} ? $json->{"download"} : $json->{"archives"}->[0]->{"link"};
my $gallery = $json->{"gallery"} ? $json->{"gallery"} : $json->{"id"};
my $category = lc $json->{"category"};
my $download = $json->{"download"} ? $json->{"download"} : $json->{"archives"}->[0]->{"link"};
my $gallery = $json->{"gallery"} ? $json->{"gallery"} : $json->{"id"};
my $timestamp = $json->{"posted"};
if ($tags && $addextra) {
if ($category ne "") {
push(@$tags, "category:" . $category);
if ( $tags && $addextra ) {
if ( $category ne "" ) {
push( @$tags, "category:" . $category );
}
if ($download ne "") {
push(@$tags, "download:" . $download);
if ( $download ne "" ) {
push( @$tags, "download:" . $download );
}
if ($gallery ne "") {
push(@$tags, "gallery:" . $gallery);
if ( $gallery ne "" ) {
push( @$tags, "gallery:" . $gallery );
}
if ($timestamp ne "") {
push(@$tags, "timestamp:" . $timestamp);
if ( $timestamp ne "" ) {
push( @$tags, "timestamp:" . $timestamp );
}
}

if ($gallery && $gallery ne "") {
if ( $gallery && $gallery ne "" ) {

# add custom source, but only if having found gallery
if ($addsource && $addsource ne "") {
push(@$tags, "source:" . $addsource);
if ( $addsource && $addsource ne "" ) {
push( @$tags, "source:" . $addsource );
}
return ( join( ', ', @$tags ), $json->{"title"} );
} else {
return "";
}
}


1;
Loading

0 comments on commit d6da3f0

Please sign in to comment.