line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WWW::LargeFileFetcher; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
23587
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
26
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
48
|
|
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
|
|
|
|
|
|
|
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 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<< >> |
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
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
11
|
|
|
1
|
|
|
|
|
19
|
|
74
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
31
|
|
75
|
1
|
|
|
1
|
|
2727
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
81620
|
|
|
1
|
|
|
|
|
785
|
|
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# Constructor new |
78
|
|
|
|
|
|
|
sub new { |
79
|
0
|
|
|
0
|
1
|
|
my $proto = shift; |
80
|
0
|
|
0
|
|
|
|
my $class = ref($proto) || $proto; |
81
|
|
|
|
|
|
|
|
82
|
0
|
|
|
|
|
|
my $self = {}; |
83
|
0
|
|
|
|
|
|
bless($self, $class); |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
# Run initialisation code |
86
|
0
|
|
|
|
|
|
return $self->_init(@_); |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub _init{ |
90
|
0
|
|
|
0
|
|
|
my $self = shift; |
91
|
0
|
0
|
|
|
|
|
if (@_ != 0) { # We are expecting our configuration to come as an anonymous hash |
92
|
0
|
0
|
|
|
|
|
if (ref $_[0] eq 'HASH') { |
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
|
0
|
|
|
|
|
my $ua = LWP::UserAgent->new( |
106
|
|
|
|
|
|
|
keep_alive => 1 |
107
|
|
|
|
|
|
|
) or return undef; |
108
|
|
|
|
|
|
|
|
109
|
0
|
0
|
|
|
|
|
if(exists $self->{'timeout'}){ |
110
|
0
|
|
|
|
|
|
$ua->timeout($self->{'timeout'}); |
111
|
|
|
|
|
|
|
} |
112
|
0
|
0
|
|
|
|
|
if(exists $self->{'proxy'}){ |
113
|
0
|
|
|
|
|
|
$ua->proxy('http',$self->{'proxy'}); |
114
|
|
|
|
|
|
|
} |
115
|
0
|
0
|
|
|
|
|
if(exists $self->{'agent'}){ |
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
|
|
|
|
|
|
|
sub err_str{ |
126
|
0
|
|
|
0
|
1
|
|
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
|
|
|
|
|
|
|
sub get{ |
133
|
0
|
|
|
0
|
1
|
|
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
|
|
|
|
|
|
|
sub { |
139
|
0
|
0
|
|
0
|
|
|
unless ($set){ |
140
|
0
|
0
|
|
|
|
|
unless (open(FILE, ">$file") ){ |
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
|
0
|
|
|
|
|
unless( print FILE $_[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
|
0
|
|
|
|
|
if (fileno(FILE)) { |
157
|
0
|
0
|
|
|
|
|
unless( close(FILE) ){ |
158
|
0
|
|
|
|
|
|
$self->{err_str} = "error, Can't write to $file: $!\n"; |
159
|
0
|
|
|
|
|
|
$self->{err_code}=-1; |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
0
|
0
|
|
|
|
|
if($self->{err_code} == -1){ |
164
|
0
|
|
|
|
|
|
unlink($file); |
165
|
0
|
|
|
|
|
|
return -1; |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
0
|
0
|
0
|
|
|
|
if( defined($res) && $res->code == 200 ) { |
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
|
|
|
|
|
|
|
sub test{ |
182
|
0
|
|
|
0
|
1
|
|
my ($self,$url)=@_; |
183
|
0
|
|
|
|
|
|
$self->{err_code}=1; |
184
|
|
|
|
|
|
|
my $res = $self->{'ua'}->get($url, ':content_cb'=> |
185
|
|
|
|
|
|
|
sub { |
186
|
0
|
|
|
0
|
|
|
die; |
187
|
|
|
|
|
|
|
} |
188
|
0
|
|
|
|
|
|
); |
189
|
|
|
|
|
|
|
|
190
|
0
|
0
|
0
|
|
|
|
if( defined($res) && $res->code == 200 ) { |
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
|
|
|
|
|
|
|
1; # End of WWW::LargeFileFetcher |