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

Annotation of openbsd/OpenBSDTorrents/CurrentTorrents.pl, Revision 1.5

1.1       andrew      1: #!/usr/bin/perl -T
1.5     ! andrew      2: #$Id: CurrentTorrents.pl,v 1.4 2005/03/24 00:20:33 andrew Exp andrew $
1.1       andrew      3: use strict;
                      4: use warnings;
                      5: use diagnostics;
                      6:
                      7: use BT::MetaInfo;
1.2       andrew      8: use Time::Local;
1.1       andrew      9:
                     10: use lib 'lib';
                     11: use OpenBSDTorrents;
                     12:
                     13: %ENV = ();
                     14:
1.2       andrew     15: #use YAML;
1.1       andrew     16:
1.2       andrew     17: my %files;
                     18: opendir DIR, $TorrentDir or die "Couldn't opendir $TorrentDir: $!";
                     19: foreach (readdir DIR) {
                     20:        if (/^([^\/]+)$/) {
                     21:                $_ = $1;
                     22:        } else {
                     23:                die "Invalid character in $_: $!";
                     24:        }
                     25:        next unless /\.torrent$/;
                     26:        my ($name, $year, $mon, $mday, $hour, $min) =
                     27:           /^(.*)-(\d{4})-(\d{2})-(\d{2})-(\d{2})(\d{2})/;
                     28:
                     29:        my $epoch = timegm(0,$min,$hour,$mday,$mon,$year);
                     30:
1.4       andrew     31:        print "Adding $_\n";
1.2       andrew     32:
                     33:        $files{$name}{$epoch} = {
                     34:                file      => $_,
                     35:                year      => $year,
                     36:                mon       => $mon,
                     37:                mday      => $mday,
                     38:                hour      => $hour,
                     39:                min       => $min,
                     40:                epoch     => $epoch,
                     41:        };
                     42:
                     43: }
                     44: closedir DIR;
                     45:
                     46: my %keep;
                     47: my @delete;
                     48: foreach my $name (keys %files) {
1.4       andrew     49:        #print "$name\n";
                     50:
                     51:        foreach my $epoch ( sort { $b <=> $a } keys %{ $files{$name} } ) {
                     52:                #print "\t$epoch\n";
                     53:                my $torrent = $files{$name}{$epoch}{file};
                     54:
                     55:                my $t;
                     56:                eval { $t = BT::MetaInfo->new("$TorrentDir/$torrent"); };
                     57:                if ($@) {
                     58:                        warn "Error reading torrent $torrent\n";
                     59:                        next;
                     60:                }
                     61:
                     62:                $files{$name}{$epoch}{comment}   = $t->{comment};
                     63:                my ($path) = $t->{comment} =~ /Files from ([^\n]+)\n/s;
                     64:
1.5     ! andrew     65:                unless (-d "$BaseDir/$path") {
1.4       andrew     66:                        print "Deleting $files{$name}{$epoch}{file} the path doesn't exist.\n";
                     67:                        push @delete, $files{$name}{$epoch}{file};
                     68:                }
                     69:
                     70:                if (keys %{ $files{$name} } == 1) {
                     71:                        print "Skipping torrent for $name there is only one.\n";
                     72:                        next;
                     73:                }
                     74:
                     75:                my $hash = $t->info_hash;
                     76:                $hash = unpack("H*", $hash);
                     77:
                     78:                $files{$name}{$epoch}{info_hash} = $hash;
                     79:
                     80:                undef $t;
                     81:
1.2       andrew     82:                if (exists $keep{$name}) {
                     83:                        if (exists $keep{$name}{$hash}) {
                     84:                                push @delete, $keep{$name}{$hash};
                     85:                                $keep{$name}{$hash} =
1.4       andrew     86:                                        $files{$name}{$epoch}{file};
1.2       andrew     87:                        } else {
1.4       andrew     88:                                push @delete, $files{$name}{$epoch}{file};
1.2       andrew     89:                        }
                     90:                } else {
                     91:                        $keep{$name}{$hash} =
1.4       andrew     92:                                $files{$name}{$epoch}{file};
1.2       andrew     93:
                     94:                }
                     95:        }
                     96: }
                     97:
                     98: #print Dump \%files, \%keep, \@delete;
                     99:
                    100: foreach (@delete) {
1.3       andrew    101:        print "Deleting '$_'\n";
1.2       andrew    102:        unlink "$TorrentDir/$_" or die "Couldn't unlink $_";
                    103: }

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