X-Git-Url: https://git.camperquake.de/gitweb.cgi?p=xmlrtorrent.git;a=blobdiff_plain;f=xmlrtorrent.pl;fp=xmlrtorrent.pl;h=397d72c78d15328ce9382c11744e2ab9b15b9377;hp=4f63ba7dec6ea25f2b70a8f98eb56026983bbb5c;hb=58be7bbed0e4ba7be83d0bd3093bcc69571a2a0c;hpb=ba42ec7d8992f8baea148b4241964eeba3d7d3b7 diff --git a/xmlrtorrent.pl b/xmlrtorrent.pl index 4f63ba7..397d72c 100644 --- a/xmlrtorrent.pl +++ b/xmlrtorrent.pl @@ -11,7 +11,6 @@ use XML::Simple; use Data::Dumper; use File::Spec; use List::Util qw(max); -use xmlrtorrent; my @talkers; my $talker; @@ -21,7 +20,6 @@ my $scriptdir = File::Spec->catfile(Irssi::get_irssi_dir(), 'scripts'); my $plugindir = File::Spec->catfile($scriptdir, 'xmlrtorrent'); my %torrentlist = (); my $torrentindex = 1; -my $rtorrent; my @outputstack = (undef); @@ -90,6 +88,33 @@ my $xmlrtorrent_commands = { }, }; +# This is shamelessly stolen from pythons urlgrabber +sub format_number { + my $number = shift; + my $SI = shift || 0; + my @symbols = ('', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'); + my $step = $SI?1000:1024; + my $thresh = 999; + my $depth = 0; + my $max_depth = $#symbols; + my $format; + + while (($number > $thresh) and ($depth < $max_depth)) { + $depth += 1; + $number /= $step; + } + + if ($number =~ /^[+-]?\d+$/) { + # Integer. + $format = '%i%s'; + } elsif ($number < 9.95) { + $format = '%.1f%s'; + } else { + $format = '%.0f%s'; + } + return sprintf($format, $number, $symbols[$depth]); +} + sub write_irssi { my @text = @_; my $output = $outputstack[0]; @@ -171,8 +196,8 @@ sub cmd_queue { $u = $torrentlist{$id}->{'URL'}; write_debug('Sending %s to rtorrent', $u); - unless(defined($rtorrent->load_start($talker, $u))) { - write_irssi('%%RError sending URL %s: %s', $u, $rtorrent->errstr()); + unless(defined($talker->load_start($u))) { + write_irssi('%%RError sending URL %s: %s', $u, $talker->errstr()); } else { write_irssi('%s enqueued', $u); delete($torrentlist{$id}); @@ -206,8 +231,8 @@ sub cmd_remote { my $rqueue; if (('list' eq $subcmd) or !defined($subcmd)) { - unless(defined($rqueue = $rtorrent->download_list($talker))) { - write_irssi('Error getting list of downloads: %s', $rtorrent->errstr()); + unless(defined($rqueue = $talker->download_list())) { + write_irssi('Error getting list of downloads: %s', $talker->errstr()); return; } @@ -217,13 +242,13 @@ sub cmd_remote { } else { foreach (@{$rqueue}) { write_irssi(' %s%s: %sB/%sB done (%d%%), %sB/s up, %sB/s down', - $_->{'ACTIVE'}?'*':' ', - $_->{'NAME'}, - $_->{'BYTES_DONE'}, - $_->{'SIZE_BYTES'}, - $_->{'BYTES_DONE'}*100/$_->{'SIZE_BYTES'}, - $_->{'UP_RATE'}, - $_->{'DOWN_RATE'}); + $_->{'ACTIVE'}?'*':' ', + $_->{'NAME'}, + format_number($_->{'BYTES_DONE'}), + format_number($_->{'SIZE_BYTES'}), + $_->{'BYTES_DONE'}*100/$_->{'SIZE_BYTES'}, + format_number($_->{'UP_RATE'}), + format_number($_->{'DOWN_RATE'})); } } } @@ -441,11 +466,6 @@ sub init_xmlrtorrent { %torrentlist = map { my $a = substr($_, 1); ("$a" => $torrentlist{$_}) } keys(%torrentlist); $torrentindex = max(keys(%torrentlist)) + 1; - unless(defined($rtorrent = xmlrtorrent->new())) { - write_irssi('Could not initialize XMLRPC instance'); - return; - } - if ($bindings) { Irssi::signal_add_first('command script load', 'sig_command_script_unload');