| 1 | #$Id: RTorrent.pm 938 2011-04-05 07:12:53Z zag $ | ||||||||||||
| 2 | |||||||||||||
| 3 | package Net::RTorrent; | ||||||||||||
| 4 | |||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 2 | ▶ | use RPC::XML; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 2 | ▶ | use RPC::XML::Client; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 2 | ▶ | use Net::RTorrent::Downloads; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 2 | ▶ | use Net::RTorrent::Socket; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 2 | ▶ | use Collection; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 2 | our @ISA = (); | |||||||||||
| 13 | 2 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 2 | ▶ | use 5.005; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | |||||||||||||
| 16 | =head1 NAME | ||||||||||||
| 17 | |||||||||||||
| 18 | Net::RTorrent - Perl interface to rtorrent via XML-RPC. | ||||||||||||
| 19 | |||||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||||
| 21 | |||||||||||||
| 22 | #from http scgi gate | ||||||||||||
| 23 | my $obj = new Net::RTorrent:: 'http://10.100.0.1:8080/scgitest'; | ||||||||||||
| 24 | #from network address | ||||||||||||
| 25 | my $obj = new Net::RTorrent:: '10.100.0.1:5000'; | ||||||||||||
| 26 | #from UNIX socket | ||||||||||||
| 27 | my $obj = new Net::RTorrent:: '/tmp/rtorrent.sock'; | ||||||||||||
| 28 | |||||||||||||
| 29 | #get completed torrents list | ||||||||||||
| 30 | my $dloads = $obj->get_downloads('complete'); | ||||||||||||
| 31 | #get all torrents list | ||||||||||||
| 32 | my $dloads = $obj->get_downloads(); | ||||||||||||
| 33 | #get stopped torrents list | ||||||||||||
| 34 | my $dloads = $obj->get_downloads('stopped'); | ||||||||||||
| 35 | |||||||||||||
| 36 | #fetch all items | ||||||||||||
| 37 | $dloads->fetch() | ||||||||||||
| 38 | #or by hash_info | ||||||||||||
| 39 | $dloads->fetch_one('02DE69B09364A355F71279FC8825ADB0AC8C3A29') | ||||||||||||
| 40 | #list oll hash_info | ||||||||||||
| 41 | my $keys = $dloads->list_ids; | ||||||||||||
| 42 | #upload remotely | ||||||||||||
| 43 | $obj->create( $torrent_raw ); | ||||||||||||
| 44 | $obj->create( $data, 0 ); | ||||||||||||
| 45 | |||||||||||||
| 46 | =head1 ABSTRACT | ||||||||||||
| 47 | |||||||||||||
| 48 | Perl interface to rtorrent via XML-RPC | ||||||||||||
| 49 | |||||||||||||
| 50 | =head1 DESCRIPTION | ||||||||||||
| 51 | |||||||||||||
| 52 | Net::RTorrent - short way to create tools for rtorrent. | ||||||||||||
| 53 | |||||||||||||
| 54 | =cut | ||||||||||||
| 55 | |||||||||||||
| 56 | 2 | ▶ | use constant { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 57 | #info atributes for system info | ||||||||||||
| 58 | 2 | S_ATTRIBUTES => [ | |||||||||||
| 59 | 'get_download_rate' => 'download_rate', #in my version dosn't work | ||||||||||||
| 60 | 'get_memory_usage' => 'memory_usage', | ||||||||||||
| 61 | 'get_max_memory_usage' => 'max_memory_usage', | ||||||||||||
| 62 | 'get_name' => 'name', | ||||||||||||
| 63 | 'get_safe_free_diskspace' => 'safe_free_diskspace', | ||||||||||||
| 64 | 'get_upload_rate' => 'upload_rate', | ||||||||||||
| 65 | 'system.client_version' => 'client_version', | ||||||||||||
| 66 | 'system.hostname' => 'hostname', | ||||||||||||
| 67 | 'system.library_version' => 'library_version', | ||||||||||||
| 68 | 'system.pid' => 'pid', | ||||||||||||
| 69 | ], | ||||||||||||
| 70 | 2 | }; | |||||||||||
| 71 | |||||||||||||
| 72 | 2 | our $VERSION = '0.11'; | |||||||||||
| 73 | 2 | my $attrs = { | |||||||||||
| 74 | _cli => undef, | ||||||||||||
| 75 | }; | ||||||||||||
| 76 | ### install get/set accessors for this object. | ||||||||||||
| 77 | 2 | for my $key ( keys %$attrs ) { | |||||||||||
| 78 | 2 | ▶ | no strict 'refs'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 79 | 2 | ▶ | *{ __PACKAGE__ . "::$key" } = sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=14.6
| |||||||||||||
| 80 | 0 | my $self = shift; | |||||||||||
| 81 | 0 | ▶ | $self->{$key} = $_[0] if @_; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 82 | 0 | return $self->{$key}; | |||||||||||
| 83 | } | ||||||||||||
| 84 | 2 | } | |||||||||||
| 85 | |||||||||||||
| 86 | =head1 METHODS | ||||||||||||
| 87 | |||||||||||||
| 88 | =cut | ||||||||||||
| 89 | |||||||||||||
| 90 | =head2 new URL | ||||||||||||
| 91 | |||||||||||||
| 92 | Creates a new client object that will route its requests to the URL provided. | ||||||||||||
| 93 | |||||||||||||
| 94 | =cut | ||||||||||||
| 95 | |||||||||||||
| 96 | 0 | ▶ | sub new { | ||||||||||
|
Subroutine
new: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||
| 97 | 0 | my $class = shift; | |||||||||||
| 98 | 0 | ▶ | $class = ref $class if ref $class; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 99 | 0 | my $self = bless( {}, $class ); | |||||||||||
| 100 | 0 | ▶ | if (@_) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 101 | 0 | my $rpc_url = shift; | |||||||||||
| 102 | 0 | ▶ | my $cli_class = $rpc_url =~m%\w+://% ? 'RPC::XML::Client': 'Net::RTorrent::Socket'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 103 | 0 | $self->_cli( $cli_class->new($rpc_url) ); | |||||||||||
| 104 | |||||||||||||
| 105 | } | ||||||||||||
| 106 | else { | ||||||||||||
| 107 | 0 | carp "need xmlrpc server URL"; | |||||||||||
| 108 | 0 | return; | |||||||||||
| 109 | } | ||||||||||||
| 110 | 0 | return $self; | |||||||||||
| 111 | } | ||||||||||||
| 112 | |||||||||||||
| 113 | =head2 create \$raw_data || new IO::File , [ start_now=>1||0 ],[ tag=><string>] | ||||||||||||
| 114 | |||||||||||||
| 115 | Load torrent from file descriptor or scalar ref. | ||||||||||||
| 116 | |||||||||||||
| 117 | Params: | ||||||||||||
| 118 | |||||||||||||
| 119 | =over 2 | ||||||||||||
| 120 | |||||||||||||
| 121 | =item start_now - start torent now | ||||||||||||
| 122 | |||||||||||||
| 123 | 1 - start download now (default) | ||||||||||||
| 124 | |||||||||||||
| 125 | 0 - not start download | ||||||||||||
| 126 | |||||||||||||
| 127 | =item tag - save <string> to rtorrent | ||||||||||||
| 128 | |||||||||||||
| 129 | For read tag value use: | ||||||||||||
| 130 | |||||||||||||
| 131 | $ditem->tag | ||||||||||||
| 132 | |||||||||||||
| 133 | =back | ||||||||||||
| 134 | |||||||||||||
| 135 | =cut | ||||||||||||
| 136 | |||||||||||||
| 137 | 0 | ▶ | sub create { | ||||||||||
|
Subroutine
create: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 138 | 0 | my $self = shift; | |||||||||||
| 139 | 0 | my $res = $self->load_raw(@_); | |||||||||||
| 140 | 0 | return $res | |||||||||||
| 141 | } | ||||||||||||
| 142 | |||||||||||||
| 143 | |||||||||||||
| 144 | 0 | ▶ | sub load_raw { | ||||||||||
|
Subroutine
load_raw: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||
| 145 | 0 | my $self = shift; | |||||||||||
| 146 | 0 | my ( $raw, %flg ) = @_; | |||||||||||
| 147 | 0 | ▶ | $flg{start_now} = 1 unless defined $flg{start_now}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 148 | 0 | ▶ | my $command = $flg{start_now} ? 'load_raw_start' : 'load_raw'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 149 | 0 | my @add =(); | |||||||||||
| 150 | 0 | ▶ | push @add, "d.set_custom2=$flg{tag}" if exists $flg{tag}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 151 | 0 | return $self->_cli->send_request( $command, RPC::XML::base64->new($raw), @add ); | |||||||||||
| 152 | } | ||||||||||||
| 153 | |||||||||||||
| 154 | |||||||||||||
| 155 | =head2 delete (<info_hash1>[, <info_hash2> ... ]) | ||||||||||||
| 156 | |||||||||||||
| 157 | Call d.erase on I<info_hashes>. | ||||||||||||
| 158 | |||||||||||||
| 159 | return { <info_hashes> => <xml-rpc response value> } | ||||||||||||
| 160 | |||||||||||||
| 161 | =cut | ||||||||||||
| 162 | |||||||||||||
| 163 | 0 | ▶ | sub _delete { | ||||||||||
|
Subroutine
_delete: not called CC=4 SCAR=30.0
| |||||||||||||
| 164 | 0 | my $self = shift; | |||||||||||
| 165 | 0 | ▶ | my (@ids) = map { ref($_) ? $_->{id} : $_ } @_; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 166 | 0 | my %res = (); | |||||||||||
| 167 | 0 | for (@ids) { | |||||||||||
| 168 | 0 | my $resp = $self->_cli->send_request( 'd.erase', $_ ); | |||||||||||
| 169 | 0 | ▶ | if ( ref $resp ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 170 | 0 | $res{$_} = $resp->value; | |||||||||||
| 171 | } | ||||||||||||
| 172 | } | ||||||||||||
| 173 | 0 | return \%res; | |||||||||||
| 174 | } | ||||||||||||
| 175 | |||||||||||||
| 176 | |||||||||||||
| 177 | |||||||||||||
| 178 | =head2 list_ids ( [ <name of view> ]) | ||||||||||||
| 179 | |||||||||||||
| 180 | Return list of rtorrent I<info_hashes> for I<name of view>. | ||||||||||||
| 181 | An empty string for I<name of view> equals "default". | ||||||||||||
| 182 | |||||||||||||
| 183 | To get list of views names : | ||||||||||||
| 184 | |||||||||||||
| 185 | xmlrpc http://10.100.0.1:8080/scgitest view_list | ||||||||||||
| 186 | |||||||||||||
| 187 | 'main' | ||||||||||||
| 188 | 'default' | ||||||||||||
| 189 | 'name' | ||||||||||||
| 190 | 'started' | ||||||||||||
| 191 | 'stopped' | ||||||||||||
| 192 | 'complete' | ||||||||||||
| 193 | 'incomplete' | ||||||||||||
| 194 | 'hashing' | ||||||||||||
| 195 | 'seeding' | ||||||||||||
| 196 | 'scheduler' | ||||||||||||
| 197 | |||||||||||||
| 198 | =cut | ||||||||||||
| 199 | |||||||||||||
| 200 | 0 | ▶ | sub list_ids { | ||||||||||
|
Subroutine
list_ids: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 201 | 0 | my $self = shift; | |||||||||||
| 202 | 0 | my $cli = $self->_cli; | |||||||||||
| 203 | 0 | ▶ | my $resp = $cli->send_request('download_list',shift ||"default"); | ||||||||||
|
Condition0%0/2
shift() || 'default'
MC/DC0%0/1
shift() || 'default'
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||
| 204 | 0 | ▶ | return ref($resp) ? $resp->value : []; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 205 | } | ||||||||||||
| 206 | |||||||||||||
| 207 | |||||||||||||
| 208 | =head2 get_downloads [ <view name > || 'default'] | ||||||||||||
| 209 | |||||||||||||
| 210 | Return collection of downloads (L< Net::RTorrent::Downloads>). | ||||||||||||
| 211 | |||||||||||||
| 212 | To get list of view: | ||||||||||||
| 213 | |||||||||||||
| 214 | xmlrpc http://10.100.0.1:8080/scgitest view_list | ||||||||||||
| 215 | |||||||||||||
| 216 | 'main' | ||||||||||||
| 217 | 'default' | ||||||||||||
| 218 | 'name' | ||||||||||||
| 219 | 'started' | ||||||||||||
| 220 | 'stopped' | ||||||||||||
| 221 | 'complete' | ||||||||||||
| 222 | 'incomplete' | ||||||||||||
| 223 | 'hashing' | ||||||||||||
| 224 | 'seeding' | ||||||||||||
| 225 | 'scheduler' | ||||||||||||
| 226 | |||||||||||||
| 227 | =cut | ||||||||||||
| 228 | |||||||||||||
| 229 | 0 | ▶ | sub get_downloads { | ||||||||||
|
Subroutine
get_downloads: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 230 | 0 | my $self = shift; | |||||||||||
| 231 | 0 | my $view = shift; | |||||||||||
| 232 | 0 | return new Net::RTorrent::Downloads:: $self->_cli, $view; | |||||||||||
| 233 | } | ||||||||||||
| 234 | |||||||||||||
| 235 | =head2 system_stat | ||||||||||||
| 236 | |||||||||||||
| 237 | Return system stat. | ||||||||||||
| 238 | |||||||||||||
| 239 | For example: | ||||||||||||
| 240 | |||||||||||||
| 241 | print Dumper $obj->system_stat; | ||||||||||||
| 242 | |||||||||||||
| 243 | Return: | ||||||||||||
| 244 | |||||||||||||
| 245 | { | ||||||||||||
| 246 | 'library_version' => '0.11.9', | ||||||||||||
| 247 | 'max_memory_usage' => '-858993460', # at my amd64 | ||||||||||||
| 248 | 'upload_rate' => '0', | ||||||||||||
| 249 | 'name' => 'gate.home.zg:1378', | ||||||||||||
| 250 | 'memory_usage' => '115867648', | ||||||||||||
| 251 | 'download_rate' => '0', | ||||||||||||
| 252 | 'hostname' => 'gate.home.zg', | ||||||||||||
| 253 | 'pid' => '1378', | ||||||||||||
| 254 | 'client_version' => '0.7.9', | ||||||||||||
| 255 | 'safe_free_diskspace' => '652738560' | ||||||||||||
| 256 | }; | ||||||||||||
| 257 | |||||||||||||
| 258 | =cut | ||||||||||||
| 259 | |||||||||||||
| 260 | 0 | ▶ | sub system_stat { | ||||||||||
|
Subroutine
system_stat: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||
| 261 | 0 | my $self = shift; | |||||||||||
| 262 | 0 | my $comms = S_ATTRIBUTES; | |||||||||||
| 263 | 0 | my @list = @{$comms}; | |||||||||||
| 264 | 0 | my ( @res_pull, @cmd_pull ) = (); | |||||||||||
| 265 | 0 | ▶ | while ( my ( $mname, $aname ) = splice( @list, 0, 2 ) ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 266 | 0 | push @res_pull, $aname; | |||||||||||
| 267 | 0 | push @cmd_pull, $mname => []; | |||||||||||
| 268 | } | ||||||||||||
| 269 | 0 | my $call_res = $self->do_sys_mutlicall(@cmd_pull); | |||||||||||
| 270 | 0 | my %res = (); | |||||||||||
| 271 | 0 | ▶ | while ( my $tmp_res = shift @$call_res ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 272 | 0 | my $attr_name = shift @res_pull; | |||||||||||
| 273 | 0 | ▶ | $res{$attr_name} = defined $tmp_res->[1] ? $tmp_res : $tmp_res->[0]; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 274 | } | ||||||||||||
| 275 | 0 | return \%res | |||||||||||
| 276 | |||||||||||||
| 277 | } | ||||||||||||
| 278 | |||||||||||||
| 279 | =head2 do_sys_mutlicall 'method1' =>[ <param1>, .. ], ... | ||||||||||||
| 280 | |||||||||||||
| 281 | Do XML::RPC I<system.multicall>. Return ref to ARRAY of results | ||||||||||||
| 282 | |||||||||||||
| 283 | For sample. | ||||||||||||
| 284 | |||||||||||||
| 285 | print Dumper $obj->do_sys_mutlicall('system.pid'=>[], 'system.hostname'=>[]); | ||||||||||||
| 286 | |||||||||||||
| 287 | Will return: | ||||||||||||
| 288 | |||||||||||||
| 289 | [ | ||||||||||||
| 290 | [ | ||||||||||||
| 291 | '1378' | ||||||||||||
| 292 | ], | ||||||||||||
| 293 | [ | ||||||||||||
| 294 | 'gate.home.zg' | ||||||||||||
| 295 | ] | ||||||||||||
| 296 | ]; | ||||||||||||
| 297 | |||||||||||||
| 298 | =cut | ||||||||||||
| 299 | |||||||||||||
| 300 | 0 | ▶ | sub do_sys_mutlicall { | ||||||||||
|
Subroutine
do_sys_mutlicall: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 301 | 0 | my $self = shift; | |||||||||||
| 302 | 0 | my $res = []; | |||||||||||
| 303 | 0 | my @methods = (); | |||||||||||
| 304 | 0 | ▶ | while ( my ( $method, $param ) = splice( @_, 0, 2 ) ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 305 | 0 | push @methods, { methodName => $method, params => $param },; | |||||||||||
| 306 | } | ||||||||||||
| 307 | 0 | ▶ | if (@methods) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 308 | 0 | my $resp = | |||||||||||
| 309 | $self->_cli->send_request( | ||||||||||||
| 310 | new RPC::XML::request::( 'system.multicall', \@methods ) ); | ||||||||||||
| 311 | 0 | $res = $resp->value; | |||||||||||
| 312 | } | ||||||||||||
| 313 | 0 | return $res; | |||||||||||
| 314 | } | ||||||||||||
| 315 | |||||||||||||
| 316 | 2 | 1; | |||||||||||
| 317 | __END__ | ||||||||||||