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

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

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

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