Fix scanner

This commit is contained in:
Ryan Shpeherd
2026-09-14 16:02:21 -04:00
parent 3eb5b5b08b
commit 639c29c2c4
+56 -32
View File
@@ -9,12 +9,20 @@ use String::ShellQuote qw/shell_quote/;
my $db_host = $ENV{'DB_HOST'} || 'mariadb'; my $db_host = $ENV{'DB_HOST'} || 'mariadb';
my $db_name = $ENV{'DB_NAME'} || 'videodetect'; my $db_name = $ENV{'DB_NAME'} || 'videodetect';
my $db_user = $ENV{'DB_USER'} || 'videodetect'; my $db_user = $ENV{'DB_USER'} || 'videodetect';
my $db_pass = $ENV{'DB_PASSWORD'} || 'videodetect123'; my $db_pass = $ENV{'DB_PASSWORD'} || 'videodetect';
my $do_scan = (defined $ENV{'DO_SCAN'}) ? $ENV{'DO_SCAN'} : 1;
my $do_review = (defined $ENV{'DO_REVIEW'}) ? $ENV{'DO_REVIEW'} : 1;
my $do_delete = (defined $ENV{'DO_DELETE'}) ? $ENV{'DO_DELETE'} : 1;
$SIG{INT} = sub { die "Interrupted\n"; }; $SIG{INT} = sub { die "Interrupted\n"; };
my $dbh = DBI->connect("DBI:mysql:database=$db_name;host=$db_host", $db_user, $db_pass); my $dbh = DBI->connect(
"DBI:mysql:database=$db_name;host=$db_host;mysql_enable_utf8=1",
$db_user,
$db_pass,
{ mysql_auto_reconnect => 1 }
);
my @video_extensions = qw(mp4 mkv avi mov flv wmv mpg mpeg webm m4v); my @video_extensions = qw(mp4 mkv avi mov flv wmv mpg mpeg webm m4v);
my %ignored_extensions; my %ignored_extensions;
@@ -28,29 +36,36 @@ my $row = $sth->fetchrow_hashref();
my $now = $row->{now}; my $now = $row->{now};
$sth->finish(); $sth->finish();
while(my $dir = shift @dir_queue) { if($do_scan) {
opendir(my $dh, $dir) or die "Cannot open directory $dir: $!"; while(my $dir = shift @dir_queue) {
print "$dir\n"; opendir(my $dh, $dir) or die "Cannot open directory $dir: $!";
while (my $file = readdir($dh)) { print "$dir\n";
next if ($file eq '.' || $file eq '..'); while (my $file = readdir($dh)) {
my $full_path = "$dir/$file"; next if ($file eq '.' || $file eq '..');
if (-d $full_path) { my $full_path = "$dir/$file";
unshift @dir_queue, $full_path; if (-d $full_path) {
} elsif (-f $full_path) { unshift @dir_queue, $full_path;
process_file($full_path); } elsif (-f $full_path) {
process_file($full_path);
}
} }
closedir($dh);
}
for my $ext (sort { $ignored_extensions{$b} <=> $ignored_extensions{$a} } keys %ignored_extensions) {
print "Ignored extension $ext: $ignored_extensions{$ext} times\n";
} }
closedir($dh);
} }
# Any entry where last_scan_time < $now is considered deleted or moved, so we can mark them as such if($do_scan && $do_delete) {
my $sth_delete = $dbh->prepare("DELETE FROM videos WHERE last_scan_time < ? OR last_scan_time IS NULL"); # Any entry where last_scan_time < $now is considered deleted or moved, so we can mark them as such
$sth_delete->execute($now); my $sth_delete = $dbh->prepare("DELETE FROM videos WHERE last_scan_time < ? OR last_scan_time IS NULL");
for my $ext (sort { $ignored_extensions{$b} <=> $ignored_extensions{$a} } keys %ignored_extensions) { $sth_delete->execute($now);
print "Ignored extension $ext: $ignored_extensions{$ext} times\n";
} }
CreateReviewTasks(); if($do_review) {
print "Scanning complete. Creating review tasks for completed AISCAN tasks.\n";
CreateReviewTasks();
}
sub process_file { sub process_file {
my ($file_path) = @_; my ($file_path) = @_;
@@ -177,25 +192,34 @@ sub get_video_info {
} }
sub CreateReviewTasks { sub CreateReviewTasks {
# Fetch all completed AISCAN tasks and filter in Perl.
# This avoids MariaDB's inconsistent JSON functions in SQL WHERE clauses,
# which behave differently between CLI client and DBI connections.
my $sth = $dbh->prepare(" my $sth = $dbh->prepare("
SELECT * SELECT id, video_id, results
FROM tasks FROM tasks
WHERE task_type='AISCAN' WHERE task_type='AISCAN' AND status='COMPLETED'
AND status='COMPLETED'
AND JSON_VALUE(results, '$.verdict')=1
AND JSON_VALUE(results, '$.reviewed') IS NULL
");
my $sth_update = $dbh->prepare("
UPDATE tasks
SET results = JSON_SET(results, '$.reviewed', 1)
WHERE id = ?
"); ");
$sth->execute(); $sth->execute();
while (my $row = $sth->fetchrow_hashref) { while (my $row = $sth->fetchrow_hashref()) {
my $results = decode_json($row->{results});
next unless $results && ref($results) eq 'HASH';
next unless $results->{verdict} == 1;
next if exists $results->{reviewed};
print "Creating REVIEW task for video_id: $row->{video_id}\n";
create_task($row->{video_id}, 'REVIEW'); create_task($row->{video_id}, 'REVIEW');
$sth_update->execute($row->{id});
# Re-encode the whole JSON blob in Perl and update it.
# This avoids MariaDB JSON_SET path parsing issues through DBI connections.
$results->{reviewed} = 1;
my $updated = encode_json($results);
my $sth_update = $dbh->prepare(
"UPDATE tasks SET results = ? WHERE id = ?"
);
$sth_update->execute($updated, $row->{id});
$sth_update->finish();
} }
$sth->finish(); $sth->finish();
$sth_update->finish();
} }