#!/usr/bin/perl
my
$progname
= $0;
$progname
=~ s,.*/,,;
$progname
=~ s,.*\\,,
if
$^O eq
"MSWin32"
;
$progname
=~ s/\.\w*$//;
my
(
@urls
,
$dest
,
$num_connections
,
@header
);
GetOptions(
'a=s'
, \
@urls
,
'd=s'
, \
$dest
,
'num-connections|n=i'
, \
$num_connections
,
'header|h=s'
, \
@header
,
);
unless
(
@urls
) {
usage();
}
$SIG
{INT} =
sub
{
die
"Interrupted\n"
; };
$| = 1;
my
(
$filename
) =
$urls
[0] =~ /([^\/]+)$/;
my
$start_t
=
time
();
my
%headers
=
map
{
split
'=|:'
,
$_
}
@header
;
my
$cv
= AE::cv;
my
$MultiDown
= AnyEvent::MultiDownload->new(
url
=>
shift
@urls
,
mirror
=> \
@urls
,
headers
=> \
%headers
,
path
=>
$dest
||
$filename
,
max_per_host
=>
$num_connections
|| 20,
on_finish
=>
sub
{
my
$len
=
shift
;
my
$dur
=
time
-
$start_t
|| 1;
my
$speed
= fbytes(
$len
/
$dur
) .
"/sec"
;
print
STDERR
"Finished. "
. fbytes(
$len
) .
" received in $dur seconds ($speed)\n"
;
$cv
->
send
;
},
on_error
=>
sub
{
my
$error
=
shift
;
print
STDERR
"Transfer aborted, $error\n"
;
$cv
->
send
;
}
)->start;
$cv
->
recv
;
sub
usage {
die
"Usage: $progname [-a] <url> [-n] <10>\n"
;
}
sub
fbytes {
my
$n
=
int
(
shift
);
if
(
$n
>= 1024 * 1024) {
return
sprintf
"%.3g MB"
,
$n
/ (1024.0 * 1024);
}
elsif
(
$n
>= 1024) {
return
sprintf
"%.3g KB"
,
$n
/ 1024.0;
}
else
{
return
"$n bytes"
;
}
}