blib/lib/WWW/LargeFileFetcher.pm

filter: Statementstmt 21.2% 17 / 80 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 41.6% 5 / 12 Podpod 100% 4 / 4 totaltotal 18.7% 26 / 139 CC 20 SCAR 36.6
CC
20
Cov
15%
_init
90.0
get
42.0
__ANON__
20.0
CRAP 38.7
↑ Summary
1 package WWW::LargeFileFetcher;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 WWW::LargeFileFetcher - a module used to fetch large files from internet.
9  
10 =head1 VERSION
11  
12 Version 0.02
13  
14 =cut
15  
16 2 our $VERSION = '0.02';
17  
18 =head1 SYNOPSIS
19  
20 use WWW::LargeFileFetcher;
21  
22 my $fetcher = WWW::LargeFileFetcher->new();
23 $fetcher->get('http://www.example.com/video.rm', 'video.rm');
24  
25 =head1 DESCRIPTION
26  
27 C<WWW::LargeFileFetcher> is used to fetch large files (like
28 videos, audios) from internet.
29  
30 =head1 METHODS
31  
32 =over
33  
34 =item $fetcher = WWW::LargeFileFetcher->new(%opinions);
35  
36 The opinions hash can be:
37 agent=>'the agent string',
38 timeout=>time in seconds,
39 proxy=>'http proxy to be used'
40  
41 =item $fetcher->get($url,$filename);
42  
43 The return value can be:
44 1 : success
45 -1 : IO error
46 -2 : internet access error
47  
48 The detailed error string can be accessed from $fetcher->err_str();
49  
50 =item $fetcher->test($url);
51  
52 This method is used to test whether the $url is downloadable.
53  
54 =item $fetcher->err_str();
55  
56 Return the detail description of the error occured.
57  
58 =back
59  
60 =head1 AUTHOR
61  
62 Zhang Jun, C<< <jzhang533 at gmail.com> >>
63  
64 =head1 COPYRIGHT & LICENSE
65  
66 Copyright 2007 Zhang Jun, all rights reserved.
67  
68 This program is free software; you can redistribute it and/or modify it
69 under the same terms as Perl itself.
70  
71 =cut
72  
73 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
76  
77 # Constructor new
78 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
79 0 my $proto = shift;
80 0 ▶ my $class = ref($proto) || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
81  
82 0 my $self = {};
83 0 bless($self, $class);
84  
85 # Run initialisation code
86 0 return $self->_init(@_);
87 }
88  
89 0 ▶sub _init{
Subroutine _init: not called CC=9 SCAR=45.0
90 0 my $self = shift;
91 0 ▶ if (@_ != 0) { # We are expecting our configuration to come as an anonymous hash
Branch: 1 branch
BranchTrueFalseTotal
if (@_ != 0) 0 0 0
92 0 ▶ if (ref $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 0 0 0
93 0 my $hash=$_[0];
94 0 foreach my $key (keys %$hash) {
95 0 $self->{lc($key)}=$hash->{$key};
96 }
97 }else { # Using a more conventional named args
98 0 my %args = @_;
99 0 foreach my $key (keys %args) {
100 0 $self->{lc($key)}=$args{$key};
101 }
102 }
103 }
104  
105 0 ▶ my $ua = LWP::UserAgent->new(
Branch: 1 branch
BranchTrueFalseTotal
my $ua = "LWP::UserAgent"->new("keep_alive", 1) or return undef 0 0 0
106 keep_alive => 1
107 ) or return undef;
108  
109 0 ▶ if(exists $self->{'timeout'}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"timeout"}) 0 0 0
110 0 $ua->timeout($self->{'timeout'});
111 }
112 0 ▶ if(exists $self->{'proxy'}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"proxy"}) 0 0 0
113 0 $ua->proxy('http',$self->{'proxy'});
114 }
115 0 ▶ if(exists $self->{'agent'}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"agent"}) 0 0 0
116 0 $ua->agent($self->{'agent'});
117 }
118  
119 0 $self->{'ua'}=$ua;
120 0 $self->{err_str}='';
121 0 $self->{err_code}=1;
122 0 return $self;
123 }
124  
125 0 ▶sub err_str{
Subroutine err_str: not called CC=1 SCAR=6.9
Pod documented
126 0 return $_[0]->{err_str};
127 }
128  
129 # input: url, file
130 # return: 1 for success, -1 for IO error, -2 for internet access error
131 # url is stored into file
132 0 ▶sub get{
Subroutine get: not called CC=6 SCAR=37.4
Pod documented
133 0 my ($self,$url,$file)=@_;
134 0 $self->{err_code}=1;
135 0 my $set=0;
136 0 $|++;
137 my $res = $self->{'ua'}->get($url, ':content_cb'=>
138 0 ▶ sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
139 0 ▶ unless ($set){
Branch: 1 branch
BranchTrueFalseTotal
unless ($set) 0 0 0
140 0 ▶ unless (open(FILE, ">$file") ){
Branch: 1 branch
BranchTrueFalseTotal
unless (open FILE, ">$file") 0 0 0
141 0 $self->{err_str} = "error, Can't open $file: $!\n";
142 0 $self->{err_code}=-1;
143 0 die;
144 }
145 0 binmode FILE;
146 0 $set = 1;
147 }
148 0 ▶ unless( print FILE $_[0] ){
Branch: 1 branch
BranchTrueFalseTotal
unless (print FILE $_[0]) 0 0 0
149 0 $self->{err_str} = "error, Can't write to $file: $!\n";
150 0 $self->{err_code}=-1;
151 0 die;
152 }
153 }
154 0 );
155
156 0 ▶ if (fileno(FILE)) {
Branch: 1 branch
BranchTrueFalseTotal
if (fileno FILE) 0 0 0
157 0 ▶ unless( close(FILE) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (close FILE) 0 0 0
158 0 $self->{err_str} = "error, Can't write to $file: $!\n";
159 0 $self->{err_code}=-1;
160 }
161 }
162  
163 0 ▶ if($self->{err_code} == -1){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"err_code"} == -1) 0 0 0
164 0 unlink($file);
165 0 return -1;
166 }
167  
168 0 ▶ if( defined($res) && $res->code == 200 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $res and $res->code == 200) { } 0 0 0
Condition0%0/3
defined $res and $res->code == 200
!ll&&!rl&&r
000
MC/DC0%0/2
defined $res and $res->code == 200
defined $res $res->code == 200
Truth table0%0/3
A and B
A= defined $resB= $res->code == 200
ABresult
0X0
100
111
169 0 return 1;
170 }else{
171 0 $self->{err_str} = "error, ". $res->status_line()."\n";
172 0 $self->{err_code}=-2;
173 0 return -2;
174 }
175 }
176  
177  
178 # test if the url can be downloaded, not really download it
179 # input: url
180 # return: 1 for success, -1 for IO error, -2 for internet access error
181 0 ▶sub test{
Subroutine test: not called CC=3 SCAR=24.8
Pod documented
182 0 my ($self,$url)=@_;
183 0 $self->{err_code}=1;
184 my $res = $self->{'ua'}->get($url, ':content_cb'=>
185 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
186 0 die;
187 }
188 0 );
189
190 0 ▶ if( defined($res) && $res->code == 200 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $res and $res->code == 200) { } 0 0 0
Condition0%0/3
defined $res and $res->code == 200
!ll&&!rl&&r
000
MC/DC0%0/2
defined $res and $res->code == 200
defined $res $res->code == 200
Truth table0%0/3
A and B
A= defined $resB= $res->code == 200
ABresult
0X0
100
111
191 0 return 1;
192 }else{
193 0 $self->{err_str} = "error, ". $res->status_line()."\n";
194 0 $self->{err_code}=-2;
195 0 return -2;
196 }
197 }
198  
199 2 1; # End of WWW::LargeFileFetcher
↑ Summary