lib/Net/Download/Queue/Download.pm

filter: Statementstmt 86.2% 88 / 102 Branchbran 36.3% 8 / 22 Conditioncond 29.4% 5 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 80.0% 16 / 20 Podpod 100% 7 / 7 totaltotal 69.6% 124 / 178 CC 19 SCAR 26.4
CC
19
Cov
72%
__ANON__
42.0
download
26.2
bytesContentFromUrl
5.1
CRAP 14.1
↑ Summary lib/Net/Download/Queue.pm »
1 =head1 NAME
2
3 Net::Download::Queue::Download
4
5 =head1 SYNOPSIS
6
7
8 =cut
9
10
11
12
13
14 package Net::Download::Queue::Download;
15 7 ▶use base 'Net::Download::Queue::DBI';
Subroutine BEGIN: called CC=1 SCAR=0.0
16
17
18
19 7 our $VERSION = Net::Download::Queue::DBI::VERSION;
20
21
22
23 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 7 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
25
26 7 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 7 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 7 ▶use HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 7 ▶use HTTP::Request::Common qw(GET POST);
Subroutine BEGIN: called CC=1 SCAR=0.0
30 7 ▶use File::Slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
31
32
33
34
35
36 =head1 CLASS DBI STUFF
37
38 =cut
39
40 7 __PACKAGE__->set_up_table('download');
41 7 __PACKAGE__->has_a( download_status_id => 'Net::Download::Queue::DownloadStatus' );
42
43
44
45
46
47 =head2 trigger: before_create
48
49 Set domain and bytesContent.
50
51 =cut
52 __PACKAGE__->add_trigger(
53 6 ▶ before_create => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
54 6 my $self = shift;
55 6 $self->domain($self->domainFromUrl($self->url));
56 6 $self->_attribute_set("bytes_content", $self->bytesContentFromUrl($self->_attrs("url"), $self->_attrs("urlReferer")));
57 }
58 7 );
59
60
61
62
63
64 =head2 retrieve_current
65
66 Return array with all current downloads.
67
68 =cut
69 7 __PACKAGE__->add_constructor(
70 retrieve_current => q{
71 download_status_id in (
72 select status_id from download_status where is_current = 1
73 )
74 });
75
76
77
78
79
80 =head2 retrieve_downloading
81
82 Return array with all downloading downloads.
83
84 =cut
85 7 __PACKAGE__->add_constructor(
86 retrieve_downloading => q{
87 download_status_id in (
88 select status_id from download_status where name = 'downloading'
89 )
90 });
91
92
93
94
95
96 =head2 sql_bytesSumCurrent->select_val
97
98 Return total size of current downloads.
99
100 =cut
101 7 __PACKAGE__->set_sql(bytesSumCurrent => q{
102 select sum(bytes_content) from __TABLE__ where download_status_id in (
103 select status_id from download_status where is_current = 1
104 )
105 });
106
107
108
109
110
111 =head2 sql_bytesSumDownloading->select_val
112
113 Return total size of downloading downloads.
114
115 =cut
116 7 __PACKAGE__->set_sql(bytesSumDownloading => q{
117 select sum(bytes_content) from __TABLE__ where download_status_id in (
118 select status_id from download_status where name = 'downloading'
119 )
120 });
121
122
123
124
125
126 =head1 METHODS
127
128
129 =head2 setDone()
130
131 Set the download status to download done ok. Set the bytesDownloaded
132 to bytesContent.
133
134 Return 1, die on errors.
135
136 =cut
137 3 ▶sub setDone {
Subroutine setDone: called CC=1 SCAR=0.0
Pod documented
138 3 my $self = shift;
139
140 3 $self->downloadStatusId($self->oDownloadStatus("downloaded: ok"));
141 3 $self->bytesDownloaded( $self->bytesContent );
142 3 $self->update();
143
144 3 return(1);
145 }
146
147
148
149
150
151 =head2 setQueued()
152
153 Set the download status to queued.
154
155 Return 1, die on errors.
156
157 =cut
158 2 ▶sub setQueued {
Subroutine setQueued: called CC=1 SCAR=0.0
Pod documented
159 2 my $self = shift;
160
161 2 $self->downloadStatusId($self->oDownloadStatus("queued"));
162 2 $self->update();
163
164 2 return(1);
165 }
166
167
168
169
170
171 =head2 setDownloading()
172
173 Set the download status to downloading.
174
175 Return 1, die on errors.
176
177 =cut
178 2 ▶sub setDownloading {
Subroutine setDownloading: called CC=1 SCAR=0.0
Pod documented
179 2 my $self = shift;
180
181 2 $self->downloadStatusId($self->oDownloadStatus("downloading"));
182 2 $self->bytesDownloaded(0);
183 2 $self->update();
184
185 2 return(1);
186 }
187
188
189
190
191
192 =head2 setBytesDownloaded($bytesTotal)
193
194 Set the total number of bytes downloaded in this download.
195
196 Return 1, die on errors.
197
198 =cut
199 1 ▶sub setBytesDownloaded {
Subroutine setBytesDownloaded: called CC=1 SCAR=0.0
Pod documented
200 1 my $self = shift;
201 1 my ($bytesDownloaded) = @_;
202
203 1 $self->bytesDownloaded($bytesDownloaded);
204 1 $self->update();
205
206 1 return(1);
207 }
208
209
210
211
212
213 =head2 download([$rsStart], [$rsReceived], [$rsCheckCancel])
214
215 Attempt to perform download and set the status accordingly.
216
217 Perform the download regardless of the current status.
218
219 $rsStart, $rsReceived, $rsCheckCancel are sub refs which are called
220 during the download.
221
222 $rsStart->($contentLength)
223 Called once.
224
225 $rsReceived->($bytesReceived)
226 Called for each chunk downloaded.
227
228 $rsCheckCancel->()
229 Called for each chunk. Should return true if the download should be
230 cancelled, else false.
231
232 Return 1, die on errors.
233
234 =cut
235 1 ▶sub download {
Subroutine download: called CC=9 SCAR=32.7
Pod documented
236 1 my $self = shift;
237 1 my ($rsStart, $rsReceived, $rsCheckCancel) = @_;
238 1 ▶ $rsStart ||= sub {};
Subroutine __ANON__: not called CC=1 SCAR=1.2
Condition33%1/3
$rsStart ||= sub { }
l!l&&r!l&&!r
010
MC/DC0%0/2
$rsStart ||= sub { }
$rsStart sub { }
Truth table33%1/3
A ||= B
A= $rsStartB= sub { }
ABresult
1X1
011
000
239 1 ▶ $rsReceived ||= sub {};
Subroutine __ANON__: not called CC=1 SCAR=1.2
Condition33%1/3
$rsReceived ||= sub { }
l!l&&r!l&&!r
010
MC/DC0%0/2
$rsReceived ||= sub { }
$rsReceived sub { }
Truth table33%1/3
A ||= B
A= $rsReceivedB= sub { }
ABresult
1X1
011
000
240 1 ▶ $rsCheckCancel ||= sub {};
Subroutine __ANON__: not called CC=1 SCAR=1.2
Condition33%1/3
$rsCheckCancel ||= sub { }
l!l&&r!l&&!r
010
MC/DC0%0/2
$rsCheckCancel ||= sub { }
$rsCheckCancel sub { }
Truth table33%1/3
A ||= B
A= $rsCheckCancelB= sub { }
ABresult
1X1
011
000
241 1 my $updateEvery = 1000;
242
243 1 eval {
244 1 my $url = $self->url;
245
246 1 my $oBrowser = LWP::UserAgent->new(
247 env_proxy => 0,
248 timeout => 50,
249 keep_alive => 1,
250 agent => "Internet Explorer 5.5 on Windows 2000: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)",
251 );
252 1 my $oRequest = HTTP::Request->new(GET => $url);
253 1 ▶ $self->urlReferer and $oRequest->referer($self->urlReferer);
Branch: 1 branch
BranchTrueFalseTotal
$self->urlReferer and $oRequest->referer($self->urlReferer) 0 1 1
254
255 1 my $content = "";
256 1 my $once = 0;
257 1 my $bytesReceived = 0;
258 my $oResponse = $oBrowser->request(
259 $oRequest,
260 0 ▶ sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
261 0 my ($chunk, $oResponse) = @_;
262
263 0 ▶ $once++ or $rsStart->($oResponse->content_length || 1);
Branch: 1 branch
BranchTrueFalseTotal
$once++ or &$rsStart($oResponse->content_length || 1) 0 0 0
Condition0%0/2
$oResponse->content_length || 1
l!l
00
MC/DC0%0/1
$oResponse->content_length || 1
$oResponse->content_length
Truth table0%0/2
A
A= $oResponse->content_length
Aresult
11
00
264
265 0 $bytesReceived += length($chunk);
266 0 ▶ $rsReceived->($bytesReceived || 1);
Condition0%0/2
$bytesReceived || 1
l!l
00
MC/DC0%0/1
$bytesReceived || 1
$bytesReceived
Truth table0%0/2
A
A= $bytesReceived
Aresult
11
00
267
268 0 $content .= $chunk;
269
270 0 ▶ $rsCheckCancel->() and die("Cancelled\n");
Branch: 1 branch
BranchTrueFalseTotal
&$rsCheckCancel() and die "Cancelled\n" 0 0 0
271
272 0 ▶ $once % $updateEvery or $self->setBytesDownloaded($bytesReceived);
Branch: 1 branch
BranchTrueFalseTotal
$once % $updateEvery or $self->setBytesDownloaded($bytesReceived) 0 0 0
273 }
274 1 );
275
276 1 ▶ $oResponse->is_success or die("Could not get url ($url) (" . $oResponse->status_line . ")\n");
Branch: 1 branch
BranchTrueFalseTotal
$oResponse->is_success or die "Could not get url ($url) (" . $oResponse->status_line . ")\n" 0 1 1
277 0 ▶ defined($content) or warn("Could not get URL ($url) (no content)\n"), return(0);
Branch: 1 branch
BranchTrueFalseTotal
defined $content or warn("Could not get URL ($url) (no content)\n"), (return 0) 0 0 0
278
279
280 0 my $nameDir = $self->dirDownload();
281 0 my $nameFile = "$nameDir/" . $self->fileDownload;
282
283 0 mkpath($nameDir);
284 0 ▶ -d $nameDir or warn("No dir ($nameDir)\n"), return(0);
Branch: 1 branch
BranchTrueFalseTotal
-d $nameDir or warn("No dir ($nameDir)\n"), (return 0) 0 0 0
285
286 0 write_file($nameFile, { binmode => ":raw", }, $content);
287
288 0 undef $content;
289
290 };
291
292 1 $self->setDone;
293
294 1 ▶ $@ and die;
Branch: 1 branch
BranchTrueFalseTotal
$@ and die 0 1 1
295
296 1 return(1);
297 }
298
299
300
301
302
303 =head1 CLASS METHODS
304
305 =head2 domainFromUrl($url)
306
307 Return the domain part of $url, or "" if none was found.
308
309 Die on errors.
310
311 =cut
312 9 ▶sub domainFromUrl {
Subroutine domainFromUrl: called CC=2 SCAR=6.9
Pod documented
313 9 my $pkg = shift;
314 9 my ($url) = @_;
315
316 9 ▶ $url =~ m|^\w+ : // (?: [^\@]+ \@ )? ([\w\.]+) |msx or return("");
Branch: 1 branch
BranchTrueFalseTotal
$url =~ m[^\w+ : // (?: [^\@]+ \@ )? ([\w\.]+) ]msx or return "" 8 1 9
317 8 my $domain = $1;
318
319 8 return($domain);
320 }
321
322
323
324
325
326 =head2 bytesContentFromUrl($url, [$urlReferer = ""])
327
328 Return the Content-Length of HEAD $url, or 0 if none was found.
329
330 Die on errors.
331
332 =cut
333 6 ▶sub bytesContentFromUrl {
Subroutine bytesContentFromUrl: called CC=5 SCAR=16.4
Pod documented
334 6 my $pkg = shift;
335 6 my ($url, $urlReferer) = @_;
336 6 ▶ $urlReferer ||= "";
Condition50%1/2
$urlReferer ||= ""
l!l
06
MC/DC0%0/1
$urlReferer ||= ""
$urlReferer
Truth table50%1/2
A
A= $urlReferer
Aresult
11
00
337
338 6 my $oBrowser = LWP::UserAgent->new(
339 env_proxy => 0,
340 timeout => 50,
341 keep_alive => 1,
342 agent => "Internet Explorer 5.5 on Windows 2000: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)",
343 );
344 6 my $oRequest = HTTP::Request->new(HEAD => $url);
345 6 ▶ $urlReferer and $oRequest->referer($urlReferer);
Branch: 1 branch
BranchTrueFalseTotal
$urlReferer and $oRequest->referer($urlReferer) 0 6 6
346 6 my $oResponse = $oBrowser->request($oRequest);
347 6 ▶ $oResponse->is_success or return(0);
Branch: 1 branch
BranchTrueFalseTotal
$oResponse->is_success or return 0 1 5 6
348 1 ▶ return( $oResponse->header("Content-Length") || 0 );
Condition50%1/2
$oResponse->header('Content-Length') || 0
l!l
10
MC/DC0%0/1
$oResponse->header('Content-Length') || 0
$oResponse->header('Content-Length')
Truth table50%1/2
A
A= $oResponse->header('Content-Length')
Aresult
11
00
349 }
350
351
352
353
354
355 7 1;
356
357
358
359
360
361 __END__
↑ Summary lib/Net/Download/Queue.pm »