=================================================================== RCS file: /cvs/openbsd/OpenBSDTorrents/ServerTorrents.pl,v retrieving revision 1.6 retrieving revision 1.27 diff -u -r1.6 -r1.27 --- openbsd/OpenBSDTorrents/ServerTorrents.pl 2005/03/29 00:28:50 1.6 +++ openbsd/OpenBSDTorrents/ServerTorrents.pl 2008/12/29 22:04:00 1.27 @@ -1,34 +1,26 @@ #!/usr/bin/perl -T -#$Id: ServerTorrents.pl,v 1.6 2005/03/29 00:28:50 andrew Exp $ +#$RedRiver: ServerTorrents.pl,v 1.26 2008/12/29 22:02:14 andrew Exp $ use strict; use warnings; use diagnostics; -use BT::MetaInfo; use LWP::UserAgent; use Time::Local; +use File::Basename; +#use YAML; use lib 'lib'; use OpenBSDTorrents; +use BT::MetaInfo::Cached; %ENV = (); -#use YAML; - justme(); -my $url_torrents = 'http://openbsd.somedomain.net/dumptorrents.php'; -my $url_upload = 'http://openbsd.somedomain.net/newtorrents.php'; -my $url_delete = 'http://openbsd.somedomain.net/deltorrents.php'; -my $url_sanity = 'http://openbsd.somedomain.net/sanity.php'; - -my $user = 'torrentup'; -my $pass = 'ssapword'; - my @Sizes = ('', 'Ki', 'Mi', 'Gi', 'Ti'); my $ua = LWP::UserAgent->new; -my $response = $ua->get($url_torrents); +my $response = $ua->get($OBT->{URL_TORRENTS}); my %server_torrents; if ($response->is_success) { @@ -40,95 +32,124 @@ s/^\s+//; s/\s+$//; next unless $_; - my ($name, $hash) = split /\t/; + my ($name, $hash, $disabled) = split /\t/; next if $name eq 'File'; - $name =~ s#^/torrents/##; - $server_torrents{$name} = $hash; + $name =~ s#.*/##; + $server_torrents{$name}{$hash} = $disabled; } } } else { die $response->status_line; } - my %files; -opendir DIR, $TorrentDir or die "Couldn't opendir $TorrentDir: $!"; +opendir DIR, $OBT->{DIR_TORRENT} or die "Couldn't opendir $OBT->{DIR_TORRENT}: $!"; foreach (readdir DIR) { + chomp; if (/^([^\/]+)$/) { $_ = $1; } else { die "Invalid character in $_: $!"; } next unless /\.torrent$/; - chomp; - my ($name, $year, $mon, $mday, $hour, $min) = - /^(.*)-(\d{4})-(\d{2})-(\d{2})-(\d{2})(\d{2})/; - my $epoch = timegm(0,$min,$hour,$mday,$mon,$year); + my $name = basename($_, '.torrent'); + if (my ($base, $year, $mon, $mday, $hour, $min) = + /^(.*)-(\d{4})-(\d{2})-(\d{2})-(\d{2})(\d{2})/) { + $name = $base; + } + + my $t; + eval { + $t = BT::MetaInfo::Cached->new( + $OBT->{DIR_TORRENT} . '/' . $_, + { + cache_root => '/tmp/OBTFileCache' + #$OBT->{DIR_HOME} . '/FileCache' + } + ); + }; + + if ($@) { + warn "Error reading torrent $_\n"; + next; + } + + my $epoch = $t->creation_date; + $files{$name}{$epoch} = { file => $_, - year => $year, - mon => $mon, - mday => $mday, - hour => $hour, - min => $min, + details => $t, + name => $name, epoch => $epoch, }; } closedir DIR; -#print Dump \%server_torrents, \%files; +#print Dump \%server_torrents; +#print Dump \%files; +#exit; -foreach my $name (keys %files) { +my %torrents; +FILE: foreach my $name (keys %files) { #print "$name\n"; foreach my $epoch ( sort { $b <=> $a } keys %{ $files{$name} } ) { #print "\t$epoch\n"; my $torrent = $files{$name}{$epoch}{file}; - unless (exists $server_torrents{$torrent} ) { - my $time = - $files{$name}{$epoch}{year} . '-' . - $files{$name}{$epoch}{mon} . '-' . - $files{$name}{$epoch}{mday} . ' ' . - $files{$name}{$epoch}{hour} . ':' . - $files{$name}{$epoch}{min} . ':00'; - - Upload_Torrent($torrent, $time); + my $hash = unpack("H*", $files{$name}{$epoch}{'details'}->info_hash ); + #printf "LOCAL: [%s] [%s]\n", $hash, $torrent; + + $torrents{$torrent}{$hash} = $files{$name}{$epoch}; + + unless (exists $server_torrents{$torrent}{$hash}) { + Upload_Torrent($files{$name}{$epoch}); } - next; } } -foreach my $file (keys %server_torrents) { - my ($name, $year, $mon, $mday, $hour, $min) = - $file =~ - /^(.*)-(\d{4})-(\d{2})-(\d{2})-(\d{2})(\d{2})/; - unless (exists $files{$name}) { - Delete_Torrent($file); +foreach my $torrent (keys %server_torrents) { + foreach my $hash (keys %{ $server_torrents{$torrent} }) { + #printf "SERVER: [%s] [%s]\n", $hash, $torrent; + if ((! exists $torrents{$torrent}{$hash}) && + ( + (! defined $server_torrents{$torrent}{$hash}) || + $server_torrents{$torrent}{$hash} != 1 + ) + ) { + Delete_Torrent($torrent, $hash); + } } } -$ua->get($url_sanity); +$ua->get($OBT->{URL_SANITY}); sub Upload_Torrent { - my $file = shift; - my $time = shift; + my $torrent = shift; + my $t = $torrent->{'details'}; + my $file = $torrent->{'file'}; print "Uploading $file\n"; - my $t; - eval { $t = BT::MetaInfo->new("$TorrentDir/$file"); }; - if ($@) { - warn "Error reading torrent $file\n"; - return undef; - } - my $size = $t->total_size; + my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = + gmtime($t->creation_date); + $year += 1900; + $mon++; + my $time = sprintf "%04d.%02d.%02d %02d:%02d", + $year, $mon, $mday, $hour, $min; - my $i; + ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = + localtime($t->creation_date); + $year += 1900; + $mon++; + my $sql_time = sprintf "%04d-%02d-%02d %02d:%02d", + $year, $mon, $mday, $hour, $min; + + my $i = 0; while ($size > 1024) { $size /= 1024; $i++; @@ -139,19 +160,22 @@ my $comment = $t->{comment}; $comment =~ s/\n.*$//s; - my ($filename) = $comment =~ /Files from (.+)/; + my $filename = + $comment =~ /($OBT->{BASENAME}.+)/ ? $1 + : $file; $filename =~ s#/# #g; + $filename =~ s/\.torrent\z//; $comment .= " [$size]"; $filename .= " [$time]"; - my $response = $ua->post($url_upload, { - username => $user, - password => $pass, - torrent => [ "$TorrentDir/$file" ], + my $response = $ua->post($OBT->{URL_UPLOAD}, { + username => $OBT->{UPLOAD_USER}, + password => $OBT->{UPLOAD_PASS}, + torrent => [ $OBT->{DIR_TORRENT} . "/$file" ], url => "/torrents/$file", filename => $filename, - filedate => $time, + filedate => $sql_time, info => $comment, hash => '', autoset => 'enabled', # -> checked="checked" @@ -167,6 +191,33 @@ sub Delete_Torrent { - my $file = shift; - print "Will delete $file soon enough\n"; + my $filename = shift; + my $hash = shift; + die "No hash passed!" unless $hash; + + print "Removing $filename [$hash]\n"; + + my $response = $ua->post($OBT->{'URL_DELETE'}, { + username => $OBT->{UPLOAD_USER}, + password => $OBT->{UPLOAD_PASS}, + filename => $filename, + hash => $hash, + }, Content_Type => 'form-data'); + + if ($response->is_success) { + my ($result) = $response->content =~ /class="error"\>([^<]+)\status_line . " removing $filename [$hash]\n"; + } }