line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#$Id: RTorrent.pm 938 2011-04-05 07:12:53Z zag $ |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package Net::RTorrent; |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
29761
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
185
|
|
6
|
1
|
|
|
1
|
|
7
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
30
|
|
7
|
1
|
|
|
1
|
|
464
|
use RPC::XML; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
use RPC::XML::Client; |
9
|
|
|
|
|
|
|
use Net::RTorrent::Downloads; |
10
|
|
|
|
|
|
|
use Net::RTorrent::Socket; |
11
|
|
|
|
|
|
|
use Collection; |
12
|
|
|
|
|
|
|
our @ISA = (); |
13
|
|
|
|
|
|
|
use Carp; |
14
|
|
|
|
|
|
|
use 5.005; |
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
|
|
|
|
|
|
|
use constant { |
57
|
|
|
|
|
|
|
#info atributes for system info |
58
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
}; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
our $VERSION = '0.11'; |
73
|
|
|
|
|
|
|
my $attrs = { |
74
|
|
|
|
|
|
|
_cli => undef, |
75
|
|
|
|
|
|
|
}; |
76
|
|
|
|
|
|
|
### install get/set accessors for this object. |
77
|
|
|
|
|
|
|
for my $key ( keys %$attrs ) { |
78
|
|
|
|
|
|
|
no strict 'refs'; |
79
|
|
|
|
|
|
|
*{ __PACKAGE__ . "::$key" } = sub { |
80
|
|
|
|
|
|
|
my $self = shift; |
81
|
|
|
|
|
|
|
$self->{$key} = $_[0] if @_; |
82
|
|
|
|
|
|
|
return $self->{$key}; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
} |
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
|
|
|
|
|
|
|
sub new { |
97
|
|
|
|
|
|
|
my $class = shift; |
98
|
|
|
|
|
|
|
$class = ref $class if ref $class; |
99
|
|
|
|
|
|
|
my $self = bless( {}, $class ); |
100
|
|
|
|
|
|
|
if (@_) { |
101
|
|
|
|
|
|
|
my $rpc_url = shift; |
102
|
|
|
|
|
|
|
my $cli_class = $rpc_url =~m%\w+://% ? 'RPC::XML::Client': 'Net::RTorrent::Socket'; |
103
|
|
|
|
|
|
|
$self->_cli( $cli_class->new($rpc_url) ); |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
else { |
107
|
|
|
|
|
|
|
carp "need xmlrpc server URL"; |
108
|
|
|
|
|
|
|
return; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
return $self; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head2 create \$raw_data || new IO::File , [ start_now=>1||0 ],[ tag=>] |
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 to rtorrent |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
For read tag value use: |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
$ditem->tag |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=back |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=cut |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
sub create { |
138
|
|
|
|
|
|
|
my $self = shift; |
139
|
|
|
|
|
|
|
my $res = $self->load_raw(@_); |
140
|
|
|
|
|
|
|
return $res |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
sub load_raw { |
145
|
|
|
|
|
|
|
my $self = shift; |
146
|
|
|
|
|
|
|
my ( $raw, %flg ) = @_; |
147
|
|
|
|
|
|
|
$flg{start_now} = 1 unless defined $flg{start_now}; |
148
|
|
|
|
|
|
|
my $command = $flg{start_now} ? 'load_raw_start' : 'load_raw'; |
149
|
|
|
|
|
|
|
my @add =(); |
150
|
|
|
|
|
|
|
push @add, "d.set_custom2=$flg{tag}" if exists $flg{tag}; |
151
|
|
|
|
|
|
|
return $self->_cli->send_request( $command, RPC::XML::base64->new($raw), @add ); |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head2 delete ([, ... ]) |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
Call d.erase on I. |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
return { => } |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=cut |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
sub _delete { |
164
|
|
|
|
|
|
|
my $self = shift; |
165
|
|
|
|
|
|
|
my (@ids) = map { ref($_) ? $_->{id} : $_ } @_; |
166
|
|
|
|
|
|
|
my %res = (); |
167
|
|
|
|
|
|
|
for (@ids) { |
168
|
|
|
|
|
|
|
my $resp = $self->_cli->send_request( 'd.erase', $_ ); |
169
|
|
|
|
|
|
|
if ( ref $resp ) { |
170
|
|
|
|
|
|
|
$res{$_} = $resp->value; |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
return \%res; |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=head2 list_ids ( [ ]) |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
Return list of rtorrent I for I. |
181
|
|
|
|
|
|
|
An empty string for I 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
|
|
|
|
|
|
|
sub list_ids { |
201
|
|
|
|
|
|
|
my $self = shift; |
202
|
|
|
|
|
|
|
my $cli = $self->_cli; |
203
|
|
|
|
|
|
|
my $resp = $cli->send_request('download_list',shift ||"default"); |
204
|
|
|
|
|
|
|
return ref($resp) ? $resp->value : []; |
205
|
|
|
|
|
|
|
} |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
=head2 get_downloads [ || '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
|
|
|
|
|
|
|
sub get_downloads { |
230
|
|
|
|
|
|
|
my $self = shift; |
231
|
|
|
|
|
|
|
my $view = shift; |
232
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
sub system_stat { |
261
|
|
|
|
|
|
|
my $self = shift; |
262
|
|
|
|
|
|
|
my $comms = S_ATTRIBUTES; |
263
|
|
|
|
|
|
|
my @list = @{$comms}; |
264
|
|
|
|
|
|
|
my ( @res_pull, @cmd_pull ) = (); |
265
|
|
|
|
|
|
|
while ( my ( $mname, $aname ) = splice( @list, 0, 2 ) ) { |
266
|
|
|
|
|
|
|
push @res_pull, $aname; |
267
|
|
|
|
|
|
|
push @cmd_pull, $mname => []; |
268
|
|
|
|
|
|
|
} |
269
|
|
|
|
|
|
|
my $call_res = $self->do_sys_mutlicall(@cmd_pull); |
270
|
|
|
|
|
|
|
my %res = (); |
271
|
|
|
|
|
|
|
while ( my $tmp_res = shift @$call_res ) { |
272
|
|
|
|
|
|
|
my $attr_name = shift @res_pull; |
273
|
|
|
|
|
|
|
$res{$attr_name} = defined $tmp_res->[1] ? $tmp_res : $tmp_res->[0]; |
274
|
|
|
|
|
|
|
} |
275
|
|
|
|
|
|
|
return \%res |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
} |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
=head2 do_sys_mutlicall 'method1' =>[ , .. ], ... |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
Do XML::RPC I. 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
|
|
|
|
|
|
|
sub do_sys_mutlicall { |
301
|
|
|
|
|
|
|
my $self = shift; |
302
|
|
|
|
|
|
|
my $res = []; |
303
|
|
|
|
|
|
|
my @methods = (); |
304
|
|
|
|
|
|
|
while ( my ( $method, $param ) = splice( @_, 0, 2 ) ) { |
305
|
|
|
|
|
|
|
push @methods, { methodName => $method, params => $param },; |
306
|
|
|
|
|
|
|
} |
307
|
|
|
|
|
|
|
if (@methods) { |
308
|
|
|
|
|
|
|
my $resp = |
309
|
|
|
|
|
|
|
$self->_cli->send_request( |
310
|
|
|
|
|
|
|
new RPC::XML::request::( 'system.multicall', \@methods ) ); |
311
|
|
|
|
|
|
|
$res = $resp->value; |
312
|
|
|
|
|
|
|
} |
313
|
|
|
|
|
|
|
return $res; |
314
|
|
|
|
|
|
|
} |
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
1; |
317
|
|
|
|
|
|
|
__END__ |