File: [local] / openbsd / OpenBSDTorrents / CurrentTorrents.pl (download)
Revision 1.12, Mon May 2 20:27:00 2005 UTC (19 years, 2 months ago) by andrew
Branch: MAIN
Changes since 1.11: +10 -2 lines
changed it so we can pass a regex on the commandline to only check certain
files
|
#!/usr/bin/perl -T
#$Id: CurrentTorrents.pl,v 1.12 2005/05/02 20:27:00 andrew Exp $
use strict;
use warnings;
use diagnostics;
use BT::MetaInfo;
use Time::Local;
use lib 'lib';
use OpenBSDTorrents;
%ENV = ();
#use YAML;
justme();
my $Name_Filter = shift || '';
if ($Name_Filter =~ /^(\w*)$/) {
$Name_Filter = $1;
} else {
die "Invalid filter: $Name_Filter";
}
my %files;
opendir DIR, $OBT->{DIR_TORRENT}
or die "Couldn't opendir $OBT->{DIR_TORRENT}: $!";
foreach (readdir DIR) {
if (/^([^\/]+)$/) {
$_ = $1;
} else {
die "Invalid character in $_: $!";
}
next unless /\.torrent$/;
my ($name, $year, $mon, $mday, $hour, $min) =
/^(.*)-(\d{4})-(\d{2})-(\d{2})-(\d{2})(\d{2})/;
$mon--;
my $epoch = timegm(0,$min,$hour,$mday,$mon,$year);
#print "Adding $_\n";
$files{$name}{$epoch} = {
file => $_,
year => $year,
mon => $mon,
mday => $mday,
hour => $hour,
min => $min,
epoch => $epoch,
};
}
closedir DIR;
my %keep;
my @delete;
foreach my $name (keys %files) {
next unless $name =~ /^$Name_Filter/;
print "Checking $name\n";
foreach my $epoch ( sort { $b <=> $a } keys %{ $files{$name} } ) {
#print "\t$epoch\n";
my $torrent = $files{$name}{$epoch}{file};
my $t;
eval { $t = BT::MetaInfo->new($OBT->{DIR_TORRENT} . "/$torrent"); };
if ($@) {
warn "Error reading torrent $torrent\n";
next;
}
$files{$name}{$epoch}{comment} = $t->{comment};
my ($path) = $t->{comment} =~ /Files from ([^\n]+)\n/s;
unless (-d $OBT->{DIR_FTP} . "/$path") {
#print "Deleting $files{$name}{$epoch}{file} the path doesn't exist.\n";
push @delete, $files{$name}{$epoch}{file};
}
if (keys %{ $files{$name} } == 1) {
#print "Skipping torrent for $name there is only one.\n";
next;
}
my $hash = $t->info_hash;
$hash = unpack("H*", $hash);
$files{$name}{$epoch}{info_hash} = $hash;
undef $t;
if (exists $keep{$name}) {
if (exists $keep{$name}{$hash}) {
push @delete, $keep{$name}{$hash};
$keep{$name}{$hash} =
$files{$name}{$epoch}{file};
} else {
push @delete, $files{$name}{$epoch}{file};
}
} else {
$keep{$name}{$hash} =
$files{$name}{$epoch}{file};
}
}
}
#print Dump \%files, \%keep, \@delete;
foreach (@delete) {
print "Deleting '$_'\n";
unlink $OBT->{DIR_TORRENT} . "/$_" or die "Couldn't unlink $_";
}