version 1.13, 2005/05/05 18:48:19 |
version 1.25, 2008/09/23 17:35:45 |
|
|
#!/usr/bin/perl -T |
#!/usr/bin/perl -T |
#$Id$ |
#$RedRiver: ServerTorrents.pl,v 1.24 2007/11/02 02:36:01 andrew Exp $ |
use strict; |
use strict; |
use warnings; |
use warnings; |
use diagnostics; |
use diagnostics; |
|
|
use LWP::UserAgent; |
use LWP::UserAgent; |
use Time::Local; |
use Time::Local; |
|
use File::Basename; |
|
#use YAML; |
|
|
use lib 'lib'; |
use lib 'lib'; |
use OpenBSDTorrents; |
use OpenBSDTorrents; |
use BT::OBTMetaInfo; |
use BT::MetaInfo::Cached; |
|
|
%ENV = (); |
%ENV = (); |
|
|
#use YAML; |
|
|
|
justme(); |
justme(); |
|
|
my @Sizes = ('', 'Ki', 'Mi', 'Gi', 'Ti'); |
my @Sizes = ('', 'Ki', 'Mi', 'Gi', 'Ti'); |
|
|
s/^\s+//; |
s/^\s+//; |
s/\s+$//; |
s/\s+$//; |
next unless $_; |
next unless $_; |
my ($name, $hash) = split /\t/; |
my ($name, $hash, $disabled) = split /\t/; |
next if $name eq 'File'; |
next if $name eq 'File'; |
|
|
$name =~ s#^/torrents/##; |
$name =~ s#.*/##; |
$server_torrents{$name} = $hash; |
$server_torrents{$name}{$hash} = $disabled; |
} |
} |
} |
} |
} else { |
} else { |
die $response->status_line; |
die $response->status_line; |
} |
} |
|
|
|
|
my %files; |
my %files; |
opendir DIR, $OBT->{DIR_TORRENT} or die "Couldn't opendir $OBT->{DIR_TORRENT}: $!"; |
opendir DIR, $OBT->{DIR_TORRENT} or die "Couldn't opendir $OBT->{DIR_TORRENT}: $!"; |
foreach (readdir DIR) { |
foreach (readdir DIR) { |
|
|
die "Invalid character in $_: $!"; |
die "Invalid character in $_: $!"; |
} |
} |
next unless /\.torrent$/; |
next unless /\.torrent$/; |
my ($name, $year, $mon, $mday, $hour, $min) = |
|
/^(.*)-(\d{4})-(\d{2})-(\d{2})-(\d{2})(\d{2})/; |
|
|
|
my $time = "$year.$mon.$mday $hour:$min"; |
my $name = basename($_, '.torrent'); |
|
|
$mon--; |
if (my ($base, $year, $mon, $mday, $hour, $min) = |
|
/^(.*)-(\d{4})-(\d{2})-(\d{2})-(\d{2})(\d{2})/) { |
|
$name = $base; |
|
} |
|
|
my $epoch = timegm(0,$min,$hour,$mday,$mon,$year); |
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} = { |
$files{$name}{$epoch} = { |
file => $_, |
file => $_, |
year => $year, |
details => $t, |
mon => $mon, |
name => $name, |
mday => $mday, |
|
hour => $hour, |
|
min => $min, |
|
time => $time, |
|
epoch => $epoch, |
epoch => $epoch, |
}; |
}; |
|
|
} |
} |
closedir DIR; |
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"; |
#print "$name\n"; |
foreach my $epoch ( sort { $b <=> $a } keys %{ $files{$name} } ) { |
foreach my $epoch ( sort { $b <=> $a } keys %{ $files{$name} } ) { |
#print "\t$epoch\n"; |
#print "\t$epoch\n"; |
my $torrent = $files{$name}{$epoch}{file}; |
my $torrent = $files{$name}{$epoch}{file}; |
unless (exists $server_torrents{$torrent} ) { |
my $hash = unpack("H*", $files{$name}{$epoch}{'details'}->info_hash ); |
#my $time = |
#printf "LOCAL: [%s] [%s]\n", $hash, $torrent; |
# $files{$name}{$epoch}{year} . '-' . |
|
# $files{$name}{$epoch}{mon} . '-' . |
$torrents{$torrent}{$hash} = $files{$name}{$epoch}; |
# $files{$name}{$epoch}{mday} . ' ' . |
|
# $files{$name}{$epoch}{hour} . ':' . |
unless (exists $server_torrents{$torrent}{$hash}) { |
# $files{$name}{$epoch}{min} . ':00'; |
Upload_Torrent($files{$name}{$epoch}); |
|
|
Upload_Torrent($torrent, $files{$name}{$epoch}{time}); |
|
} |
} |
next; |
|
} |
} |
} |
} |
|
|
foreach my $file (keys %server_torrents) { |
foreach my $torrent (keys %server_torrents) { |
my ($name, $year, $mon, $mday, $hour, $min) = |
foreach my $hash (keys %{ $server_torrents{$torrent} }) { |
$file =~ |
#printf "SERVER: [%s] [%s]\n", $hash, $torrent; |
/^(.*)-(\d{4})-(\d{2})-(\d{2})-(\d{2})(\d{2})/; |
if ((! exists $torrents{$torrent}{$hash}) && |
unless (exists $files{$name}) { |
$server_torrents{$torrent}{$hash} != 1 |
Delete_Torrent($file); |
) { |
|
Delete_Torrent($torrent, $hash); |
|
} |
} |
} |
} |
} |
|
|
|
|
|
|
sub Upload_Torrent |
sub Upload_Torrent |
{ |
{ |
my $file = shift; |
my $torrent = shift; |
my $time = shift; |
my $t = $torrent->{'details'}; |
|
|
|
my $file = $torrent->{'file'}; |
print "Uploading $file\n"; |
print "Uploading $file\n"; |
|
|
my $t; |
|
eval { $t = BT::OBTMetaInfo->new("$OBT->{DIR_TORRENT}/$file"); }; |
|
if ($@) { |
|
warn "Error reading torrent $file\n"; |
|
return undef; |
|
} |
|
|
|
my $size = $t->total_size; |
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; |
|
|
|
($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; |
my $i = 0; |
while ($size > 1024) { |
while ($size > 1024) { |
$size /= 1024; |
$size /= 1024; |
|
|
my $comment = $t->{comment}; |
my $comment = $t->{comment}; |
$comment =~ s/\n.*$//s; |
$comment =~ s/\n.*$//s; |
|
|
my ($filename) = $comment =~ /Files from (.+)/; |
my $filename = |
|
$comment =~ /($OBT->{BASENAME}.+)/ ? $1 |
|
: $file; |
$filename =~ s#/# #g; |
$filename =~ s#/# #g; |
|
$filename =~ s/\.torrent\z//; |
|
|
$comment .= " [$size]"; |
$comment .= " [$size]"; |
$filename .= " [$time]"; |
$filename .= " [$time]"; |
|
|
torrent => [ $OBT->{DIR_TORRENT} . "/$file" ], |
torrent => [ $OBT->{DIR_TORRENT} . "/$file" ], |
url => "/torrents/$file", |
url => "/torrents/$file", |
filename => $filename, |
filename => $filename, |
filedate => $time, |
filedate => $sql_time, |
info => $comment, |
info => $comment, |
hash => '', |
hash => '', |
autoset => 'enabled', # -> checked="checked" |
autoset => 'enabled', # -> checked="checked" |
|
|
|
|
sub Delete_Torrent |
sub Delete_Torrent |
{ |
{ |
my $file = shift; |
my $filename = shift; |
print "Will delete $file soon enough\n"; |
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"\>([^<]+)\</; |
|
|
|
if ($result eq 'Torrent was removed successfully.') { |
|
print STDERR "Removed $filename [$hash]\n"; |
|
} |
|
elsif ($result) { |
|
print STDERR "Error: $result (removing $filename [$hash])\n"; |
|
} |
|
else { |
|
print STDERR "An unknown error occurred removing $filename [$hash]\n"; |
|
} |
|
} |
|
else { |
|
die $response->status_line . " removing $filename [$hash]\n"; |
|
} |
} |
} |