blib/lib/Net/uTorrent.pm

filter: Statementstmt 16.8% 42 / 249 Branchbran 0.0% 0 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 29.0% 9 / 31 Podpod 90.9% 20 / 22 totaltotal 21.9% 71 / 324 CC 35 SCAR 29.2
CC
35
Cov
15%
get_settings
56.0
filelist
56.0
get_properties
30.0
CRAP 18.5
↑ Summary
1 package Net::uTorrent;
2  
3 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use URI::QueryParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use HTML::TreeBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use 5.010000;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 require Exporter;
15  
16 1 our @ISA = qw(Exporter);
17  
18 1 our %EXPORT_TAGS = ( 'all' => [ qw() ] );
19  
20 1 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
21  
22 1 our @EXPORT = qw();
23  
24 1 our $VERSION = '0.02';
25  
26  
27 1 our $token;
28 1 our $api_url;
29 1 our $http_request = HTTP::Request->new;
30 1 our $ua = LWP::UserAgent->new;
31 1 our $html_tree = HTML::TreeBuilder->new;
32 1 our $json = JSON::XS->new;
33 1 our $cid; #cache id
34 1 our $error;
35  
36  
37  
38 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
39 0 my ($class,%args) = @_;
40 0 my $self = bless({}, $class);
41  
42 0 my $host = $args{'hostname'};
43 0 my $port = $args{'port'};
44 0 my $user = $args{'user'};
45 0 my $pass = $args{'pass'};
46
47 0 $ua->credentials (
48 "$host:$port",
49 'uTorrent',
50 $user => $pass
51 );
52 0 $api_url = "http://$host:$port/gui/";
53 0 my $http = $ua->get($api_url."token.html");
54 0 my $parsed = $html_tree->parse($http->decoded_content); # Build HTML tree of elements
55 0 $token = $parsed->{'_body'}->{'_content'}[0]->{'_content'}[0]; # Get authentication token
56  
57 0 return $self;
58 }
59
60
61 0 ▶sub token {
Subroutine token: not called CC=1 SCAR=6.9
Pod documented
62 0 return $token;
63 }
64  
65 0 ▶sub login_success {
Subroutine login_success: not called CC=2 SCAR=17.9
Pod documented
66  
67 0 ▶ if ($token) { return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($Net::uTorrent::token) { } 0 0 0
68 0 else { return 0; }
69  
70 }
71
72 0 ▶sub torrents {
Subroutine torrents: not called CC=3 SCAR=24.8
Pod documented
73 0 my ($class,%args) = @_;
74
75 0 my %params = (
76 list => 1,
77 );
78 0 %params = (%params,%args); #Add optional cache id from argument.
79 0 my @args = qw();
80 0 push @args,\%params;
81 0 my $json_text = api_query_result(@args);
82 0 my $decoded_json = $json->decode($json_text);
83 0 $cid = $decoded_json->{torrentc};
84
85 0 my @torrent = qw();
86
87 0 for (@{$decoded_json->{torrents}}) {
88 0 my @keys = qw (
89 infohash status name size percent downloaded
90 uploaded ratio upstream downstream eta label
91 peers_connected peers_in_swarm seeds_connected
92 seeds_in_swarm availability queue_order remaining
93 );
94 0 my @values = @$_;
95 0 my %torrent_details;
96 0 ▶ for (my $i = 0; $i <= $#keys; $i++) { $torrent_details{$keys[$i]} = $values[$i]; }
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#keys) 0 0 0
97 0 push @torrent,\%torrent_details;
98  
99 }
100 0 return \@torrent;
101 }
102
103 0 ▶sub cache_id {
Subroutine cache_id: not called CC=1 SCAR=6.9
Pod documented
104 0 return $cid;
105 }
106  
107 0 ▶sub get_settings {
Subroutine get_settings: not called CC=7 SCAR=40.3
Pod documented
108 0 my ($class,$type) = @_;
109 0 ▶ $type = 'array' unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 0 0
110 0 my @settings;
111 my %settings;
112 0 my @type = qw (integer boolean string);
113  
114 0 my @args = qw();
115 0 push @args,{action => 'getsettings'};
116 0 my $json_text = api_query_result(@args);
117 0 my $decoded_json = $json->decode($json_text);
118
119 0 for (@{$decoded_json->{settings}}) {
120 0 my %settings_info;
121 0 my @keys = qw (name type value);
122 0 my @values = @$_;
123 0 ▶ for (my $i = 0; $i <= $#keys; $i++) { $settings_info{$keys[$i]} = $values[$i]; }
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#keys) 0 0 0
124 0 $settings_info{'type_human'} = $type[$settings_info{'type'}];
125 0 $settings{$settings_info{'name'}} = \%settings_info;
126 0 ▶ if ($type eq 'hash') { delete $settings_info{'name'}; }
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "hash") 0 0 0
127 0 push @settings,\%settings_info;
128 }
129 0 ▶ if ($type eq 'array') { return \@settings; }
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'array') { } 0 0 0
elsif ($type eq 'hash') { } 0 0 0
130 0 elsif ($type eq 'hash') { return \%settings; }
131 }
132  
133 0 ▶sub set_settings {
Subroutine set_settings: not called CC=2 SCAR=17.9
Pod documented
134 0 my ($class,%settings) = @_;
135 0 my @args;
136 0 push @args, {action => 'setsetting'};
137 0 for my $key (keys %settings) {
138 0 push @args, { 's' => $key };
139 0 push @args, { 'v' => $settings{$key} };
140 }
141 0 api_query_result(@args);
142 0 return;
143 }
144 0 ▶sub filelist {
Subroutine filelist: not called CC=7 SCAR=40.3
Pod documented
145 0 my ($class,@infohash) = @_;
146 0 my @args;
147 0 my @priority_human = qw(skip low normal high);
148 0 my @keys = qw(filename size downloaded priority);
149
150 0 push @args,{action => 'getfiles'};
151 0 for my $hash (@infohash) { push @args,{hash => $hash}; }
152 0 my $json_text = api_query_result(@args);
153 0 my $i = 0; for ($json_text =~ /,"(files)":/g) { my $append = $i++; $json_text =~ s/,"(files)":/,"files$append":/; } # Ugly hack to avoid repeating keys due to bug in uTorrent's JSON generator
154 0 my $decoded_json = $json->decode($json_text);
155 0 my %torrent_hash;
156 0 for my $key (keys %$decoded_json) {
157 0 ▶ if ($key ne 'build') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key ne "build") 0 0 0
158 0 my $torrent = $decoded_json->{$key};
159 0 my $infohash = $$torrent[0];
160 0 my $file_array = $$torrent[1];
161 0 my @files_in_torrent = qw();
162 0 for my $file (@$file_array) {
163 0 my %fileinfo;
164 0 ▶ for (my $i = 0; $i <= $#$file; $i++) { $fileinfo{$keys[$i]} = $$file[$i]; }
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $file->$#*) 0 0 0
165 0 $fileinfo{'priority_human'} = $priority_human[$fileinfo{'priority'}];
166 0 push @files_in_torrent,\%fileinfo;
167
168 }
169 0 $torrent_hash{$infohash} = \@files_in_torrent;
170 }
171  
172 }
173 0 return \%torrent_hash;
174 }
175  
176 0 ▶sub get_properties {
Subroutine get_properties: not called CC=5 SCAR=34.0
Pod documented
177  
178 0 my ($class,@infohash) = @_;
179 0 my @args;
180 0 push @args,{action => 'getprops'};
181 0 for my $hash (@infohash) { push @args,{hash => $hash}; }
182 0 my $json_text = api_query_result(@args);
183 0 my $i = 0; for ($json_text =~ /,"(props)":/g) { my $append = $i++; $json_text =~ s/,"(props)":/,"props$append":/; } # Ugly hack to avoid repeating keys due to bug in uTorrent's JSON generator
184 0 my $decoded_json = $json->decode($json_text);
185 0 my %properties;
186 0 for my $key (keys %$decoded_json) {
187 0 ▶ if ($key ne 'build') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key ne "build") 0 0 0
188 0 my $property = $decoded_json->{$key};
189 0 my @trackers = split(/\s+/,$$property[0]->{trackers});
190 0 $$property[0]->{trackers} = \@trackers;
191 0 $properties{$$property[0]->{hash}} = $$property[0];
192 }
193 }
194 0 return \%properties;
195 }
196  
197 0 ▶sub set_properties {
Subroutine set_properties: not called CC=3 SCAR=24.8
Pod documented
198 0 my ($class,@properties) = @_;
199 0 my @args;
200 0 push @args,{action => 'setprops'};
201 0 for my $property (@properties) {
202 0 push @args, { hash => $$property{'hash'}};
203 0 delete $$property{'hash'};
204 0 for my $key (keys %$property) {
205 0 push (@args, { 's' => $key } );
206 0 push (@args, { 'v' => $$property{$key} } );
207 }
208 }
209 0 api_query_result(@args);
210 0 return;
211  
212  
213  
214 }
215  
216 0 ▶sub start {
Subroutine start: not called CC=2 SCAR=17.9
Pod documented
217 0 my ($class,@infohash) = @_;
218 0 my @args;
219 0 push @args, {action => 'start'};
220 0 for (@infohash) { push @args, { hash => $_ }; }
221 0 api_query_result(@args);
222 }
223  
224  
225 0 ▶sub stop {
Subroutine stop: not called CC=2 SCAR=17.9
Pod documented
226 0 my ($class,@infohash) = @_;
227 0 my @args;
228 0 push @args, {action => 'stop'};
229 0 for (@infohash) { push @args, { hash => $_ }; }
230 0 api_query_result(@args);
231  
232 }
233  
234 0 ▶sub pause {
Subroutine pause: not called CC=2 SCAR=17.9
Pod documented
235 0 my ($class,@infohash) = @_;
236 0 my @args;
237 0 push @args, {action => 'pause'};
238 0 for (@infohash) { push @args, { hash => $_ }; }
239 0 api_query_result(@args);
240  
241 }
242  
243 0 ▶sub resume {
Subroutine resume: not called CC=2 SCAR=17.9
Pod documented
244 0 my ($class,@infohash) = @_;
245 0 my @args;
246 0 push @args, {action => 'unpause'};
247 0 for (@infohash) { push @args, { hash => $_ }; }
248 0 api_query_result(@args);
249  
250 }
251  
252 0 ▶sub force_start {
Subroutine force_start: not called CC=2 SCAR=17.9
Pod documented
253 0 my ($class,@infohash) = @_;
254 0 my @args;
255 0 push @args, {action => 'forcestart'};
256 0 for (@infohash) { push @args, { hash => $_ }; }
257 0 api_query_result(@args);
258  
259  
260 }
261  
262 0 ▶sub recheck {
Subroutine recheck: not called CC=2 SCAR=17.9
Pod documented
263 0 my ($class,@infohash) = @_;
264 0 my @args;
265 0 push @args, {action => 'recheck'};
266 0 for (@infohash) { push @args, { hash => $_ }; }
267 0 api_query_result(@args);
268  
269 }
270  
271 0 ▶sub remove_torrent {
Subroutine remove_torrent: not called CC=2 SCAR=17.9
Pod documented
272 0 my ($class,@infohash) = @_;
273 0 my @args;
274 0 push @args, {action => 'remove'};
275 0 for (@infohash) { push @args, { hash => $_ }; }
276 0 api_query_result(@args);
277  
278  
279 }
280  
281  
282 0 ▶sub remove_data {
Subroutine remove_data: not called CC=2 SCAR=17.9
Pod documented
283 0 my ($class,@infohash) = @_;
284 0 my @args;
285 0 push @args, {action => 'removedata'};
286 0 for (@infohash) { push @args, { hash => $_ }; }
287 0 api_query_result(@args);
288  
289  
290 }
291  
292  
293 0 ▶sub set_priority {
Subroutine set_priority: not called CC=2 SCAR=17.9
Pod documented
294 0 my ($class,%args_hash) = @_;
295 0 my @args;
296 0 push @args, { action => 'setprio' };
297 0 push @args, { hash => $args_hash{'hash'} };
298 0 push @args, { p => $args_hash{'priority'} };
299 0 delete $args_hash{'hash'};
300 0 for (@{$args_hash{'files'}}) { push @args, { f => $_}; }
301 0 api_query_result(@args);
302 0 return;
303 }
304  
305  
306 0 ▶sub add_url {
Subroutine add_url: not called CC=1 SCAR=6.9
Pod documented
307 0 my ($class,$url) = @_;
308 0 my @args;
309 0 push @args, { action => 'add-url' };
310 0 push @args, { 's' => $url };
311 0 api_query_result(@args);
312 0 return;
313 }
314  
315  
316 0 ▶sub add_file {
Subroutine add_file: not called CC=2 SCAR=17.9
Pod documented
317 0 my ($class,$file) = @_;
318 0 my $api = URI->new($api_url);
319 0 $api->query_param(action => 'add-file');
320 0 $api->query_param(t => $token);
321
322 # This is the only method that uses HTTP POST, so a separate subroutine is not necessary.
323
324 0 my $request = $ua->request (
325 POST $api,
326 Content_Type => 'multipart/form-data',
327 Content => [
328 'torrent_file' => [$file]
329 ]
330
331 );
332  
333 0 my $json_text = $request->content;
334 0 my $decoded_json = $json->decode($json_text);
335 0 my $status = $$decoded_json{error};
336 0 ▶ $status = 'Success' unless $status;
Branch: 1 branch
BranchTrueFalseTotal
unless $status 0 0 0
337 0 return { Status => $status};
338  
339 }
340  
341 ############################### Module's own sub routines ###############################
342  
343 0 ▶sub api_query_result {
Subroutine api_query_result: not called CC=3 SCAR=24.8
Pod undocumented
344 0 my (@params) = @_;
345 0 my $api = URI->new($api_url);
346 0 for my $array_value (@params) {
347 0 for my $key (keys %$array_value) {
348 0 $api->query_param_append($key => $$array_value{$key});
349 }
350 }
351  
352 0 $api->query_param(token => $token);
353 0 my $http = $ua->get($api);
354 0 return $http->decoded_content;
355 }
356  
357  
358
359 1 1;
360  
361 __END__
↑ Summary