Compare commits

...

2 Commits

View File

@@ -17,11 +17,6 @@ use URI::Escape;
# --- Environment Variables ---
use constant LIDARR_BASE => $ENV{LIDARR_BASE} || 'http://localhost:8686';
# ANSI escape sequences
sub ERASE_EOL { return "\033[K"; }
sub CURSOR_BACK { return "\033[${_[0]}D"; }
sub STATUS { return $_[0] . ERASE_EOL . CURSOR_BACK(length($_[0])) }
getopts('bfhv');
my $fresh_result = defined $::opt_f ? $::opt_f : 0; # vs STALE
@@ -96,7 +91,7 @@ foreach my $id (@ids_to_process) {
LIDARR_BASE . "/add/search?term=" . uri_escape("lidarr:$id") . "\n";
}
## subroutines (ping_api and validate remain the same as your original script)
## subroutines
sub ping_api {
my ($type, $id) = @_;
@@ -110,24 +105,34 @@ sub ping_api {
while (1) {
$loops++;
print STDERR STATUS("- attempt $loops");
print STDERR "- attempt $loops...\n";
my $res = $ua->get($api);
my $status = $res->code;
if ($res->is_success) {
eval {
$json = decode_json($res->content);
};
if ($@) {
chomp $@;
warn "Retrying: $@\n";
warn "Retrying: JSON decode failed: $@\n";
} elsif ($fresh_result and $res->header('cf-cache-status') eq 'STALE') {
$status .= ' STALE';
warn "Retrying: Response is STALE ($status)\n";
} else {
print STDERR ERASE_EOL;
last;
# *** VALIDATION CHECK ***
my @validation_warnings = validate($type, $json);
if (@validation_warnings) {
warn "Retrying: Validation failed: " . join(', ', @validation_warnings) . "\n";
} else {
# Success: HTTP 2xx, JSON parsed, and content passed validation
last;
}
}
} else {
# Log failure if not success and retry
warn "Retrying: HTTP failed with status $status\n";
}
print STDERR STATUS("- attempt $loops: $status");
sleep 5;
}
@@ -148,6 +153,9 @@ sub validate {
my @warnings;
if ($type eq 'artist') {
# make sure there are albums
unless (exists $json->{Albums} and scalar @{$json->{Albums}}) {
unless (exists $json->{albums} and scalar @{$json->{albums}}) {
push(@warnings, 'no albums');
}
}
}
return @warnings;
}