[BACK]Return to CurrentTorrents.pl CVS log [TXT][DIR] Up to [local] / openbsd / OpenBSDTorrents

Diff for /openbsd/OpenBSDTorrents/CurrentTorrents.pl between version 1.13 and 1.33

version 1.13, 2005/05/02 22:43:53 version 1.33, 2010/03/03 18:15:31
Line 1 
Line 1 
 #!/usr/bin/perl -T  #!/usr/bin/perl -T
 #$Id$  #$RedRiver: CurrentTorrents.pl,v 1.32 2010/03/03 18:13:07 andrew Exp $
 use strict;  use strict;
 use warnings;  use warnings;
 use diagnostics;  use diagnostics;
   
 use Time::Local;  use Time::Local;
   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;
   Process_Dir( $OBT->{DIR_FTP} );
   
 my %files;  my %files;
 foreach my $DIR ($OBT->{DIR_NEW_TORRENT}, $OBT->{DIR_TORRENT}) {  my @delete;
         opendir DIR, $DIR  foreach my $DIR ( $OBT->{DIR_NEW_TORRENT}, $OBT->{DIR_TORRENT} ) {
                 or die "Couldn't opendir $DIR: $!";      opendir DIR, $DIR
         foreach (readdir DIR) {          or die "Couldn't opendir $DIR: $!";
                 next unless my ($ext) = /\.(torrent|$OBT->{META_EXT})$/;      foreach ( readdir DIR ) {
           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 $_\n";
                         file      => $_,  
                         dir       => $DIR,  
                         path      => "$DIR/$_",  
                         ext       => $ext,  
                         year      => $year,  
                         mon       => $mon,  
                         mday      => $mday,  
                         hour      => $hour,  
                         min       => $min,  
                         name      => $name,  
                         epoch     => $epoch,  
                 };  
   
         }          $files{$ext}{$name}{$epoch} = {
         closedir DIR;              file => $_,
               dir  => $DIR,
               path => "$DIR/$_",
               ext  => $ext,
   
               #year      => $year,
               #mon       => $mon,
               #mday      => $mday,
               #hour      => $hour,
               #min       => $min,
               name  => $name,
               epoch => $epoch,
           };
   
           if ( $name =~ m/\A $OBT->{BASENAME} /xms
               && !exists $Possible_Torrents{$name} )
           {
               #print "Would remove $_\n";
               push @delete, $files{$ext}{$name}{$epoch};
           }
       }
       closedir DIR;
 }  }
   
   #print Dump \%files;
   
 my %keep;  my %keep;
 my @delete;  my %seen;
 foreach my $name (keys %{ $files{torrent} }) {  foreach my $name ( sort keys %{ $files{torrent} } ) {
         next unless $name =~ /^$Name_Filter/;      next unless $name =~ /^$Name_Filter/;
         print "Checking $name\n";  
   
         foreach my $epoch ( sort { $b <=> $a } keys %{ $files{torrent}{$name} } ) {      #next if $name !~ /songs/xms;
                 #print "\t$epoch\n";      #next if $name =~ /_packages_/xms;
                 my $torrent = $files{torrent}{$name}{$epoch}{path};      #print "Checking $name\n";
   
                 my $t;      my $cn = $files{torrent}{$name};
                 eval { $t = BT::OBTMetaInfo->new( $torrent ); };  
   
                 if ($@) {  EPOCH: foreach my $epoch ( sort { $b <=> $a } keys %{$cn} ) {
                         warn "Error reading torrent $torrent\n";          my $ct = $cn->{$epoch};
                         next;          my $cf = $ct->{path};
                 }  
   
                 $files{torrent}{$name}{$epoch}{comment}   = $t->{comment};          #print "\t$epoch - $cf\n";
                 my ($path) = $t->{comment} =~ /Files from ([^\n]+)\n/s;  
   
                 unless (-d $OBT->{DIR_FTP} . "/$path") {          my $t;
                         #print "Deleting $files{torrent}{$name}{$epoch}{file} the path doesn't exist.\n";          eval {
                         push @delete, $files{torrent}{$name}{$epoch};              $t
                         delete $files{torrent}{$name}{$epoch};                  = BT::MetaInfo::Cached->new( $cf,
                         next;                  { cache_root => '/tmp/OBTFileCache' } );
                 }          };
   
                 if (          if ($@) {
                         keys %{ $files{torrent}{$name} } == 1 &&              warn "Error reading torrent $cf\n";
                         $files{torrent}{$name}{$epoch}{dir}              push @delete, $ct;
                                 eq $OBT->{DIR_TORRENT}              next EPOCH;
                 ) {          }
                         #print "Skipping torrent for $name there is only one.\n";  
                         next;  
                 }  
   
                 my $hash = $t->info_hash_cached($torrent);          $ct->{comment} = $t->{comment};
                 $hash = unpack("H*", $hash);          my ($path) = $t->{comment} =~ /($OBT->{BASENAME}\/[^\n]+)\n/s;
   
                 $files{torrent}{$name}{$epoch}{info_hash} = $hash;          if ( !-e $OBT->{DIR_FTP} . "/$path" ) {
               print
                   'Deleting ',
                   $cn->{$epoch}{file}, ' the path (', $path,
                   ") doesn't exist.\n";
               push @delete, $ct;
               next EPOCH;
           }
   
                 undef $t;          my $hash = unpack( "H*", $t->info_hash );
           $ct->{info_hash} = $hash;
   
                 if (exists $keep{$name}) {          undef $t;
                         if (exists $keep{$name}{$hash}) {  
                                 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};  
   
                 }          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;
               }
   
               print "Removing duplicate [$name] [$hash]\n\t",
                   $keep{$hash}{path}, "\n";
   
               $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;
   
   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};
   
           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 $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}{path}, "\n";
               push @delete, $keep{$hash};
           }
       }
   }
   
 foreach (@delete) {  foreach (@delete) {
         print "Deleting '$_->{path}'\n";      if ( $_->{path} ) {
         unlink $_->{path} or die "Couldn't unlink $_->{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} } }) {  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}{path};
                         print "Unlinking '$path'\n";              print "Unlinking '$path'\n";
                         unlink $path or die "couldn't unlink '$path': $!";              unlink $path or die "couldn't unlink '$path': $!";
                 }          }
         }      }
 }  }
   
   $client->start;
   
 #print Dump \%keep;  sub Process_Dir {
 foreach my $name (keys %keep) {      my $basedir = shift;
         foreach my $hash (keys %{ $keep{$name} }) {  
                 my $file = $keep{$name}{$hash}{file};  
                 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 ( $dirs, $files ) = Get_Files_and_Dirs($basedir);
                         my $epoch = $keep{$name}{$hash}{epoch};      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");
               }
           }
       }
   
                         if (exists $files{txt}{$name}{$epoch}) {      foreach my $subdir (@$dirs) {
                                 my $m_file = $files{txt}{$name}{$epoch}{file};          next if $subdir eq '.';
                                 my $m_dir  = $files{txt}{$name}{$epoch}{dir };          next if $subdir eq '..';
                                 rename(          Process_Dir("$basedir/$subdir");
                                         "$m_dir/$m_file",      }
                                         $OBT->{DIR_TORRENT} . "/" . $m_file  }
                                 ) or die "Couldn't rename '$m_file': $!";  
                         }  sub Make_Possible {
                 }      my ($path) = @_;
         }  
       my $torrent = Name_Torrent($path);
       $torrent =~ s/-.*$//;
       $Possible_Torrents{$torrent} = 1;
   
       return $torrent;
 }  }

Legend:
Removed from v.1.13  
changed lines
  Added in v.1.33

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>