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