=================================================================== RCS file: /cvs/openbsd/OpenBSDTorrents/CurrentTorrents.pl,v retrieving revision 1.27 retrieving revision 1.31 diff -u -r1.27 -r1.31 --- openbsd/OpenBSDTorrents/CurrentTorrents.pl 2010/01/05 19:43:44 1.27 +++ openbsd/OpenBSDTorrents/CurrentTorrents.pl 2010/02/26 22:58:20 1.31 @@ -1,5 +1,5 @@ #!/usr/bin/perl -T -#$RedRiver: CurrentTorrents.pl,v 1.26 2009/10/20 19:04:28 andrew Exp $ +#$RedRiver: CurrentTorrents.pl,v 1.30 2010/02/25 18:03:01 andrew Exp $ use strict; use warnings; use diagnostics; @@ -8,6 +8,9 @@ use Fcntl ':flock'; use File::Basename; +use Transmission::Client; +use Transmission::Utils; + #use YAML; use lib 'lib'; @@ -30,7 +33,6 @@ Process_Dir( $OBT->{DIR_FTP} ); my %files; -my %keep; my @delete; foreach my $DIR ( $OBT->{DIR_NEW_TORRENT}, $OBT->{DIR_TORRENT} ) { opendir DIR, $DIR @@ -76,7 +78,7 @@ if ( $name =~ m/\A $OBT->{BASENAME} /xms && !exists $Possible_Torrents{$name} ) { - print "Would remove $_\n"; + #print "Would remove $_\n"; push @delete, $files{$ext}{$name}{$epoch}; } } @@ -84,102 +86,162 @@ } #print Dump \%files; + +my %keep; +my %seen; foreach my $name ( sort keys %{ $files{torrent} } ) { next unless $name =~ /^$Name_Filter/; + #next if $name !~ /songs/xms; + #next if $name =~ /_packages_/xms; #print "Checking $name\n"; - foreach my $epoch ( sort { $b <=> $a } keys %{ $files{torrent}{$name} } ) - { + my $cn = $files{torrent}{$name}; - #print "\t$epoch\n"; - my $torrent = $files{torrent}{$name}{$epoch}{path}; +EPOCH: foreach my $epoch ( sort { $b <=> $a } keys %{$cn} ) { + my $ct = $cn->{$epoch}; + my $cf = $ct->{path}; - if ( keys %{ $files{torrent}{$name} } == 1 - && $files{torrent}{$name}{$epoch}{dir} eq $OBT->{DIR_TORRENT} ) - { + #print "\t$epoch - $cf\n"; - #print "Skipping torrent for $name there is only one.\n"; - next; - } - my $t; eval { - $t = BT::MetaInfo::Cached->new( - $torrent, - { cache_root => '/tmp/OBTFileCache' - - #$OBT->{DIR_HOME} . '/FileCache' - } - ); + $t + = BT::MetaInfo::Cached->new( $cf, + { cache_root => '/tmp/OBTFileCache' } ); }; if ($@) { - warn "Error reading torrent $torrent\n"; - push @delete, $files{torrent}{$name}{$epoch}; - delete $files{torrent}{$name}{$epoch}; - next; + warn "Error reading torrent $cf\n"; + push @delete, $ct; + next EPOCH; } - $files{torrent}{$name}{$epoch}{comment} = $t->{comment}; + $ct->{comment} = $t->{comment}; my ($path) = $t->{comment} =~ /($OBT->{BASENAME}\/[^\n]+)\n/s; - unless ( -e $OBT->{DIR_FTP} . "/$path" ) { + if ( !-e $OBT->{DIR_FTP} . "/$path" ) { print - "Deleting $files{torrent}{$name}{$epoch}{file} the path ($path) doesn't exist.\n"; - push @delete, $files{torrent}{$name}{$epoch}; - delete $files{torrent}{$name}{$epoch}; - next; + 'Deleting ', + $cn->{$epoch}{file}, ' the path (', $path, + ") doesn't exist.\n"; + push @delete, $ct; + next EPOCH; } - my $hash = $t->info_hash; - $hash = unpack( "H*", $hash ); + my $hash = unpack( "H*", $t->info_hash ); + $ct->{info_hash} = $hash; undef $t; - $files{torrent}{$name}{$epoch}{info_hash} = $hash; - - if ( exists $keep{$name} ) { - if ( exists $keep{$name}{$hash} ) { - if ( $keep{$name}{$hash}{epoch} == $epoch ) { - next; - } - print "Removing [$name] [$hash]\n\t", - $keep{$name}{$hash}{path}, - "\n"; - push @delete, $keep{$name}{$hash}; - delete $files{torrent}{ $keep{$name}{$hash}{name} } - { $keep{$name}{$hash}{epoch} }; - $keep{$name}{$hash} = $files{torrent}{$name}{$epoch}; - print "Keeping additional instance of [$name] [$hash]\n\t", - $keep{$name}{$hash}{path}, - "\n"; + if ( $seen{$name} && $seen{$name} ne $hash ) { + print "Removing older [$name] [$hash]\n\t", + $ct->{path}, + "\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; } - else { - print "Removing old [$name] [$hash]\n"; - if ( $keep{$name}{$hash}{path} ) { - print "\t", $keep{$name}{$hash}{path}, "\n"; - } - push @delete, $files{torrent}{$name}{$epoch}; - delete $files{torrent}{$name}{$epoch}; - } + + print "Removing duplicate [$name] [$hash]\n\t", + $keep{$hash}{path}, "\n"; + + $keep{$hash}{reason} = 'duplicate'; + $ct->{reason} = 'duplicate'; + + push @delete, $keep{$hash}; } else { - print "Keeping first instance of $name [$hash]\n\t", - $files{torrent}{$name}{$epoch}{path}, - "\n"; - $keep{$name}{$hash} = $files{torrent}{$name}{$epoch}; - + $ct->{reason} = 'first'; } + + $keep{$hash} = $ct; + $seen{$name} = $hash; } } #print Dump \%files, \%keep, \@delete; +#print Dump \%keep, \@delete; #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}; + + if ($reason && $reason ne 'only') { + print "Keeping $reason instance of [$name] [$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}; + + 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': $!"; + } + } + + if ( !$seeding{$hash} ) { + print "Starting seed of [$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}{path}, "\n"; + push @delete, $keep{$hash}; + } + } +} + foreach (@delete) { - print "Deleting '$_->{path}'\n"; - unlink $_->{path} or die "Couldn't unlink $_->{path}"; + if ( $_->{path} ) { + print "Deleting '$_->{path}'\n"; + unlink $_->{path} or die "Couldn't unlink $_->{path}"; + delete $files{torrent}{ $_->{name} }{ $_->{epoch} }; + } + else { + use Data::Dumper; + print Dumper $_; + } } foreach my $name ( keys %{ $files{ $OBT->{META_EXT} } } ) { @@ -192,30 +254,8 @@ } } -#print Dump \%keep; -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': $!"; +$client->start; - 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 { my $basedir = shift; @@ -223,14 +263,10 @@ if (@$files) { my $dir = $basedir; $dir =~ s/^$OBT->{DIR_FTP}\///; - my $torrent = Name_Torrent($dir); - $torrent =~ s/-.*$//; - $Possible_Torrents{$torrent} = 1; + Make_Possible($dir); foreach my $file (@$files) { if ( $file =~ /$INSTALL_ISO_REGEX/ ) { - $torrent = Name_Torrent("$dir/$file"); - $torrent =~ s/-.*$//; - $Possible_Torrents{$torrent} = 1; + Make_Possible("$dir/$file"); } } } @@ -242,3 +278,12 @@ } } +sub Make_Possible { + my ($path) = @_; + + my $torrent = Name_Torrent($path); + $torrent =~ s/-.*$//; + $Possible_Torrents{$torrent} = 1; + + return $torrent; +}