diff --git a/tools/remove-recordings.pl b/tools/remove-recordings.pl new file mode 100644 index 0000000..f6c58eb --- /dev/null +++ b/tools/remove-recordings.pl @@ -0,0 +1,42 @@ +#! /usr/bin/perl + +use warnings; +use strict; +use Data::Dumper; +use List::Util qw(); + +use FindBin qw($Bin); +use lib "$Bin/../lib/calcms"; +use config; +use time; +use db; +use File::Basename qw(basename); +my $config = config::get(pop @ARGV); +my $delete = grep {$_ eq "--delete"} @ARGV; + +my $dbh = db::connect($config); +my $query = qq{ + select start, path + from calcms_events e, calcms_audio_recordings r + where e.id = r.event_id + and e.start > date_add(now(), INTERVAL -14 DAY) +}; +my $entries = db::get($dbh, $query); +my %paths = map {normalize($_->{path}) => $_->{start}} @$entries; + +my $dir = $config->{locations}->{local_audio_recordings_dir}; +for my $file (sort glob("$dir/*m4a")) { + next if -M $file < 14; + my $filename = normalize($file); + unless (exists $paths{$filename}) { + print " --- $filename\n"; + unlink $file or die $! if $delete; + } +} + +sub normalize { + my $s = shift; + $s = basename $s; + $s =~ s/\.master(\.\w+)$/$1/; + return $s; +}