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

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

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

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