Xunlei.pm

filter: Statementstmt 87.1% 183 / 210 Branchbran 57.5% 23 / 40 Conditioncond 50.0% 9 / 18 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 90.7% 39 / 43 Podpod 42.8% 3 / 7 totaltotal 78.6% 258 / 328 CC 32 SCAR 12.3
CC
32
Cov
80%
_login
12.0
_request
8.5
_delete_temp_cookies
6.0
CRAP 3.4
« Xunlei/Downloader.pm ↑ Summary
1 package WWW::Xunlei;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Path qw/mkpath/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Time::HiRes qw/gettimeofday/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use POSIX qw/strftime/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Term::ANSIColor qw/:constants/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 ▶use WWW::Xunlei::Downloader;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 our $DEBUG;
22  
23 1 ▶use constant URL_LOGIN => 'http://login.xunlei.com/';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use constant URL_REMOTE => 'http://homecloud.yuancheng.xunlei.com/';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 use constant DEFAULT_USER_AGENT =>
26 "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:41.0) "
27 1 ▶ . "Gecko/20100101 Firefox/41.0";
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use constant URL_LOGIN_REFER => 'http://i.xunlei.com/login/2.5/?r_d=1';
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use constant BUSINESS_TYPE => '113';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use constant V => '2';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use constant CT => '0';
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 sub new {
34 1 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
35 1 my ( $user, $pass, %options ) = @_;
36 1 my $self = {
37 'ua' => undef,
38 'json' => undef,
39 'user' => $user,
40 'pass' => md5pass($pass),
41 };
42  
43 1 my $cookie_file = $options{'cookie_file'};
44 1 $self->{'ua'} = LWP::UserAgent->new;
45 1 $self->{'ua'}->cookie_jar( { file => $cookie_file } );
46 1 $self->{'ua'}->agent(DEFAULT_USER_AGENT);
47  
48 1 $self->{'json'} = JSON->new->allow_nonref;
49  
50 1 bless $self, $class;
51 1 return $self;
52 }
53  
54 sub list_downloaders {
55 3 ▶ my $self = shift;
Subroutine list_downloaders: called CC=4 SCAR=14.0
Pod documented
56  
57 3 my $parameters = { 'type' => 0, };
58  
59 3 my $res = $self->_yc_request( 'listPeer', $parameters );
60  
61 3 ▶ if ( $res->{'rtn'} != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{"rtn"} != 0) 0 3 3
62 0 die "Unable to get the Downloader List: $@";
63 }
64  
65 3 my @downloaders;
66 3 for my $p ( @{ $res->{'peerList'} } ) {
67 3 push @downloaders, WWW::Xunlei::Downloader->new( $self, $p );
68 }
69  
70 3 ▶ return wantarray ? @downloaders : \@downloaders;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 2 3
71 }
72  
73 sub get_downloader {
74 1 ▶ my $self = shift;
Subroutine get_downloader: called CC=2 SCAR=7.0
Pod undocumented
75
76 1 my $name = shift;
77  
78 1 my @downloaders = grep { $_->{'name'} eq $name } $self->list_downloaders();
79 1 ▶ die "No such Downloader named >>$name<<" unless @downloaders;
Branch: 1 branch
BranchTrueFalseTotal
unless @downloaders 0 1 1
80 1 return shift @downloaders;
81 }
82  
83 sub bind {
84 0 ▶ my $self = shift;
Subroutine bind: not called CC=1 SCAR=6.9
Pod documented
85  
86 0 my ( $key, $name ) = @_;
87  
88 0 my $parameters = {
89 'boxname' => $name,
90 'key' => $key,
91 };
92  
93 0 my $res = $self->_yc_request( 'bind', $parameters );
94 }
95  
96 sub unbind {
97 0 ▶ my $self = shift;
Subroutine unbind: not called CC=1 SCAR=6.9
Pod undocumented
98 0 my $pid = shift;
99  
100 0 my $res = $self->_yc_request( 'unbind', { 'pid' => $pid } );
101 }
102  
103 sub _login {
104 0 ▶ my $self = shift;
Subroutine _login: not called CC=3 SCAR=24.8
105 0 my $res;
106 0 ▶ if ( $self->_is_session_expired ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_session_expired) { } 0 0 0
107 0 $res = $self->_form_login;
108 }
109 else {
110 0 $res = $self->_session_login;
111 }
112  
113 0 ▶ die "Login Error: $res" if ( $res != 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $res != 0 0 0 0
114 0 $self->_set_auto_login();
115 0 $self->_save_cookie();
116 }
117  
118 sub _form_login {
119 1 ▶ my $self = shift;
Subroutine _form_login: called CC=1 SCAR=0.0
120 1 my $verify_code = uc $self->_get_verify_code();
121 1 $self->_debug( "Verify Code: " . $verify_code );
122 1 my $password = md5_hex( $self->{'pass'} . $verify_code );
123 my $parameters = {
124 1 'u' => $self->{'user'},
125 'p' => $password,
126 'verifycode' => $verify_code,
127 };
128  
129 # $self->{'ua'}->post(join( '/', URL_LOGIN, 'sec2login/'), $parameters);
130 1 $self->_request( 'POST', URL_LOGIN . 'sec2login/', $parameters );
131  
132 1 return $self->_get_cookie('blogresult');
133 }
134  
135 sub _session_login {
136 1 ▶ my $self = shift;
Subroutine _session_login: called CC=1 SCAR=0.0
137 1 my $parameters = { 'sessionid' => $self->_get_cookie('_x_a_') };
138 1 $self->_request( 'GET', URL_LOGIN . 'sessionid/', $parameters );
139 1 return $self->_get_cookie('blogresult');
140 }
141  
142 sub _is_logged_in {
143 5 ▶ my $self = shift;
Subroutine _is_logged_in: called CC=2 SCAR=8.1
144 5 ▶ return ( $self->_get_cookie('sessionid')
Condition33%1/3
$self->_get_cookie('sessionid') && $self->_get_cookie('userid')
!ll&&!rl&&r
005
MC/DC0%0/2
$self->_get_cookie('sessionid') && $self->_get_cookie('userid')
$self->_get_cookie('sessionid') $self->_get_cookie('userid')
Truth table33%1/3
A && B
A= $self->_get_cookie('sessionid')B= $self->_get_cookie('userid')
ABresult
0X0
100
111
145 && $self->_get_cookie('userid') );
146 }
147  
148 sub _is_session_expired {
149 3 ▶ my $self = shift;
Subroutine _is_session_expired: called CC=2 SCAR=6.9
150  
151 my $session_expired_time
152 = $self->{'ua'}
153 3 ->{'cookie_jar'}{'COOKIES'}{'.xunlei.com'}{'/'}{'_x_a_'}[5];
154 3 ▶ return 1 unless $session_expired_time;
Branch: 1 branch
BranchTrueFalseTotal
unless $session_expired_time 2 1 3
155 1 return (gettimeofday)[0] > $session_expired_time;
156 }
157  
158 sub _get_verify_code {
159 2 ▶ my $self = shift;
Subroutine _get_verify_code: called CC=1 SCAR=0.0
160 my $parameters = {
161 2 'u' => $self->{'user'},
162 'business_type' => BUSINESS_TYPE,
163 'cachetime' => int( gettimeofday() * 1000 ),
164 };
165 2 $self->_request( 'GET', URL_LOGIN . 'check/', $parameters );
166 2 my $check_result = $self->_get_cookie('check_result');
167 2 my $verify_code = ( split( ':', $check_result ) )[1];
168 2 return $verify_code;
169 }
170  
171 sub _set_auto_login {
172 2 ▶ my $self = shift;
Subroutine _set_auto_login: called CC=1 SCAR=0.0
173 2 my $sessionid = $self->_get_cookie('sessionid');
174 2 $self->_set_cookie( '_x_a_', $sessionid, 604800 );
175 }
176  
177 sub _delete_temp_cookies {
178 0 ▶ my $self = shift;
Subroutine _delete_temp_cookies: not called CC=2 SCAR=17.9
179 my @login_cookie
180 0 = qw/VERIFY_KEY verify_type check_n check_e logindetail result/;
181 0 for my $c (@login_cookie) {
182 0 $self->_delete_cookie($c);
183 }
184 }
185  
186 sub _get_cookie {
187 21 ▶ my $self = shift;
Subroutine _get_cookie: called CC=3 SCAR=11.3
188 21 my ( $key, $domain, $path ) = @_;
189 21 ▶ $domain ||= ".xunlei.com";
Condition50%1/2
$domain ||= ".xunlei.com"
l!l
021
MC/DC0%0/1
$domain ||= ".xunlei.com"
$domain
Truth table50%1/2
A
A= $domain
Aresult
11
00
190 21 ▶ $path ||= "/";
Condition50%1/2
$path ||= "/"
l!l
021
MC/DC0%0/1
$path ||= "/"
$path
Truth table50%1/2
A
A= $path
Aresult
11
00
191 21 $self->{'ua'}->{'cookie_jar'}->{'COOKIES'}{$domain}{'/'}{$key}[1];
192 }
193  
194 sub _set_cookie {
195 5 ▶ my $self = shift;
Subroutine _set_cookie: called CC=3 SCAR=11.2
196 5 my ( $key, $value, $expire, $domain, $path ) = @_;
197 5 ▶ $domain ||= ".xunlei.com";
Condition50%1/2
$domain ||= ".xunlei.com"
l!l
05
MC/DC0%0/1
$domain ||= ".xunlei.com"
$domain
Truth table50%1/2
A
A= $domain
Aresult
11
00
198 5 ▶ $path ||= "/";
Condition50%1/2
$path ||= "/"
l!l
05
MC/DC0%0/1
$path ||= "/"
$path
Truth table50%1/2
A
A= $path
Aresult
11
00
199 5 $self->{'ua'}->{'cookie_jar'}
200 ->set_cookie( undef, $key, $value, $path, $domain, undef,
201 undef, undef, $expire );
202 5 $self->{'ua'}->{'cookie_jar'}->{'COOKIES'}{$domain}{$path}{$key};
203 }
204  
205 sub _save_cookie {
206 5 ▶ my $self = shift;
Subroutine _save_cookie: called CC=3 SCAR=11.0
207  
208 5 $self->_delete_cookie('blogresult');
209 5 my $cookie_file = $self->{'ua'}->{'cookie_jar'}->{'file'};
210 5 ▶ return unless $cookie_file;
Branch: 1 branch
BranchTrueFalseTotal
unless $cookie_file 2 3 5
211 3 my $cookie_path = dirname($cookie_file);
212 3 ▶ if ( !-d $cookie_path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $cookie_path) 1 2 3
213 1 mkpath($cookie_path);
214 }
215 3 $self->{'ua'}->{'cookie_jar'}->save();
216 }
217  
218 sub _delete_cookie {
219 7 ▶ my $self = shift;
Subroutine _delete_cookie: called CC=3 SCAR=11.3
220 7 my ( $key, $domain, $path ) = @_;
221 7 ▶ $domain ||= ".xunlei.com";
Condition50%1/2
$domain ||= ".xunlei.com"
l!l
07
MC/DC0%0/1
$domain ||= ".xunlei.com"
$domain
Truth table50%1/2
A
A= $domain
Aresult
11
00
222 7 ▶ $path ||= "/";
Condition50%1/2
$path ||= "/"
l!l
07
MC/DC0%0/1
$path ||= "/"
$path
Truth table50%1/2
A
A= $path
Aresult
11
00
223 7 $self->{'ua'}->{'cookie_jar'}->clear($domain, $path, $key);
224 }
225  
226 sub _yc_request {
227 3 ▶ my $self = shift;
Subroutine _yc_request: called CC=4 SCAR=14.4
228 3 my ( $action, $parameters, $data ) = @_;
229  
230 3 ▶ my $method = $data ? 'POST' : 'GET';
Branch: 1 branch
BranchTrueFalseTotal
$data ? : 0 3 3
231 3 my $uri = URL_REMOTE . $action;
232 3 $parameters->{'v'} = V;
233 3 $parameters->{'ct'} = CT;
234  
235 3 ▶ $self->_login unless $self->_is_logged_in;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_logged_in 0 3 3
236 3 my $res = $self->_request( $method, $uri, $parameters, $data );
237 3 ▶ if ( $res->{'rtn'} != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{"rtn"} != 0) 0 3 3
238  
239 # Todo: Handling not login failed here.
240 0 die "Request Error: $res->{'rtn'}";
241 }
242  
243 3 return $res;
244 }
245  
246 sub _request {
247 7 ▶ my $self = shift;
Subroutine _request: called CC=8 SCAR=21.4
248 7 my ( $method, $uri, $parameters, $postdata ) = @_;
249 7 my ( $form_string, $payload );
250 7 ▶ if ($parameters) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parameters) 7 0 7
251 7 $form_string = urlencode($parameters);
252 }
253  
254 7 ▶ if ( $method ne 'GET' && !$postdata ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method ne 'GET' and not $postdata) { } 1 6 7
Condition66%2/3
$method ne "GET" and not $postdata
!ll&&!rl&&r
601
MC/DC50%1/2
$method ne "GET" and not $postdata
$method ne "GET" not $postdata
Truth table66%2/3
A and not(B)
A= $method ne "GET"B= not $postdata
ABresult
0X0
100
111
255  
256 # use urlencode parameters as post data when posting login form.
257 1 $payload = $form_string;
258 }
259 else {
260 6 $uri .= '?' . $form_string;
261 6 ▶ if ( ref $postdata ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $postdata) 0 6 6
262 0 $payload = $self->{'json'}->encode($postdata);
263 0 $payload = urlencode( { 'json' => $payload } );
264 }
265 }
266  
267 7 my $request = HTTP::Request->new( $method => $uri, undef, $payload );
268 7 $request->header( 'Content-Type' => 'application/x-www-form-urlencoded' );
269 7 $self->_debug($request);
270 7 my $response = $self->{'ua'}->request($request);
271 7 ▶ die $response->code . ":" . $response->message
Branch: 1 branch
BranchTrueFalseTotal
unless $response->is_success 0 7 7
272 unless $response->is_success;
273 7 my $content = $response->content;
274  
275 7 $self->_debug($content);
276  
277 7 $content =~ s/\s$//g;
278 7 ▶ return "" unless ( length($content) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $content 4 3 7
279  
280 3 ▶ return $self->{'json'}->decode($content) if ( $content =~ /\s*[\[\{\"]/ );
Branch: 1 branch
BranchTrueFalseTotal
if $content =~ /\s*[\[\{\"]/ 3 0 3
281 }
282  
283 sub urlencode {
284 7 ▶ my $data = shift;
Subroutine urlencode: called CC=2 SCAR=6.9
Pod undocumented
285  
286 7 my @parameters;
287 7 for my $key ( keys %$data ) {
288 push @parameters,
289 19 join( '=', map { uri_escape_utf8($_) } $key, $data->{$key} );
290 }
291 7 my $encoded_data = join( '&', @parameters );
292 7 return $encoded_data;
293 }
294  
295 sub md5pass {
296 1 ▶ my $pass = shift;
Subroutine md5pass: called CC=2 SCAR=7.0
Pod undocumented
297 1 ▶ if ( $pass !~ /^[0-9a-f]{32}$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pass =~ /^[0-9a-f]{32}$/i) 1 0 1
298 1 $pass = md5_hex( md5_hex($pass) );
299 }
300 1 return $pass;
301 }
302  
303 sub _debug {
304 15 ▶ my $self = shift;
Subroutine _debug: called CC=3 SCAR=16.7
305 15 my $message = shift;
306 15 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($WWW::Xunlei::DEBUG) 0 15 15
307 0 ▶ if ( ref $message ) { $message = Dumper($message); }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $message) 0 0 0
308 0 my $date = strftime( "%Y-%m-%d %H:%M:%S", localtime );
309  
310 #$date .= sprintf(".%03f", current_timestamp());
311 0 print BLUE "[ $date ] ", GREEN $message, RESET "\n";
312 }
313 }
314  
315 1 1;
316  
317 =pod
318  
319 =encoding UTF-8
320  
321 =head1 NAME
322  
323 WWW::Xunlei - Perl API For Official Xunlei Remote API.
324  
325 =head1 VERSION
326  
327 version 0.05
328  
329 =head1 SYNOPSIS
330  
331 use WWW::Xunlei;
332 my $client = WWW::Xunlei->new("username", "password");
333 # use the first downloader;
334 my $downloader = $client->list_downloaders()->[0];
335 # create a remote task;
336 $downloader->create_task("http://www.cpan.org/src/5.0/perl-5.22.0.tar.gz");
337  
338 =head1 DESCRIPTION
339  
340 C<WWW::Xunlei> is a Perl Wrapper of Xunlei Remote Downloader API.
341 L<Official Site of Xunlei Remote|http://yuancheng.xunlei.com>
342  
343 B<This module is now under deveopment. It's not stable.>
344  
345 =head1 METHODS
346  
347 =head2 new( $username, $password, [cookie_file=>'/path/to/cookie'])
348  
349 create a Xunlei client. Load or save Cookies to a plain text file with
350 C<cookie_file> option.
351  
352 =head2 bind($key, [$name])
353  
354 Bind a new downloader with a activation code. The new downloader's name can
355 be defined with the optional argument C<$name>.
356  
357 =head2 list_downloaders
358  
359 List all the downloaders binding with your account. Return a list of
360 C<WWW::Xunlei::Downloader> object.
361  
362 =head1 AUTHOR
363  
364 Zhu Sheng Li <zshengli@cpan.org>
365  
366 =head1 COPYRIGHT AND LICENSE
367  
368 This software is Copyright (c) 2015 by Zhu Sheng Li.
369  
370 This is free software, licensed under:
371  
372 The MIT (X11) License
373  
374 =cut
375  
376 __END__
« Xunlei/Downloader.pm ↑ Summary