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

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

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

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