10 $PROGRAM_DIR =~ s/[^\/\\]+$//;
11 $PROGRAM_DIR = "./" unless($PROGRAM_DIR);
13 map "$PROGRAM_DIR$_",qw{modules lib
../modules
..lib
};
18 help
|h
|? version
|ver edit
-me manual
|man
19 autoname
|a cookie
|b
=s directory
|d ext
|e
=s
20 fullname
|f logger
|L
=s maxtime
|M
=i maxtask
|m
=i
21 taskname
|n
=s referer
|r
=s workdir
|w
=s urlhist
|U
22 no-clobber
|nc
|c numlen
|i
=i
28 require MyPlace
::Usage
;
29 Getopt
::Long
::Configure
('no_ignore_case');
30 Getopt
::Long
::GetOptions
(\
%OPTS,@OPTIONS);
31 MyPlace
::Usage
::Process
(\
%OPTS,$VERSION);
34 use MyPlace
::ParallelRun
;
36 use MyPlace
::Script
::Message
;
40 my $createdir = $OPTS{"directory"} ?
$OPTS{"directory"} : 0;
41 my $maxtime = $OPTS{"maxtime"} ?
$OPTS{"maxtime"} : undef;
42 my $muldown = $OPTS{"maxtask"} ?
$OPTS{"maxtask"} : $def_mul;
43 my $taskname = $OPTS{"taskname"} ?
$OPTS{"taskname"} : "";
44 my $autoname = $OPTS{"autoname"} ?
$OPTS{"autoname"} : 0;
45 my $extname = $OPTS{"ext"} ?
$OPTS{"ext"} : "";
46 my $workdir = $OPTS{"workdir"} ?
$OPTS{"workdir"} : "";
47 my $refer = $OPTS{"referer"} ?
$OPTS{"referer"} : "";
48 my $logger = $OPTS{"logging"} ?
$OPTS{"logging"} : "";
49 my $cookie = $OPTS{"cookie"} ?
$OPTS{"cookie"} : "";
50 my $number = $OPTS{"numlen"} ?
$OPTS{"numlen"} : "";
51 my $fullname = $OPTS{"fullname"} ?
1 : 0;
52 my $urlhist = $OPTS{'urlhist'} ?
1 : 0;
53 my $URL_DATABASE_FILE = 'URLS.txt';
56 $autoname="true" if($number);
58 $taskname = shift(@ARGV) if(@ARGV);
60 $taskname = "" unless($taskname);
61 $muldown = 1 if( $muldown<1);
64 # mkdir $workdir unless(-d $workdir);
65 # $workdir .= "/" unless($workdir =~ /\/$/);
72 open FI
,"<",$URL_DATABASE_FILE or return;
81 if($URL_DATABASE{$url}) {
83 # print STDERR Data::Dumper->Dump([\%URL_DATABASE],['*URL_DATABASE']),"\n";
88 $URL_DATABASE{$url}=1;
93 open FO
,">",$URL_DATABASE_FILE or return;
94 foreach (keys %URL_DATABASE) {
101 my $max = 10000000000;
104 my $n1 = int (rand($max));
105 my $n2 = log($max / $n1)/log(10);
106 $result=$n1 . "0"x
$n2 . $ext;
107 } until (! -f
$result);
110 sub GetFilename_Fullname
{
112 $result =~ s/^.*:\/\///;
113 $result =~ s/[\/\?\:\\\*\&]/_
/g
;
118 sub GetFilename_Auto
{
123 #($ext=$URL) =~ s/^.*\.([^\.]*)$/.$1/ unless($ext);
124 #$result=$num ? "$num$ext" : Uniqname($ext);
126 $result =~ s/^.*:\/\///;
127 $result =~ s/[\/\?\:\\\*\&]/_
/g
;
129 if(length($result)>=128) {
130 $result = substr($result,0,127);
132 $result = "$num.$result" if(defined $num);
135 $dirname =~ s/^.*:\/*[^\/]*\
///;
136 $dirname =~ s/\/[^\/]*//;
137 $dirname .= "/" if($dirname);
138 $result = $dirname . $result;
142 sub GetFilename_NoAuto
{
145 $result =~ s/^.*:\/*[^\/]*\
///;
148 $result =~ s/^.*\///;
153 sub pushArgPair
(\@
$$) {
159 push @
{$DARG},$value;
165 my ($URL,$index,$count,$prefix)=@_;
166 my $curname= "[$prefix$index/$count]";
167 my $stridx = "0" x
(length($count)-length($index)+1) . $index if($number);
169 app_warning
($curname . "Duplicated, $URL [Ignored]\n");
177 GetFilename_Fullname
($URL)
178 #GetFilename_Fullname(uri_unescape($URL))
180 GetFilename_Auto
($URL,$stridx)
181 #GetFilename_Auto(uri_unescape($URL),$stridx)
182 : GetFilename_NoAuto
($URL);
184 if($OPTS{"no-clobber"}) {
185 if(-f
$workdir . $filename) {
186 app_warning
($curname . "$URL\t[Ignored, TARGET EXISTS]\n");
191 my $thisrefer= $refer ?
$refer : $URL;
192 if($logger) {system($logger,$filename,$URL);}
194 pushArgPair
(@DARG,"-u",$URL);
195 pushArgPair
(@DARG,"-s",$filename);
196 #pushArgPair(@DARG,"-s",$workdir . $filename);
197 pushArgPair
(@DARG,"-n",$curname);
198 pushArgPair
(@DARG,"-r",$thisrefer);
199 pushArgPair
(@DARG,"-b",$cookie);
200 pushArgPair
(@DARG,"-m",$maxtime);
202 para_queue
"download",@DARG;
207 my $prefix = $taskname ?
$taskname . " " : "";
215 return undef unless($w);
217 system("mkdir","-p","--",$w) and die("$!\n");
219 chdir $w or die("$!\n");
226 set_workdir
($workdir);
231 system("mkcookie '$cookie' >download.cookie");
232 $cookie="download.cookie";
235 load_database
() if($urlhist);
245 elsif($_ =~ m/^#BATCHGET:/) {
251 elsif($_ =~ /([^\t]+)\t+([^\t]+)/) {
254 $Local_Map{$_} =~ s/^(http|ftp|https):?\/*//gi
;
255 $Local_Map{$_} =~ s/[\/:,\?\*\\]/_
/g
;
258 if(check_database
($_)) {
259 app_warning
("[Ignored, In DATABASE]$_\n");
265 #my $is_old = check_database($_);
266 #if($urlhist and $is_old) {
267 # app_warning("$_\t[Ingored, IN DATABASE]\n");
270 #check_database($_));
273 #if(0 and ¶_isfree()) { # disable pre processing! (08/03/2010 xiaoranzzz@gmail.com)
274 # my $URL = shift @URLS;
276 # &inqueue($URL,$index,$count,$prefix);
278 # print STDERR ("\r$count URLS enqueued... ");
280 $count = $count - $no_urls_count;
282 if(m/^#BATCHGET:chdir:(.+)$/) {
286 app_message
('Change working directory to ' . "$w\n");
287 chdir $PWD or die("$!\n");
293 if(inqueue
($_,$index,$count,$prefix)) {
301 save_database
() if($urlhist);
303 #print STDERR ("\n");
304 #exit 0 unless($count);
314 batchget - A batch mode downloader
318 batchget [options] ...
320 cat url.lst | batchget
322 cat url.lst | batchget -a -d
328 =item B<-a,--autoname>
330 Use indexing of URLs as output filename
336 =item B<-c,--nc,--no-clobber>
338 No clobber when target exists.
340 =item B<-d,--directory>
346 Extension name for autonaming
348 =item B<-f,--fullname>
350 Use URL as output filename
354 Number length for index filename
356 =item B<-M,--maxtime>
358 Max time for a single download process
360 =item B<-m,--maxtask>
362 Max number of simulatanous downloading task
364 =item B<-n,--taskname>
368 =item B<-r,--referer>
372 =item B<-w,--workdir>
374 Global working directory
376 =item B<-U,--urlhist>
378 Use URL downloading history databasa
382 Print version infomation.
384 =item B<-h>,B<--help>
386 Print a brief help message and exits.
388 =item B<--manual>,B<--man>
390 View application manual
394 Invoke 'editor' against the source
400 A downloader which can download multiple urls at the same time and/or in queue.
404 2007-10-28 xiaoranzzz <xiaoranzzz@myplace.hell>
406 * file created, version 0.1
408 2010-08-03 xiaoranzzz <xiaoranzzz@myplace.hell>
410 * update to version 0.2
414 xiaoranzzz <xiaoranzzz@myplace.hell>