5 ###APPDATE: Fri Sep 28 08:31:24 2007
7 ###APPDESC: A downloader,nothing else.
8 ###APPUSAGE: [options] URL
9 ###APPEXAMPLE: download www.google.com/index.html
10 ###APPOPTION: -v:Verbose Output|-u:URL to download|-s:Filename for output|-d:Create directories|-n:Task name|-r:Referer URL|-b:cookie file|-l:Enable Logging|-a:Auto rename if file exists|-p:Downloader - wget or curl-m:Max time for downloading|-f:Force over writting
12 use lib
$ENV{XR_PERL_MODULE_DIR
};
14 use MyPlace
::Script
::Usage qw
/help_required help_even_empty/;
17 use MyPlace
::Script
::Message
;
19 exit 0 if(help_even_empty
($0,@ARGV));
21 my $proxy = '127.0.0.1:9050';
22 my $blocked_host = 'wretch\.cc|facebook\.com|fbcdn\.net';
23 my $BLOCKED_EXP = qr/^[^\/]+:\
/\/[^\
/]*(?:$blocked_host)(?:\/?
|\
/.*)$/;
25 wget
--user
-agent Mozilla
/5.0 --connect-timeout
15 -q
--progress bar
28 curl
--fail
--globoff
--location
--user
-agent Mozilla
/5.0 --progress
-bar
--create
-dirs
33 open FO
,">>",$fn or return;
39 my($name,$url,$saveas,$refer,$cookie,$verbose,$maxtime) = @_;
40 return undef unless($url);
42 if($name =~ /^wget$/i) {
44 push @result,"--referer",$refer ?
$refer : $url;
45 push @result,"--output-document",$saveas if($saveas);
46 push @result,"--load-cookie",$cookie if(-f
$cookie);
47 push @result,"--save-cookie",$cookie if($cookie);
48 push @result,'--read-timeout',$maxtime if($maxtime);
53 push @result,"--url",$url;
54 push @result,"--referer",$refer ?
$refer : $url;
55 push @result,"--output",$saveas if($saveas);
56 push @result,"--cookie",$cookie if(-f
$cookie);
57 push @result,"--cookie-jar",$cookie if($cookie);
58 push @result,"--max-time",$maxtime if($maxtime);
59 if($url =~ $BLOCKED_EXP) {
60 app_message
"USE PROXY $proxy\n";
61 push @result,"--socks5-hostname",$proxy;
70 my $retry = shift || 2;
74 $r=system(@
{$cmdline});
76 return 2 if($r==2); #/KILL,TERM,USERINT;
79 #22 => Request Error 404,403
80 #56 => Recv failure: Connection reset by peer
81 return $r if($r == 2 or $r == 22 or $r == 56 or $r == 6);
82 app_warning
"\rdownload:error($r), wait 1 second,retry $taskname\n";
89 my $OptFlag='m:lvu:s:dn:r:b:ap:f';
91 getopts
($OptFlag,\
%OPT);
93 my $verbose= $OPT{"v"} ?
$OPT{"v"} : 0;
94 my $url= $OPT{"u"} ?
$OPT{"u"} : $ARGV[@ARGV-1];
95 my $saveas= $OPT{"s"} ?
$OPT{"s"} : "";
96 my $createdir= $OPT{"d"} ?
$OPT{"d"} : 0;
97 my $name= $OPT{"n"} ?
$OPT{"n"} : "";
98 my $refer= $OPT{"r"} ?
$OPT{"r"} : "";
99 my $cookie= $OPT{b
} ?
$OPT{b
} : "";
100 my $logging= $OPT{l
} ?
$OPT{l
} : "";
101 my $autorename = $OPT{a
} ?
$OPT{a
} : "";
102 my $maxtime = $OPT{m
} ?
$OPT{m
} : undef;
103 my $force = $OPT{f
} ?
$OPT{f
} : undef;
104 #my $downloader = $OPT{p} ? $OPT{p} : "wget";
105 my $downloader = $OPT{p
} ?
$OPT{p
} : "curl";
106 my $FAILLOG="download.failed";
107 my $DOWNLOADLOG="download.log";
109 if ($url !~ m/^\w+:\/\
// ) {
110 app_message
("invaild URL:\"",$url,"\"\n");
115 $refer=$url unless($refer);
116 if($createdir && !$saveas) {
118 $filename =~ s/^\w+:\/+[^\/]*\
/+//;
119 $filename =~ s/^[^\?]*\?[^\/]*\/+//g;
124 $basename =~ s/^.*\///g
;
125 $basename = "index.htm" unless($basename);
128 if($saveas =~ m/\/$/) {
129 $saveas .= "index.htm";
131 if($saveas and $autorename and -f
$saveas) {
134 if($saveas =~ m/^(.*)(\.[^\.]*)$/) {
138 use MyPlace
::Filename qw
/get_uniqname/;
139 $saveas = get_uniqname
($base,$ext);
143 app_message
(sprintf("%s\n%-8s: %s\n%-8s: %s\n%-8s: %s\n",
144 $name ?
"\n$name" : "",
150 app_message
"$name$url\t[starting]\n";
153 if ((!$force) and -f
"$saveas" ) {
154 app_warning
"$saveas exists\t[canceled]\n";
160 app_message
"creating cookie for $url...\n";
161 my @match = $url =~ /^(http:\/\
/[^\/]+)\
//;
163 my $domain=$match[0];
164 system("curl --url '$domain' -c '$cookie' -o '/dev/null'");
169 my $saveas_temp = "$saveas.downloading";
170 my @cmdline = build_cmdline
($downloader,$url,$saveas_temp,$refer,$cookie,$verbose,$maxtime);
171 my $r=process
("$name$url",\
@cmdline,2);
173 if($r==0 and -f
$saveas_temp) {
174 unlink ($saveas) if(-f
$saveas);
175 rename($saveas_temp,$saveas) or die("$!\n");
176 &log("$url->$saveas\n","$DOWNLOADLOG") if($logging);;
177 app_ok
"$name$saveas\t[completed]\n";
181 unlink $saveas_temp if(-f
$saveas_temp);
182 app_warning
"$name$url\t[killed]\n";
186 unlink $saveas_temp if(-f
$saveas_temp);
187 app_error
"$name$url\t[failed]\n";
188 &log("$url->$saveas\n","$FAILLOG") if($logging);;