-
Notifications
You must be signed in to change notification settings - Fork 0
/
curl-parallel.pl
executable file
·80 lines (65 loc) · 2.02 KB
/
curl-parallel.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/perl -w
# Given a list of urls in the stdin fetch the urls as quickly as possible
# and save into a directory
#
use LWP::Simple;
use File::Path;
use POSIX;
use File::Basename;
require LWP::Parallel::UserAgent;
my $batchSize = 100;
my @lines = ();
my $dir = "./".strftime("%Y%m%d_%H_%M", localtime())."/";
while (<>)
{
chomp();
push(@lines, $_);
if (length(@lines) == $batchSize)
{
parallel_download(\@lines);
@lines = ();
}
}
parallel_download(\@lines);
sub parallel_download
{
my ($AR_urls) = @_;
if (!ref($AR_urls))
{
die "parallel_download expect argument to be ref to an array\n";
}
mkpath($dir)
unless(-d $dir);
my $ua = LWP::Parallel::UserAgent->new();
$ua->redirect (0); # prevents automatic following of redirects
$ua->max_hosts(20); # sets maximum number of locations accessed in parallel
$ua->max_req (40); # sets maximum number of parallel requests per host
my @A_requests = ();
my %H_downloads = ();
foreach my $S_url (@$AR_urls)
{
my $request = new HTTP::Request('GET', $S_url);
my $basename = basename($S_url);
$basename =~ s/\W/_/g;
my $download_file = $dir.$basename; # _downloadPath has '/' at the end
$ua->register($request, $download_file);
$H_downloads{$S_url} = $dir.basename($S_url);
}
my $S_starttime = time();
print "start ".scalar(localtime())." downloading urls in parallel\n";
my $entries = $ua->wait();
print "[DURATION ",(time() - $S_starttime),"] ";
print "done ".scalar(localtime())."downloading urls in parallel\n";
foreach (keys %$entries) {
my $response = $entries->{$_}->response;
my $uri = $response->request->url;
if ($response->is_success())
{
print "\n\nSUCCESS: Request to ",$uri, "returned code ", $response->code,
": ", $response->message, "\n";
} else {
print "\n\nERROR! Request to [$uri], returned code ", $response->code,
": ", $response->message, "\n";
}
}
}