Xunlei/Downloader.pm

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