version 1.27, 2010/01/05 19:43:44 |
version 1.37, 2010/03/16 23:40:18 |
|
|
#!/usr/bin/perl -T |
#!/usr/bin/perl -T |
#$RedRiver: CurrentTorrents.pl,v 1.26 2009/10/20 19:04:28 andrew Exp $ |
#$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 Fcntl ':flock'; |
use Fcntl ':flock'; |
use File::Basename; |
use File::Basename; |
|
|
|
use Transmission::Client; |
|
use Transmission::Utils; |
|
|
#use YAML; |
#use YAML; |
|
|
use lib 'lib'; |
use lib 'lib'; |
|
|
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 |
|
|
$name = $base; |
$name = $base; |
} |
} |
|
|
#print "Adding $_\n"; |
#print "Adding $DIR/$_\n"; |
|
|
|
if (exists $files{$ext}{$name}{$epoch}) { |
|
warn "Multiple torrents with $name and epoch $epoch\n"; |
|
push @delete, $files{$ext}{$name}{$epoch}; |
|
} |
|
|
$files{$ext}{$name}{$epoch} = { |
$files{$ext}{$name}{$epoch} = { |
file => $_, |
file => $_, |
dir => $DIR, |
dir => $DIR, |
path => "$DIR/$_", |
#path => "$DIR/$_", |
ext => $ext, |
ext => $ext, |
|
|
#year => $year, |
#year => $year, |
|
|
if ( $name =~ m/\A $OBT->{BASENAME} /xms |
if ( $name =~ m/\A $OBT->{BASENAME} /xms |
&& !exists $Possible_Torrents{$name} ) |
&& !exists $Possible_Torrents{$name} ) |
{ |
{ |
print "Would remove $_\n"; |
#print "Would remove $_\n"; |
push @delete, $files{$ext}{$name}{$epoch}; |
push @delete, $files{$ext}{$name}{$epoch}; |
} |
} |
} |
} |
|
|
} |
} |
|
|
#print Dump \%files; |
#print Dump \%files; |
|
|
|
my %keep; |
|
my %seen; |
foreach my $name ( sort keys %{ $files{torrent} } ) { |
foreach my $name ( sort keys %{ $files{torrent} } ) { |
next unless $name =~ /^$Name_Filter/; |
next unless $name =~ /^$Name_Filter/; |
|
|
|
#next if $name !~ /songs/xms; |
|
#next if $name =~ /_packages_/xms; |
#print "Checking $name\n"; |
#print "Checking $name\n"; |
|
|
foreach my $epoch ( sort { $b <=> $a } keys %{ $files{torrent}{$name} } ) |
my $cn = $files{torrent}{$name}; |
{ |
|
|
|
#print "\t$epoch\n"; |
EPOCH: foreach my $epoch ( sort { $b <=> $a } keys %{$cn} ) { |
my $torrent = $files{torrent}{$name}{$epoch}{path}; |
my $ct = $cn->{$epoch}; |
|
my $cf = $ct->{dir} . '/' . $ct->{file}; |
|
|
if ( keys %{ $files{torrent}{$name} } == 1 |
#print "\t$epoch - $cf\n"; |
&& $files{torrent}{$name}{$epoch}{dir} eq $OBT->{DIR_TORRENT} ) |
|
{ |
|
|
|
#print "Skipping torrent for $name there is only one.\n"; |
|
next; |
|
} |
|
|
|
my $t; |
my $t; |
eval { |
eval { |
$t = BT::MetaInfo::Cached->new( |
$t |
$torrent, |
= BT::MetaInfo::Cached->new( $cf, |
{ cache_root => '/tmp/OBTFileCache' |
{ cache_root => '/tmp/OBTFileCache' } ); |
|
|
#$OBT->{DIR_HOME} . '/FileCache' |
|
} |
|
); |
|
}; |
}; |
|
|
if ($@) { |
if ($@) { |
warn "Error reading torrent $torrent\n"; |
warn "Error reading torrent $cf\n"; |
push @delete, $files{torrent}{$name}{$epoch}; |
push @delete, $ct; |
delete $files{torrent}{$name}{$epoch}; |
next EPOCH; |
next; |
|
} |
} |
|
|
$files{torrent}{$name}{$epoch}{comment} = $t->{comment}; |
$ct->{comment} = $t->{comment}; |
my ($path) = $t->{comment} =~ /($OBT->{BASENAME}\/[^\n]+)\n/s; |
my ($path) = $t->{comment} =~ /($OBT->{BASENAME}\/[^\n]+)\n/s; |
|
|
unless ( -e $OBT->{DIR_FTP} . "/$path" ) { |
if ( !-e $OBT->{DIR_FTP} . "/$path" ) { |
print |
print |
"Deleting $files{torrent}{$name}{$epoch}{file} the path ($path) doesn't exist.\n"; |
'Deleting ', |
push @delete, $files{torrent}{$name}{$epoch}; |
$cn->{$epoch}{file}, ' the path (', $path, |
delete $files{torrent}{$name}{$epoch}; |
") doesn't exist.\n"; |
next; |
push @delete, $ct; |
|
next EPOCH; |
} |
} |
|
|
my $hash = $t->info_hash; |
my $hash = unpack( "H*", $t->info_hash ); |
$hash = unpack( "H*", $hash ); |
$ct->{info_hash} = $hash; |
|
|
undef $t; |
undef $t; |
|
|
$files{torrent}{$name}{$epoch}{info_hash} = $hash; |
if ( $seen{$name} && $seen{$name} ne $hash ) { |
|
print "Removing older [$name] [$hash]\n\t", |
if ( exists $keep{$name} ) { |
$cf, |
if ( exists $keep{$name}{$hash} ) { |
"\n"; |
if ( $keep{$name}{$hash}{epoch} == $epoch ) { |
$ct->{reason} = 'older'; |
next; |
push @delete, $ct; |
} |
next EPOCH; |
print "Removing [$name] [$hash]\n\t", |
} |
$keep{$name}{$hash}{path}, |
elsif ( keys %{$cn} == 1 && $ct->{dir} eq $OBT->{DIR_TORRENT} ) { |
"\n"; |
$ct->{reason} = 'only'; |
push @delete, $keep{$name}{$hash}; |
} |
delete $files{torrent}{ $keep{$name}{$hash}{name} } |
elsif ( $keep{$hash} ) { |
{ $keep{$name}{$hash}{epoch} }; |
if ( $keep{$hash}{epoch} == $epoch ) { |
$keep{$name}{$hash} = $files{torrent}{$name}{$epoch}; |
next EPOCH; |
print "Keeping additional instance of [$name] [$hash]\n\t", |
|
$keep{$name}{$hash}{path}, |
|
"\n"; |
|
} |
} |
else { |
|
print "Removing old [$name] [$hash]\n"; |
print "Removing duplicate [$name] [$hash]\n\t", |
if ( $keep{$name}{$hash}{path} ) { |
$keep{$hash}{file}, "\n"; |
print "\t", $keep{$name}{$hash}{path}, "\n"; |
|
} |
$keep{$hash}{reason} = 'duplicate'; |
push @delete, $files{torrent}{$name}{$epoch}; |
$ct->{reason} = 'duplicate'; |
delete $files{torrent}{$name}{$epoch}; |
|
} |
push @delete, $keep{$hash}; |
} |
} |
else { |
else { |
print "Keeping first instance of $name [$hash]\n\t", |
$ct->{reason} = 'first'; |
$files{torrent}{$name}{$epoch}{path}, |
|
"\n"; |
|
$keep{$name}{$hash} = $files{torrent}{$name}{$epoch}; |
|
|
|
} |
} |
|
|
|
$keep{$hash} = $ct; |
|
$seen{$name} = $hash; |
} |
} |
} |
} |
|
|
#print Dump \%files, \%keep, \@delete; |
#print Dump \%files, \%keep, \@delete; |
|
#print Dump \%keep, \@delete; |
#exit; |
#exit; |
|
|
|
my $client = Transmission::Client->new; |
|
my %seeding; |
|
foreach my $torrent ( @{ $client->torrents } ) { |
|
|
|
#my $status = Transmission::Utils::from_numeric_status($torrent->status); |
|
my $hash = $torrent->hash_string; |
|
if ( exists $keep{$hash} ) { |
|
$seeding{$hash} = $torrent; |
|
} |
|
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; |
|
foreach my $hash ( keys %keep ) { |
|
my $file = $keep{$hash}{file} || q{}; |
|
my $dir = $keep{$hash}{dir} || q{}; |
|
|
|
my $name = $keep{$hash}{name}; |
|
my $epoch = $keep{$hash}{epoch}; |
|
my $reason = $keep{$hash}{reason} ? $keep{$hash}{reason} . q{ } : q{}; |
|
|
|
#if ($reason && $reason ne 'only') { |
|
# print "Keeping $reason instance of [$file] [$hash]\n", |
|
# "\t", $file, "\n"; |
|
#} |
|
|
|
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': $!"; |
|
|
|
$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}; |
|
} |
|
} |
|
} |
|
|
foreach (@delete) { |
foreach (@delete) { |
print "Deleting '$_->{path}'\n"; |
my $path = $_->{dir} . '/' . $_->{file}; |
unlink $_->{path} or die "Couldn't unlink $_->{path}"; |
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 $_; |
|
} |
} |
} |
|
|
foreach my $name ( keys %{ $files{ $OBT->{META_EXT} } } ) { |
foreach my $name ( keys %{ $files{ $OBT->{META_EXT} } } ) { |
foreach my $epoch ( keys %{ $files{ $OBT->{META_EXT} }{$name} } ) { |
foreach my $epoch ( keys %{ $files{ $OBT->{META_EXT} }{$name} } ) { |
unless ( exists $files{torrent}{$name}{$epoch} ) { |
unless ( exists $files{torrent}{$name}{$epoch} ) { |
my $path = $files{ $OBT->{META_EXT} }{$name}{$epoch}{path}; |
my $path = $files{ $OBT->{META_EXT} }{$name}{$epoch}{dir} |
|
. '/' |
|
. $files{ $OBT->{META_EXT} }{$name}{$epoch}{file}; |
|
|
print "Unlinking '$path'\n"; |
print "Unlinking '$path'\n"; |
unlink $path or die "couldn't unlink '$path': $!"; |
unlink $path or die "couldn't unlink '$path': $!"; |
} |
} |
} |
} |
} |
} |
|
|
#print Dump \%keep; |
$client->start; |
foreach my $name ( keys %keep ) { |
|
foreach my $hash ( keys %{ $keep{$name} } ) { |
|
my $file = $keep{$name}{$hash}{file} || q{}; |
|
my $dir = $keep{$name}{$hash}{dir} || q{}; |
|
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 $epoch = $keep{$name}{$hash}{epoch}; |
|
|
|
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': $!"; |
|
} |
|
} |
|
} |
|
} |
|
|
|
sub Process_Dir { |
sub Process_Dir { |
my $basedir = shift; |
my $basedir = shift; |
|
|
|
|
if (@$files) { |
if (@$files) { |
my $dir = $basedir; |
my $dir = $basedir; |
$dir =~ s/^$OBT->{DIR_FTP}\///; |
$dir =~ s/^$OBT->{DIR_FTP}\///; |
my $torrent = Name_Torrent($dir); |
Make_Possible($dir); |
$torrent =~ s/-.*$//; |
|
$Possible_Torrents{$torrent} = 1; |
|
foreach my $file (@$files) { |
foreach my $file (@$files) { |
if ( $file =~ /$INSTALL_ISO_REGEX/ ) { |
if ( $file =~ /$INSTALL_ISO_REGEX/ ) { |
$torrent = Name_Torrent("$dir/$file"); |
Make_Possible("$dir/$file"); |
$torrent =~ s/-.*$//; |
|
$Possible_Torrents{$torrent} = 1; |
|
} |
} |
} |
} |
} |
} |
|
|
} |
} |
} |
} |
|
|
|
sub Make_Possible { |
|
my ($path) = @_; |
|
|
|
my $torrent = Name_Torrent($path); |
|
$torrent =~ s/-.*$//; |
|
$Possible_Torrents{$torrent} = 1; |
|
|
|
return $torrent; |
|
} |