line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
=head1 NAME
|
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
Net::Download::Queue - Download files with one or more workers.
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
# *** In the app which needs to download files
|
8
|
|
|
|
|
|
|
use Net::Download::Queue;
|
9
|
|
|
|
|
|
|
my $oQueue = Net::Download::Queue->new() or die;
|
10
|
|
|
|
|
|
|
my $oDownload = $oQueue->oDownloadAdd(
|
11
|
|
|
|
|
|
|
"http://www.darserman.com/Perl/TexQL/texql.pl",
|
12
|
|
|
|
|
|
|
"./downloads",
|
13
|
|
|
|
|
|
|
"texql.pl.txt",
|
14
|
|
|
|
|
|
|
$urlReferer, #Optional
|
15
|
|
|
|
|
|
|
) or die;
|
16
|
|
|
|
|
|
|
#The url is now queued
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# *** Or using the command line
|
20
|
|
|
|
|
|
|
download_queue --url=http://www.darserman.com/Perl/TexQL/texql.pl \
|
21
|
|
|
|
|
|
|
--dir=./downloads --file=texql.pl.txt --
|
22
|
|
|
|
|
|
|
#The url is now queued
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# *** On another command line (you can have many of these)
|
27
|
|
|
|
|
|
|
download_queue --process
|
28
|
|
|
|
|
|
|
#Urls are downloaded as they appear in the queue
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
Download files asynchronously in a queued fashion.
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
Your application (or a CLI script) can add files to the queue, and
|
36
|
|
|
|
|
|
|
other workes will process the queue and actually download the files.
|
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=cut
|
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
package Net::Download::Queue;
|
45
|
|
|
|
|
|
|
|
46
|
1
|
|
|
1
|
|
15193406
|
use warnings;
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
32
|
|
47
|
1
|
|
|
1
|
|
5
|
use strict;
|
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
29
|
|
48
|
|
|
|
|
|
|
|
49
|
1
|
|
|
1
|
|
6
|
use File::Basename;
|
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
117
|
|
50
|
|
|
|
|
|
|
|
51
|
1
|
|
|
1
|
|
895
|
use LWP::UserAgent;
|
|
1
|
|
|
|
|
14057940
|
|
|
1
|
|
|
|
|
38
|
|
52
|
1
|
|
|
1
|
|
11
|
use HTTP::Response;
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
26
|
|
53
|
1
|
|
|
1
|
|
924
|
use HTTP::Request::Common qw(HEAD);
|
|
1
|
|
|
|
|
2606
|
|
|
1
|
|
|
|
|
80
|
|
54
|
|
|
|
|
|
|
|
55
|
1
|
|
|
1
|
|
624
|
use Net::Download::Queue::DBI;
|
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
18
|
|
56
|
1
|
|
|
1
|
|
690
|
use Net::Download::Queue::Download;
|
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
12
|
|
57
|
1
|
|
|
1
|
|
57
|
use Net::Download::Queue::DownloadStatus;
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
9
|
|
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
our $VERSION = '0.04';
|
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=head1 METHODS
|
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=head2 new()
|
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
Create new queue.
|
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=cut
|
75
|
|
|
|
|
|
|
sub new {
|
76
|
0
|
|
|
0
|
1
|
|
my $self = bless {}, shift;
|
77
|
|
|
|
|
|
|
|
78
|
0
|
|
|
|
|
|
return($self);
|
79
|
|
|
|
|
|
|
}
|
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head2 oDownloadAdd($url, $dirDownload, $file, [$urlReferer = ""])
|
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
Add $url to the queue, to be downloaded in $dirDownload/$file.
|
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
Return the new Download object on success, else die.
|
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=cut
|
92
|
|
|
|
|
|
|
sub oDownloadAdd {
|
93
|
0
|
|
|
0
|
1
|
|
my $self = shift;
|
94
|
0
|
|
|
|
|
|
my ($url, $dirDownload, $file, $urlReferer) = @_;
|
95
|
0
|
|
0
|
|
|
|
$urlReferer ||= "";
|
96
|
|
|
|
|
|
|
|
97
|
0
|
|
0
|
|
|
|
return( Net::Download::Queue::Download->create({
|
98
|
|
|
|
|
|
|
url => $url,
|
99
|
|
|
|
|
|
|
dirDownload => $dirDownload,
|
100
|
|
|
|
|
|
|
fileDownload => $file,
|
101
|
|
|
|
|
|
|
urlReferer => $urlReferer,
|
102
|
|
|
|
|
|
|
}) or die );
|
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
}
|
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head2 oDownloadDequeue()
|
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
If there is any pending download in the queue, return a Download
|
113
|
|
|
|
|
|
|
object, or return undef if there are no pending downloads.
|
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
The Download object is now in "downloading" state.
|
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
Die on errors.
|
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=cut
|
120
|
|
|
|
|
|
|
sub oDownloadDequeue {
|
121
|
0
|
|
|
0
|
1
|
|
my $self = shift;
|
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
#Race condition, ignore that for now.
|
124
|
0
|
0
|
|
|
|
|
my $oDownload = Net::Download::Queue::Download->search_first({
|
125
|
|
|
|
|
|
|
download_status_id => Net::Download::Queue::Download->oDownloadStatus("queued"),
|
126
|
|
|
|
|
|
|
}) or return(undef);
|
127
|
|
|
|
|
|
|
|
128
|
0
|
|
|
|
|
|
$oDownload->setDownloading();
|
129
|
|
|
|
|
|
|
#End race condition
|
130
|
|
|
|
|
|
|
|
131
|
0
|
|
|
|
|
|
return($oDownload);
|
132
|
|
|
|
|
|
|
}
|
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head2 requeueDownloading()
|
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
Set any downloads in status "downloading" back into status "queued".
|
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
Die on errors.
|
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=cut
|
145
|
|
|
|
|
|
|
sub requeueDownloading {
|
146
|
0
|
|
|
0
|
1
|
|
my $self = shift;
|
147
|
|
|
|
|
|
|
|
148
|
0
|
|
|
|
|
|
$_->setQueued() for(Net::Download::Queue::Download->retrieve_downloading);
|
149
|
|
|
|
|
|
|
|
150
|
0
|
|
|
|
|
|
return(1);
|
151
|
|
|
|
|
|
|
}
|
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head2 percentComplete()
|
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
Return the percent 0..100 how complete the download of all current
|
160
|
|
|
|
|
|
|
downloads are.
|
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
If none is current, that's 100% complete.
|
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
Die on errors.
|
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=cut
|
167
|
|
|
|
|
|
|
sub percentComplete {
|
168
|
0
|
|
|
0
|
1
|
|
my $self = shift;
|
169
|
|
|
|
|
|
|
|
170
|
0
|
0
|
|
|
|
|
my $currentCount = Net::Download::Queue::Download->sql_bytesSumCurrent->select_val or return(100);
|
171
|
0
|
|
|
|
|
|
my $downloadingCount = Net::Download::Queue::Download->sql_bytesSumDownloading->select_val;
|
172
|
|
|
|
|
|
|
# my $currentCount = Net::Download::Queue::Download->retrieve_current or return(100);
|
173
|
|
|
|
|
|
|
# my $downloadingCount = Net::Download::Queue::Download->retrieve_downloading;
|
174
|
|
|
|
|
|
|
|
175
|
0
|
|
|
|
|
|
my $percent = $downloadingCount / ($currentCount + $downloadingCount) * 100;
|
176
|
|
|
|
|
|
|
|
177
|
0
|
|
|
|
|
|
return($percent);
|
178
|
|
|
|
|
|
|
}
|
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head1 CLASS METHODS
|
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=head2 rebuildDatabase()
|
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
Empty and rebuild the SQLite database.
|
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
Return 1 on success, else die on errors.
|
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=cut
|
193
|
|
|
|
|
|
|
sub rebuildDatabase {
|
194
|
0
|
|
|
0
|
1
|
|
Net::Download::Queue::DBI->rebuildDatabase();
|
195
|
|
|
|
|
|
|
}
|
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=head2 ensureDatabase()
|
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
Rebuild the SQLite database if it's not present.
|
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
Return 1 on success, else die on errors.
|
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=cut
|
208
|
|
|
|
|
|
|
sub ensureDatabase {
|
209
|
0
|
|
|
0
|
1
|
|
Net::Download::Queue::DBI->ensureDatabase();
|
210
|
|
|
|
|
|
|
}
|
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
1;
|
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
__END__
|