Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds plugin CopyArchiveTags #1021

Merged
merged 9 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions lib/LANraragi/Plugin/Metadata/CopyArchiveTags.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package LANraragi::Plugin::Metadata::CopyArchiveTags;

use v5.36;
use experimental 'try';

use strict;
use warnings;

use LANraragi::Model::Plugins;
use LANraragi::Utils::Database;
use LANraragi::Utils::Logging qw(get_plugin_logger);
use LANraragi::Utils::Tags qw(join_tags_to_string split_tags_to_array);
use LANraragi::Utils::String qw(trim);

#Meta-information about your plugin.
sub plugin_info {

return (
#Standard metadata
name => "Copy Archive Tags",
type => "metadata",
namespace => "copy-archive-tags",
author => "IceBreeze",
version => "1.0",
description => "Copy tags from another LRR archive given either the URI or the ID.",
parameters => [
{ type => "bool",
name => 'copy_date_added',
desc => "Enable to also copy the date (but it's up to you to remove the old one)"
}
],
oneshot_arg => "LRR Gallery URL or ID:"
);

}

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";
}

if ( $lrr_gid eq $params->{'lrr_info'}{'archive_id'} ) {
die "You are using the current archive ID\n";
}

$logger->info("Copying tags from archive \"${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 );

return %hashdata;
}

sub extract_archive_id {
my ($oneshot) = @_;
return if ( !$oneshot || length($oneshot) < 40 );
if ( ( lc $oneshot ) =~ m/([0-9a-f]{40,})/ ) {
return $1 if length($1) == 40;
}
return;
}

sub read_params {
IceBreeze marked this conversation as resolved.
Show resolved Hide resolved
my %plugin_info = plugin_info();
my $lrr_info = $_[1];
my @param_cfg = @{ $plugin_info{parameters} };

my %params;
$params{lrr_info} = $lrr_info;
$params{oneshot} = $lrr_info->{oneshot_param};
for ( my $i = 0; $i < scalar @param_cfg; $i++ ) {
my $value = $_[ $i + 2 ] || $param_cfg[$i]->{default};
$params{ $param_cfg[$i]->{name} } = $value;
}
return \%params;
}

1;
19 changes: 17 additions & 2 deletions lib/LANraragi/Utils/Database.pm
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ use LANraragi::Utils::Logging qw(get_logger);

# Functions for interacting with the DB Model.
use Exporter 'import';
our @EXPORT_OK =
qw(redis_encode redis_decode invalidate_cache compute_id change_archive_id set_tags set_title set_summary set_isnew get_computed_tagrules save_computed_tagrules get_archive_json get_archive_json_multi get_tankoubons_by_file);
our @EXPORT_OK = qw(
redis_encode redis_decode invalidate_cache compute_id change_archive_id set_tags set_title set_summary set_isnew get_computed_tagrules save_computed_tagrules get_tankoubons_by_file
get_archive get_archive_json get_archive_json_multi get_tags);

# Creates a DB entry for a file path with the given ID.
# This function doesn't actually require the file to exist at its given location.
Expand Down Expand Up @@ -135,6 +136,14 @@ sub add_pagecount ( $redis, $id ) {
$redis->hset( $id, "pagecount", scalar @images );
}

# Retrieves the archive's info as hash (empty if not found)
sub get_archive ($id) {
my $redis = LANraragi::Model::Config->get_redis;
my %hash = $redis->hgetall($id);
$redis->quit();
return %hash;
}

# Builds a JSON object for an archive registered in the database and returns it.
# If you need to get many JSONs at once, use the multi variant.
sub get_archive_json ( $redis, $id ) {
Expand Down Expand Up @@ -187,6 +196,12 @@ sub get_archive_json_multi (@ids) {
return @archives;
}

sub get_tags ($id) {
my %archive_info = get_archive($id);
return undef if ( !%archive_info );
return $archive_info{tags};
}

# Internal function for building an archive JSON.
sub build_json ( $id, %hash ) {

Expand Down
Loading