Xunlei/Downloader.pm

filter: Statementstmt 8.4% 13 / 154 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 10.0% 3 / 30 Podpod 0.0% 0 / 24 totaltotal 6.1% 16 / 260 CC 25 SCAR 24.5
CC
25
Cov
7%
list_tasks
42.0
_control_tasks
20.0
get_general_task_info
20.0
CRAP 11.6
↑ Summary Xunlei.pm »
1 package WWW::Xunlei::Downloader;
2  
3 # ABSTRACT: Downloader Object for Xunlei Remote Service.
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 sub new {
9 3 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
10 3 my ( $client, $downloader ) = @_;
11  
12 3 my $self = { 'client' => $client, };
13 3 $self->{$_} = $downloader->{$_} for (%$downloader);
14  
15 3 bless $self, $class;
16 3 return $self;
17 }
18  
19 sub is_online {
20 0 ▶ my $self = shift;
Subroutine is_online: not called CC=1 SCAR=6.9
Pod undocumented
21  
22 0 return $self->{'online'};
23 }
24  
25 sub login {
26 0 ▶ my $self = shift;
Subroutine login: not called CC=1 SCAR=6.9
Pod undocumented
27  
28 0 my $res = $self->_request('login');
29 }
30  
31 sub get_config {
32 0 ▶ my $self = shift;
Subroutine get_config: not called CC=1 SCAR=6.9
Pod undocumented
33  
34 0 my $res = $self->_request('settings');
35 }
36  
37 sub set_config {
38 0 ▶ my $self = shift;
Subroutine set_config: not called CC=3 SCAR=24.8
Pod undocumented
39 0 my %config = @_;
40  
41 0 my $parameters = $self->get_config;
42  
43 0 for my $k ( keys %config ) {
44 0 ▶ if (defined $parameters->{$k}){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parameters->{$k}) { } 0 0 0
45 0 $parameters->{$k} = $config{$k};
46 } else {
47 0 warn "$k is not a valid config option. Discard.";
48 }
49 }
50  
51 0 my $res = $self->_request( 'settings', $parameters );
52 }
53  
54 sub unbind {
55 0 ▶ my $self = shift;
Subroutine unbind: not called CC=1 SCAR=6.9
Pod undocumented
56  
57 0 my $res = $self->_request('unbind');
58 }
59  
60 sub rename {
61 0 ▶ my $self = shift;
Subroutine rename: not called CC=1 SCAR=6.9
Pod undocumented
62 0 my ( $pid, $new_name ) = @_;
63 0 my $parameters = { 'boxname' => $new_name, };
64  
65 0 my $res = $self->_request( 'rename', $parameters );
66 }
67  
68 sub get_box_space {
69 0 ▶ my $self = shift;
Subroutine get_box_space: not called CC=2 SCAR=17.9
Pod undocumented
70  
71 0 my $res = $self->_request('boxSpace');
72 0 ▶ return wantarray ? @{ $res->{'space'} } : $res->{'space'};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
73 }
74  
75 sub list_running_tasks {
76 0 ▶ my $self = shift;
Subroutine list_running_tasks: not called CC=1 SCAR=6.9
Pod undocumented
77 0 my ($number) = @_;
78 0 return $self->list_tasks( 'running', $number );
79 }
80  
81 sub list_completed_tasks {
82 0 ▶ my $self = shift;
Subroutine list_completed_tasks: not called CC=1 SCAR=6.9
Pod undocumented
83 0 my ($number) = @_;
84 0 return $self->list_tasks( 'completed', $number );
85 }
86  
87 sub list_recycled_tasks {
88 0 ▶ my $self = shift;
Subroutine list_recycled_tasks: not called CC=1 SCAR=6.9
Pod undocumented
89 0 my ($number) = @_;
90 0 return $self->list_tasks( 'recycled', $number );
91 }
92  
93 sub list_failed_tasks {
94 0 ▶ my $self = shift;
Subroutine list_failed_tasks: not called CC=1 SCAR=6.9
Pod undocumented
95 0 my ($number) = @_;
96 0 return $self->list_tasks( 'failed', $number );
97 }
98  
99 sub list_tasks {
100 0 ▶ my $self = shift;
Subroutine list_tasks: not called CC=6 SCAR=37.4
Pod undocumented
101 0 my ( $type, $pos, $number );
102  
103 0 ▶ $type = shift || 'running';
Condition0%0/2
shift() || 'running'
l!l
00
MC/DC0%0/1
shift() || 'running'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
104 0 ▶ if ( @_ > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 0) { } 0 0 0
105 0 ( $pos, $number ) = @_;
106 }
107 else {
108 0 ($number) = @_;
109 }
110  
111 0 my %types = (
112 'running' => 0,
113 'completed' => 1,
114 'recycled' => 2,
115 'failed' => 3,
116 );
117  
118 0 $type = $types{$type};
119 0 ▶ $number ||= 20;
Condition0%0/2
$number ||= 20
l!l
00
MC/DC0%0/1
$number ||= 20
$number
Truth table0%0/2
A
A= $number
Aresult
11
00
120 0 ▶ $pos ||= 0;
Condition0%0/2
$pos ||= 0
l!l
00
MC/DC0%0/1
$pos ||= 0
$pos
Truth table0%0/2
A
A= $pos
Aresult
11
00
121  
122 0 my $parameters = {
123 'type' => $type,
124 'pos' => $pos,
125 'number' => $number,
126 };
127  
128 0 my $res = $self->_request( 'list', $parameters );
129 0 ▶ return wantarray ? @{ $res->{'tasks'} } : $res->{'tasks'};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
130 }
131  
132 sub start {
133 0 ▶ my $self = shift;
Subroutine start: not called CC=1 SCAR=6.9
Pod undocumented
134 0 my $tasks = shift;
135 0 return $self->_control_tasks( 'start', $tasks );
136 }
137  
138 sub pause {
139 0 ▶ my $self = shift;
Subroutine pause: not called CC=1 SCAR=6.9
Pod undocumented
140 0 my $tasks = shift;
141 0 return $self->_control_tasks( 'pause', $tasks );
142 }
143  
144 sub delete {
145 0 ▶ my $self = shift;
Subroutine delete: not called CC=2 SCAR=17.9
Pod undocumented
146 0 my ( $tasks, $delete_file ) = @_;
147 0 my $parameters = {
148 'deleteFile' => \1,
149 'recycleFile' => 1,
150 };
151  
152 0 ▶ $parameters->{'deleteFile'} = $delete_file ? \1 : \0;
Branch: 1 branch
BranchTrueFalseTotal
$delete_file ? : 0 0 0
153  
154 0 return $self->_control_tasks( 'del', $tasks );
155 }
156  
157 sub _control_tasks {
158 0 ▶ my $self = shift;
Subroutine _control_tasks: not called CC=4 SCAR=30.0
159 0 my ( $action, $tasks, $parameters ) = @_;
160 0 ▶ if ( ref $tasks ne 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tasks ne "ARRAY") 0 0 0
161 0 $tasks = [$tasks];
162 }
163  
164 0 my @ids;
165 0 for my $t (@$tasks) {
166 0 push @ids, join( '_', $t->{'id'}, $t->{'state'} );
167 }
168  
169 0 $parameters->{'tasks'} = join( ',', @ids );
170  
171 0 my $res = $self->_request( $action, $parameters );
172 0 ▶ return wantarray ? @{ $res->{'tasks'} } : $res->{'tasks'};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
173 }
174  
175 sub open_lixian_channel {
176 0 ▶ my $self = shift;
Subroutine open_lixian_channel: not called CC=1 SCAR=6.9
Pod undocumented
177 0 my $task_id = shift;
178 0 my $parameters = {
179 'open' => \1,
180 'taskid' => $task_id,
181 };
182  
183 0 return $self->_request( 'openLixianChannel', $parameters );
184 }
185  
186 sub open_vip_channel {
187 0 ▶ my $self = shift;
Subroutine open_vip_channel: not called CC=1 SCAR=6.9
Pod undocumented
188 0 my $task_id = shift;
189 0 my $parameters = { 'taskid' => $task_id, };
190  
191 0 return $self->_request( 'openVipChannel', $parameters );
192 }
193  
194 sub url_check {
195 0 ▶ my $self = shift;
Subroutine url_check: not called CC=1 SCAR=6.9
Pod undocumented
196 0 my ( $url, $type ) = @_;
197  
198 0 my $parameters = {
199 'url' => $url,
200 'type' => $type,
201 };
202  
203 0 my $res = $self->_request( 'urlCheck', $parameters );
204 }
205  
206 sub url_resolve {
207 0 ▶ my $self = shift;
Subroutine url_resolve: not called CC=1 SCAR=6.9
Pod undocumented
208 0 my $url = shift;
209  
210 0 my $data = { 'url' => $url };
211  
212 0 my $res = $self->_request( 'urlResolve', undef, $data );
213 0 return $res;
214 }
215  
216 sub get_general_task_info {
217 0 ▶ my $self = shift;
Subroutine get_general_task_info: not called CC=4 SCAR=30.0
Pod undocumented
218  
219 0 my ( $url, $filename ) = @_;
220  
221 0 ▶ unless ( $url =~ /^(http|https|ftp|magnet|ed2k|thunder|mms|rtsp)\:.+/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url =~ /^(http|https|ftp|magnet|ed2k|thunder|mms|rtsp)\:.+/) 0 0 0
222 0 die "Not a valid URL.";
223 }
224  
225 0 my $task = {
226 'gcid' => "",
227 'cid' => '',
228 'filesize' => 0,
229 'ext_json' => { 'autoname' => 1 },
230 };
231  
232 0 my $res = $self->url_resolve($url)->{'taskInfo'};
233 $task = {
234 'url' => $res->{'url'},
235 'name' => $res->{'name'},
236 0 'filesize' => $res->{'size'},
237 };
238  
239 0 ▶ if ( $filename && $task->{'name'} ne $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename and $task->{"name"} ne $filename) 0 0 0
Condition0%0/3
$filename and $task->{"name"} ne $filename
!ll&&!rl&&r
000
MC/DC0%0/2
$filename and $task->{"name"} ne $filename
$filename $task->{"name"} ne $filename
Truth table0%0/3
A and B
A= $filenameB= $task->{"name"} ne $filename
ABresult
0X0
100
111
240 0 $task->{'name'} = $filename;
241 0 $task->{'ext_json'}->{'autoname'} = 0;
242 }
243  
244 0 return $task;
245 }
246  
247 sub create_task {
248 0 ▶ my $self = shift;
Subroutine create_task: not called CC=2 SCAR=17.9
Pod undocumented
249 0 my ( $url, $filename, $path ) = @_;
250  
251 0 my $task = $self->get_general_task_info( $url, $filename );
252  
253 0 my $res = $self->_create_general_tasks( [$task], $path );
254 0 ▶ return wantarray ? @{ $res->{'tasks'} } : $res->{'tasks'};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
255 }
256  
257 sub create_tasks {
258 0 ▶ my $self = shift;
Subroutine create_tasks: not called CC=3 SCAR=24.8
Pod undocumented
259 0 my ( $urls, $path ) = @_;
260  
261 0 my @tasks;
262 0 for my $url (@$urls) {
263 0 my $task_info = $self->url_resolve($url);
264 0 ▶ if ( $task_info->{'taskInfo'}->{'type'} == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($task_info->{'taskInfo'}{'type'} == 2) { } 0 0 0
265 0 my @btsub = map { $_->{'id'} }
266 0 grep { auto_select($_) }
267 0 @{ $task_info->{'taskInfo'}->{'subList'} };
268  
269 $self->_create_bt_task(
270 $task_info->{'taskInfo'}->{'name'},
271 0 $task_info->{'infohash'},
272 \@btsub, $path
273 );
274 }
275 else {
276 0 push @tasks, $self->get_general_task_info($url);
277 }
278 }
279  
280 0 $self->_create_general_tasks( \@tasks, $path );
281  
282 #return wantarray ? @{ $res->{'tasks'} } : $res->{'tasks'};
283 }
284  
285 sub _create_general_tasks {
286 0 ▶ my $self = shift;
Subroutine _create_general_tasks: not called CC=2 SCAR=17.9
287 0 my ( $tasks, $path ) = @_;
288  
289 0 my $data;
290 0 $data->{'tasks'} = $tasks;
291 0 ▶ $data->{'path'} = $path || $self->get_config->{'defaultPath'};
Condition0%0/3
$path || $self->get_config->{'defaultPath'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$path || $self->get_config->{'defaultPath'}
$path $self->get_config->{'defaultPath'}
Truth table0%0/3
A || B
A= $pathB= $self->get_config->{'defaultPath'}
ABresult
1X1
011
000
292  
293 0 my $res = $self->_request( 'createTask', undef, $data );
294 }
295  
296 sub _create_bt_task {
297 0 ▶ my $self = shift;
Subroutine _create_bt_task: not called CC=2 SCAR=17.9
298 0 my ( $name, $infohash, $btsub, $path ) = @_;
299  
300 0 ▶ $path ||= $self->get_config->{'defaultPath'};
Condition0%0/3
$path ||= $self->get_config->{"defaultPath"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$path ||= $self->get_config->{"defaultPath"}
$path $self->get_config->{"defaultPath"}
Truth table0%0/3
A ||= B
A= $pathB= $self->get_config->{"defaultPath"}
ABresult
1X1
011
000
301 0 my $data = {
302 'name' => $name,
303 'infohash' => $infohash,
304 'btSub' => $btsub,
305 'path' => $path,
306 };
307  
308 0 my $res = $self->_request( 'createBtTask', undef, $data );
309 }
310  
311 sub auto_select {
312 0 ▶ my $btsub = shift;
Subroutine auto_select: not called CC=3 SCAR=24.8
Pod undocumented
313  
314 0 ▶ return 0 if $btsub->{'size'} < 15360;
Branch: 1 branch
BranchTrueFalseTotal
if $btsub->{"size"} < 15360 0 0 0
315 0 ▶ return 0 if $btsub->{'name'} =~ /txt|html|htm|url$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $btsub->{"name"} =~ /txt|html|htm|url$/i 0 0 0
316 0 return 1;
317 }
318  
319 sub _request {
320 0 ▶ my $self = shift;
Subroutine _request: not called CC=2 SCAR=17.9
321 0 my ( $action, $parameters, $data ) = @_;
322  
323 0 $parameters->{'pid'} = $self->{'pid'};
324  
325 0 ▶ unless ( $self->is_online ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_online) 0 0 0
326 0 die "Downloader is not Online. Please check Xunlei Remote Service.";
327 }
328  
329 0 my $res = $self->{'client'}->_yc_request( $action, $parameters, $data );
330 0 return $res;
331 }
332  
333 1 1;
334  
335 __END__
↑ Summary Xunlei.pm »