5 # This script extracts all links from a HTML page, compares them to a pattern
6 # entered on the command line and then downloads matching links into the
7 # target dir (also specified on the command line).
9 # Written to use 'curl' for URL fetching, uses the source file names in the
12 # Author: Daniel Stenberg <Daniel.Stenberg@sth.frontec.se>
13 # Version: 0.1 Oct 7, 1998
23 if($ARGV[0] eq "-v" ) {
28 if($ARGV[0] eq "-d" ) {
33 elsif($ARGV[0] eq "-h" ) {
44 (($getdir eq "") && !$display) ||
46 print "Usage: $0 [-hv] <full source URL> <target dir> [regex]\n",
47 " Use a traling slash for directory URLs!\n",
48 " Use \"quotes\" around the regex!\n",
49 " -h This help text\n",
50 " -d Display matches only instead of downloading\n",
55 # change to target directory:
57 die "couldn't cd into $getdir";
59 # This is necessary from where I tried this:
60 #$proxy =" -x 194.237.142.41:80";
62 # linkchecker, URL will be appended to the right of this command line
63 # this is the one using HEAD:
64 $linkcheck = "curl -s -m 20 -I$proxy";
66 # as a second attempt, this will be used. This is not using HEAD but will
67 # get the whole frigging document!
68 $linkcheckfull = "curl -s -m 20 -i$proxy";
70 # htmlget, URL will be appended to the right of this command line
71 $htmlget = "curl -s$proxy";
73 # urlget, URL will be appended to the right of this command line
74 # this stores the file with the remote file name in the current dir
75 $urlget = "curl -O -s$proxy";
77 # Parse the input URL and split it into the relevant parts:
82 if($inurl=~ /^([^:]+):\/\
/([^\/]*)\
/(.*)\/(.*)/ ) {
88 elsif ($inurl=~ /^([^:]+):\/\
/([^\/]*)\
/(.*)/ ) {
94 if($getpath !~ /\//) {
100 elsif ($inurl=~ /^([^:]+):\/\
/(.*)/ ) {
107 print "Couldn't parse the specified URL, retry please!\n";
114 #print "protocol = $getprotocol\n";
115 #print "server = $getserver\n";
116 #print "path = $getpath\n";
117 #print "document = $getdocument\n";
121 open(HEADGET
, "$linkcheck $geturl|") ||
122 die "Couldn't get web page for some reason";
126 if($_ =~ /HTTP\/.*3\d\d
/) {
130 ($_ =~ /^Location: (.*)/)) {
141 if($pagemoved == 1) {
142 print "Page is moved but we don't know where. Did you forget the ",
147 open(WEBGET
, "$htmlget $geturl|") ||
148 die "Couldn't get web page for some reason";
176 while($in =~ /[^<]*(<[^>]+>)/g ) {
177 # we have a tag in $1
180 if($tag =~ /^<!--/) {
181 # this is a comment tag, ignore it
184 if($tag =~ /(src|href|background|archive) *= *(\"[^\"]\"|[^ )>]*)/i) {
186 if($url =~ /^\"(.*)\"$/) {
187 # this was a "string" now $1 has removed the quotes:
192 $url =~ s/([^\#]*)\#.*/$1/g;
195 # if the link was nothing than a #-link it may now have
196 # been emptied completely so then we skip the rest
201 # if this url already is done, do next
206 $done{$url} = 1; # this is "done"
209 if($tag =~ /< *([^ ]+)/) {
219 @links = &GetLinks
($in);
225 if($url =~ /^([^:]+):/) {
229 # this is an absolute link on the same server:
232 $link = "$getprotocol://$getserver$url";
235 # from the scanned page's dir
238 if(length($getpath) &&
239 ($getpath !~ /\/$/) &&
241 # lacks ending slash, add one to the document part:
244 $link = "$getprotocol://$getserver/$getpath$nyurl";
248 if($link =~ /$getregex/) {
254 print "Gets $link\n";
256 print `$urlget $link`;