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

Annotation of openbsd/OpenBSDTorrents/ServerTorrents.pl, Revision 1.9

1.1       andrew      1: #!/usr/bin/perl -T
1.9     ! andrew      2: #$Id: ServerTorrents.pl,v 1.8 2005/04/01 01:20:15 andrew Exp andrew $
1.1       andrew      3: use strict;
                      4: use warnings;
                      5: use diagnostics;
                      6:
                      7: use BT::MetaInfo;
                      8: use LWP::UserAgent;
                      9: use Time::Local;
                     10:
                     11: use lib 'lib';
                     12: use OpenBSDTorrents;
                     13:
                     14: %ENV = ();
                     15:
1.5       andrew     16: #use YAML;
1.3       andrew     17:
1.5       andrew     18: justme();
1.1       andrew     19:
                     20: my $url_torrents = 'http://openbsd.somedomain.net/dumptorrents.php';
                     21: my $url_upload   = 'http://openbsd.somedomain.net/newtorrents.php';
                     22: my $url_delete   = 'http://openbsd.somedomain.net/deltorrents.php';
1.4       andrew     23: my $url_sanity   = 'http://openbsd.somedomain.net/sanity.php';
1.1       andrew     24:
                     25: my $user = 'torrentup';
                     26: my $pass = 'ssapword';
                     27:
1.3       andrew     28: my @Sizes = ('', 'Ki', 'Mi', 'Gi', 'Ti');
1.1       andrew     29: my $ua = LWP::UserAgent->new;
                     30:
                     31: my $response = $ua->get($url_torrents);
                     32:
                     33: my %server_torrents;
                     34: if ($response->is_success) {
                     35:     my $content = $response->content;  # or whatever
                     36:     $content =~ s/^.*<!-- BEGIN LIST -->//s || die "Beginning of list not found!";
                     37:     $content =~ s/<!-- END LIST -->.*$//s   || die "End of list not found!";
                     38:     unless ($content =~ /No data/) {
                     39:         foreach (split /\n/, $content) {
                     40:             s/^\s+//;
                     41:             s/\s+$//;
                     42:             next unless $_;
                     43:             my ($name, $hash) = split /\t/;
                     44:             next if $name eq 'File';
                     45:
                     46:             $name =~ s#^/torrents/##;
                     47:             $server_torrents{$name} = $hash;
                     48:         }
                     49:     }
                     50: } else {
                     51:     die $response->status_line;
                     52: }
                     53:
                     54:
                     55: my %files;
                     56: opendir DIR, $TorrentDir or die "Couldn't opendir $TorrentDir: $!";
                     57: foreach (readdir DIR) {
1.8       andrew     58:        chomp;
1.1       andrew     59:        if (/^([^\/]+)$/) {
                     60:                $_ = $1;
                     61:        } else {
                     62:                die "Invalid character in $_: $!";
                     63:        }
                     64:        next unless /\.torrent$/;
                     65:        my ($name, $year, $mon, $mday, $hour, $min) =
                     66:           /^(.*)-(\d{4})-(\d{2})-(\d{2})-(\d{2})(\d{2})/;
1.7       andrew     67:
1.9     ! andrew     68:        my $time = "$year.$mon.$mday $hour:$min";
        !            69:
1.7       andrew     70:        $mon--;
1.1       andrew     71:
                     72:        my $epoch = timegm(0,$min,$hour,$mday,$mon,$year);
                     73:
                     74:        $files{$name}{$epoch} = {
                     75:                file      => $_,
                     76:                year      => $year,
                     77:                mon       => $mon,
                     78:                mday      => $mday,
                     79:                hour      => $hour,
                     80:                min       => $min,
1.9     ! andrew     81:                time      => $time,
1.1       andrew     82:                epoch     => $epoch,
                     83:        };
                     84:
                     85: }
                     86: closedir DIR;
                     87:
                     88: #print Dump \%server_torrents, \%files;
                     89:
                     90: foreach my $name (keys %files) {
                     91:        #print "$name\n";
                     92:        foreach my $epoch ( sort { $b <=> $a } keys %{ $files{$name} } ) {
                     93:                #print "\t$epoch\n";
                     94:                my $torrent = $files{$name}{$epoch}{file};
                     95:                unless (exists $server_torrents{$torrent} ) {
1.9     ! andrew     96:                        #my $time =
        !            97:                        #       $files{$name}{$epoch}{year} . '-' .
        !            98:                        #       $files{$name}{$epoch}{mon}  . '-' .
        !            99:                        #       $files{$name}{$epoch}{mday} . ' ' .
        !           100:                        #       $files{$name}{$epoch}{hour} . ':' .
        !           101:                        #       $files{$name}{$epoch}{min}  . ':00';
1.1       andrew    102:
1.9     ! andrew    103:                        Upload_Torrent($torrent, $files{$name}{$epoch}{time});
1.1       andrew    104:                }
                    105:                next;
                    106:        }
                    107: }
                    108:
                    109: foreach my $file (keys %server_torrents) {
1.2       andrew    110:        my ($name, $year, $mon, $mday, $hour, $min) =
                    111:           $file =~
                    112:           /^(.*)-(\d{4})-(\d{2})-(\d{2})-(\d{2})(\d{2})/;
                    113:        unless (exists $files{$name}) {
1.1       andrew    114:                Delete_Torrent($file);
                    115:        }
                    116: }
                    117:
1.4       andrew    118: $ua->get($url_sanity);
1.1       andrew    119:
                    120: sub Upload_Torrent
                    121: {
                    122:        my $file = shift;
                    123:        my $time = shift;
                    124:
                    125:        print "Uploading $file\n";
                    126:
                    127:        my $t;
                    128:        eval { $t = BT::MetaInfo->new("$TorrentDir/$file"); };
                    129:        if ($@) {
                    130:                warn "Error reading torrent $file\n";
                    131:                return undef;
                    132:        }
                    133:
1.3       andrew    134:        my $size = $t->total_size;
                    135:
                    136:        my $i;
                    137:        while ($size > 1024) {
                    138:                $size /= 1024;
                    139:                $i++;
                    140:        }
                    141:        $size = sprintf('%.2f', $size);
                    142:        $size .= $Sizes[$i] . 'B';
                    143:
1.1       andrew    144:        my $comment = $t->{comment};
                    145:        $comment =~ s/\n.*$//s;
1.3       andrew    146:
                    147:        my ($filename) = $comment =~ /Files from (.+)/;
1.1       andrew    148:        $filename =~ s#/# #g;
                    149:
1.3       andrew    150:        $comment  .= " [$size]";
                    151:        $filename .= " [$time]";
1.1       andrew    152:
                    153:        my $response = $ua->post($url_upload, {
                    154:                username => $user,
                    155:                password => $pass,
                    156:                torrent  => [ "$TorrentDir/$file" ],
                    157:                url      => "/torrents/$file",
                    158:                filename => $filename,
                    159:                filedate => $time,
                    160:                info     => $comment,
                    161:                hash     => '',
                    162:                autoset  => 'enabled', # -> checked="checked"
                    163:        }, Content_Type => 'form-data');
                    164:
                    165:        if ($response->is_success) {
1.6       andrew    166:                print STDERR "Uploaded  $file\n";
1.1       andrew    167:                #print $response->content;
                    168:        } else {
                    169:                die $response->status_line;
                    170:        }
                    171: }
                    172:
                    173: sub Delete_Torrent
                    174: {
                    175:        my $file = shift;
                    176:        print "Will delete $file soon enough\n";
                    177: }

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