version 1.16, 2005/05/05 02:09:43 |
version 1.37, 2010/03/16 23:40:18 |
|
|
#!/usr/bin/perl -T |
#!/usr/bin/perl -T |
#$Id$ |
#$RedRiver: CurrentTorrents.pl,v 1.36 2010/03/16 22:37:32 andrew Exp $ |
use strict; |
use strict; |
use warnings; |
use warnings; |
use diagnostics; |
use diagnostics; |
|
|
use Time::Local; |
use Time::Local; |
use Fcntl ':flock'; |
use Fcntl ':flock'; |
|
use File::Basename; |
|
|
|
use Transmission::Client; |
|
use Transmission::Utils; |
|
|
|
#use YAML; |
|
|
use lib 'lib'; |
use lib 'lib'; |
use OpenBSDTorrents; |
use OpenBSDTorrents; |
use BT::OBTMetaInfo; |
use BT::MetaInfo::Cached; |
|
|
%ENV = (); |
%ENV = (); |
|
|
use YAML; |
|
|
|
#justme(); |
#justme(); |
|
|
my $Name_Filter = shift || ''; |
my $Name_Filter = shift || ''; |
if ($Name_Filter =~ /^(\w*)$/) { |
if ( $Name_Filter =~ /^(\w*)$/ ) { |
$Name_Filter = $1; |
$Name_Filter = $1; |
} else { |
|
die "Invalid filter: $Name_Filter"; |
|
} |
} |
|
else { |
|
die "Invalid filter: $Name_Filter"; |
|
} |
|
|
my %Possible_Torrents; |
my %Possible_Torrents; |
Process_Dir($OBT->{DIR_FTP}); |
Process_Dir( $OBT->{DIR_FTP} ); |
|
|
my %files; |
my %files; |
my %keep; |
|
my @delete; |
my @delete; |
foreach my $DIR ($OBT->{DIR_NEW_TORRENT}, $OBT->{DIR_TORRENT}) { |
foreach my $DIR ( $OBT->{DIR_NEW_TORRENT}, $OBT->{DIR_TORRENT} ) { |
opendir DIR, $DIR |
opendir DIR, $DIR |
or die "Couldn't opendir $DIR: $!"; |
or die "Couldn't opendir $DIR: $!"; |
foreach (readdir DIR) { |
foreach ( readdir DIR ) { |
next unless my ($ext) = /\.(torrent|$OBT->{META_EXT})$/; |
next unless my ($ext) = /\.(torrent|$OBT->{META_EXT})$/; |
|
|
if (/^([^\/]+)$/) { |
if (/^([^\/]+)$/) { |
$_ = $1; |
$_ = $1; |
} else { |
} |
die "Invalid character in $_: $!"; |
else { |
} |
die "Invalid character in $_: $!"; |
my ($name, $year, $mon, $mday, $hour, $min) = |
} |
/^(.*)-(\d{4})-(\d{2})-(\d{2})-(\d{2})(\d{2})/; |
my $epoch = 0; |
|
my $name = basename( $_, '.torrent' ); |
|
|
$mon--; |
if ( my ( $base, $year, $mon, $mday, $hour, $min ) |
my $epoch = timegm(0,$min,$hour,$mday,$mon,$year); |
= /^(.*)-(\d{4})-(\d{2})-(\d{2})-(\d{2})(\d{2})/ ) |
|
{ |
|
|
#print "Adding $_\n"; |
$mon--; |
|
$epoch = timegm( 0, $min, $hour, $mday, $mon, $year ); |
|
$name = $base; |
|
} |
|
|
$files{$ext}{$name}{$epoch} = { |
#print "Adding $DIR/$_\n"; |
file => $_, |
|
dir => $DIR, |
|
path => "$DIR/$_", |
|
ext => $ext, |
|
year => $year, |
|
mon => $mon, |
|
mday => $mday, |
|
hour => $hour, |
|
min => $min, |
|
name => $name, |
|
epoch => $epoch, |
|
}; |
|
|
|
unless (exists $Possible_Torrents{$name}) { |
if (exists $files{$ext}{$name}{$epoch}) { |
print "Would remove $_\n"; |
warn "Multiple torrents with $name and epoch $epoch\n"; |
push @delete, $files{$ext}{$name}{$epoch}; |
push @delete, $files{$ext}{$name}{$epoch}; |
} |
} |
} |
|
closedir DIR; |
|
} |
|
|
|
foreach my $name (keys %{ $files{torrent} }) { |
$files{$ext}{$name}{$epoch} = { |
next unless $name =~ /^$Name_Filter/; |
file => $_, |
print "Checking $name\n"; |
dir => $DIR, |
|
#path => "$DIR/$_", |
|
ext => $ext, |
|
|
foreach my $epoch ( sort { $b <=> $a } keys %{ $files{torrent}{$name} } ) { |
#year => $year, |
#print "\t$epoch\n"; |
#mon => $mon, |
my $torrent = $files{torrent}{$name}{$epoch}{path}; |
#mday => $mday, |
|
#hour => $hour, |
|
#min => $min, |
|
name => $name, |
|
epoch => $epoch, |
|
}; |
|
|
if ( |
if ( $name =~ m/\A $OBT->{BASENAME} /xms |
keys %{ $files{torrent}{$name} } == 1 && |
&& !exists $Possible_Torrents{$name} ) |
$files{torrent}{$name}{$epoch}{dir} |
{ |
eq $OBT->{DIR_TORRENT} |
#print "Would remove $_\n"; |
) { |
push @delete, $files{$ext}{$name}{$epoch}; |
#print "Skipping torrent for $name there is only one.\n"; |
} |
next; |
} |
} |
closedir DIR; |
|
} |
|
|
my $meta_file = $torrent; |
#print Dump \%files; |
$meta_file =~ s/\.torrent$/.$OBT->{META_EXT}/; |
|
|
|
my $hash = undef; |
my %keep; |
if (-e $meta_file) { |
my %seen; |
#print "Reading meta file: $meta_file\n"; |
foreach my $name ( sort keys %{ $files{torrent} } ) { |
open my $meta, $meta_file |
next unless $name =~ /^$Name_Filter/; |
or die "Couldn't open $meta_file: $!"; |
|
flock($meta, LOCK_SH); |
|
binmode $meta; |
|
|
|
$hash = do { local $/; <$meta> }; |
#next if $name !~ /songs/xms; |
|
#next if $name =~ /_packages_/xms; |
|
#print "Checking $name\n"; |
|
|
flock($meta, LOCK_UN); |
my $cn = $files{torrent}{$name}; |
close $meta; |
|
} else { |
|
|
|
my $t; |
EPOCH: foreach my $epoch ( sort { $b <=> $a } keys %{$cn} ) { |
eval { $t = BT::OBTMetaInfo->new( $torrent ); }; |
my $ct = $cn->{$epoch}; |
|
my $cf = $ct->{dir} . '/' . $ct->{file}; |
|
|
if ($@) { |
#print "\t$epoch - $cf\n"; |
warn "Error reading torrent $torrent\n"; |
|
next; |
|
} |
|
|
|
$files{torrent}{$name}{$epoch}{comment} = $t->{comment}; |
my $t; |
my ($path) = $t->{comment} =~ /Files from ([^\n]+)\n/s; |
eval { |
|
$t |
|
= BT::MetaInfo::Cached->new( $cf, |
|
{ cache_root => '/tmp/OBTFileCache' } ); |
|
}; |
|
|
unless (-d $OBT->{DIR_FTP} . "/$path") { |
if ($@) { |
#print "Deleting $files{torrent}{$name}{$epoch}{file} the path doesn't exist.\n"; |
warn "Error reading torrent $cf\n"; |
push @delete, $files{torrent}{$name}{$epoch}; |
push @delete, $ct; |
delete $files{torrent}{$name}{$epoch}; |
next EPOCH; |
next; |
} |
} |
|
|
|
$hash = $t->info_hash_cached($torrent); |
$ct->{comment} = $t->{comment}; |
$hash = unpack("H*", $hash); |
my ($path) = $t->{comment} =~ /($OBT->{BASENAME}\/[^\n]+)\n/s; |
|
|
undef $t; |
if ( !-e $OBT->{DIR_FTP} . "/$path" ) { |
|
print |
|
'Deleting ', |
|
$cn->{$epoch}{file}, ' the path (', $path, |
|
") doesn't exist.\n"; |
|
push @delete, $ct; |
|
next EPOCH; |
|
} |
|
|
} |
my $hash = unpack( "H*", $t->info_hash ); |
|
$ct->{info_hash} = $hash; |
|
|
$files{torrent}{$name}{$epoch}{info_hash} = $hash; |
undef $t; |
|
|
|
if ( $seen{$name} && $seen{$name} ne $hash ) { |
|
print "Removing older [$name] [$hash]\n\t", |
|
$cf, |
|
"\n"; |
|
$ct->{reason} = 'older'; |
|
push @delete, $ct; |
|
next EPOCH; |
|
} |
|
elsif ( keys %{$cn} == 1 && $ct->{dir} eq $OBT->{DIR_TORRENT} ) { |
|
$ct->{reason} = 'only'; |
|
} |
|
elsif ( $keep{$hash} ) { |
|
if ( $keep{$hash}{epoch} == $epoch ) { |
|
next EPOCH; |
|
} |
|
|
if (exists $keep{$name}) { |
print "Removing duplicate [$name] [$hash]\n\t", |
if (exists $keep{$name}{$hash}) { |
$keep{$hash}{file}, "\n"; |
push @delete, $keep{$name}{$hash}; |
|
delete $files{torrent}{ |
|
$keep{$name}{$hash}{name} |
|
}{ |
|
$keep{$name}{$hash}{epoch} |
|
}; |
|
$keep{$name}{$hash} = |
|
$files{torrent}{$name}{$epoch}; |
|
} else { |
|
push @delete, $files{torrent}{$name}{$epoch}; |
|
delete $files{torrent}{$name}{$epoch}; |
|
} |
|
} else { |
|
$keep{$name}{$hash} = |
|
$files{torrent}{$name}{$epoch}; |
|
|
|
} |
$keep{$hash}{reason} = 'duplicate'; |
} |
$ct->{reason} = 'duplicate'; |
|
|
|
push @delete, $keep{$hash}; |
|
} |
|
else { |
|
$ct->{reason} = 'first'; |
|
} |
|
|
|
$keep{$hash} = $ct; |
|
$seen{$name} = $hash; |
|
} |
} |
} |
|
|
#print Dump \%files, \%keep, \@delete; |
#print Dump \%files, \%keep, \@delete; |
|
#print Dump \%keep, \@delete; |
|
#exit; |
|
|
foreach (@delete) { |
my $client = Transmission::Client->new; |
print "Deleting '$_->{path}'\n"; |
my %seeding; |
unlink $_->{path} or die "Couldn't unlink $_->{path}"; |
foreach my $torrent ( @{ $client->torrents } ) { |
} |
|
|
|
foreach my $name (keys %{ $files{$OBT->{META_EXT} } }) { |
#my $status = Transmission::Utils::from_numeric_status($torrent->status); |
foreach my $epoch (keys %{ $files{ $OBT->{META_EXT} }{$name} }) { |
my $hash = $torrent->hash_string; |
unless ( exists $files{torrent}{$name}{$epoch} ) { |
if ( exists $keep{$hash} ) { |
my $path = $files{$OBT->{META_EXT}}{$name}{$epoch}{path}; |
$seeding{$hash} = $torrent; |
print "Unlinking '$path'\n"; |
} |
unlink $path or die "couldn't unlink '$path': $!"; |
else { |
} |
print "No longer seeding [$hash]\n"; |
} |
$torrent->stop or warn $torrent->error_string; |
|
$client->remove( $torrent->id ) or warn $client->error; |
|
} |
} |
} |
|
|
|
|
#print Dump \%keep; |
#print Dump \%keep; |
foreach my $name (keys %keep) { |
foreach my $hash ( keys %keep ) { |
foreach my $hash (keys %{ $keep{$name} }) { |
my $file = $keep{$hash}{file} || q{}; |
my $file = $keep{$name}{$hash}{file}; |
my $dir = $keep{$hash}{dir} || q{}; |
my $dir = $keep{$name}{$hash}{dir }; |
|
if ($dir eq $OBT->{DIR_NEW_TORRENT}) { |
|
print "Moving $file to current torrents\n"; |
|
rename("$dir/$file", $OBT->{DIR_TORRENT} . "/" . $file) |
|
or die "Couldn't rename '$file': $!"; |
|
|
|
my $name = $keep{$name}{$hash}{name}; |
my $name = $keep{$hash}{name}; |
my $epoch = $keep{$name}{$hash}{epoch}; |
my $epoch = $keep{$hash}{epoch}; |
|
my $reason = $keep{$hash}{reason} ? $keep{$hash}{reason} . q{ } : q{}; |
|
|
if (exists $files{txt}{$name}{$epoch}) { |
#if ($reason && $reason ne 'only') { |
my $m_file = $files{txt}{$name}{$epoch}{file}; |
# print "Keeping $reason instance of [$file] [$hash]\n", |
my $m_dir = $files{txt}{$name}{$epoch}{dir }; |
# "\t", $file, "\n"; |
rename( |
#} |
"$m_dir/$m_file", |
|
$OBT->{DIR_TORRENT} . "/" . $m_file |
if ( $dir eq $OBT->{DIR_NEW_TORRENT} ) { |
) or die "Couldn't rename '$m_file': $!"; |
print "Moving $file to current torrents\n"; |
} |
rename( "$dir/$file", $OBT->{DIR_TORRENT} . "/" . $file ) |
} |
or die "Couldn't rename '$file': $!"; |
} |
|
|
$dir = $OBT->{DIR_TORRENT}; |
|
$keep{$hash}{dir} = $dir; |
|
|
|
if ( exists $files{txt}{$name}{$epoch} ) { |
|
my $m_file = $files{txt}{$name}{$epoch}{file}; |
|
my $m_dir = $files{txt}{$name}{$epoch}{dir}; |
|
rename( "$m_dir/$m_file", $OBT->{DIR_TORRENT} . "/" . $m_file ) |
|
or die "Couldn't rename '$m_file': $!"; |
|
$files{txt}{$name}{$epoch}{dir} = $OBT->{DIR_TORRENT}; |
|
} |
|
} |
|
|
|
if ( !$seeding{$hash} ) { |
|
print 'Starting seed of ' . $reason . "[$file] [$hash]\n"; |
|
if (!$client->add( |
|
filename => "$dir/$file", |
|
download_dir => $OBT->{DIR_FTP}, |
|
) |
|
) |
|
{ |
|
|
|
#warn $client->error, ": $dir/$file\n"; |
|
print "Removing invalid torrent\n\t", $keep{$hash}{file}, "\n"; |
|
push @delete, $keep{$hash}; |
|
} |
|
} |
} |
} |
|
|
sub Process_Dir |
foreach (@delete) { |
{ |
my $path = $_->{dir} . '/' . $_->{file}; |
my $basedir = shift; |
if ( -e $path ) { |
|
print "Deleting '$path'\n"; |
|
unlink $path or die "Couldn't delete $path"; |
|
delete $files{torrent}{ $_->{name} }{ $_->{epoch} }; |
|
} |
|
else { |
|
use Data::Dumper; |
|
print Dumper $_; |
|
} |
|
} |
|
|
my ($dirs, $files) = Get_Files_and_Dirs($basedir); |
foreach my $name ( keys %{ $files{ $OBT->{META_EXT} } } ) { |
if (@$files) { |
foreach my $epoch ( keys %{ $files{ $OBT->{META_EXT} }{$name} } ) { |
my $dir = $basedir; |
unless ( exists $files{torrent}{$name}{$epoch} ) { |
$dir =~ s/^$OBT->{DIR_FTP}\///; |
my $path = $files{ $OBT->{META_EXT} }{$name}{$epoch}{dir} |
my $torrent = Name_Torrent($dir); |
. '/' |
$torrent =~ s/-.*$//; |
. $files{ $OBT->{META_EXT} }{$name}{$epoch}{file}; |
$Possible_Torrents{$torrent} = 1; |
|
|
print "Unlinking '$path'\n"; |
|
unlink $path or die "couldn't unlink '$path': $!"; |
} |
} |
|
} |
|
} |
|
|
foreach my $subdir (@$dirs) { |
$client->start; |
next if $subdir eq '.'; |
|
next if $subdir eq '..'; |
sub Process_Dir { |
Process_Dir("$basedir/$subdir") |
my $basedir = shift; |
|
|
|
my ( $dirs, $files ) = Get_Files_and_Dirs($basedir); |
|
if (@$files) { |
|
my $dir = $basedir; |
|
$dir =~ s/^$OBT->{DIR_FTP}\///; |
|
Make_Possible($dir); |
|
foreach my $file (@$files) { |
|
if ( $file =~ /$INSTALL_ISO_REGEX/ ) { |
|
Make_Possible("$dir/$file"); |
|
} |
} |
} |
|
} |
|
|
|
foreach my $subdir (@$dirs) { |
|
next if $subdir eq '.'; |
|
next if $subdir eq '..'; |
|
Process_Dir("$basedir/$subdir"); |
|
} |
} |
} |
|
|
|
sub Make_Possible { |
|
my ($path) = @_; |
|
|
|
my $torrent = Name_Torrent($path); |
|
$torrent =~ s/-.*$//; |
|
$Possible_Torrents{$torrent} = 1; |
|
|
|
return $torrent; |
|
} |