home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / Share / Editores / Perl5 / perl / bin / lwp-rget.bat < prev    next >
Encoding:
DOS Batch File  |  1997-08-10  |  8.1 KB  |  328 lines

  1. @rem = '--*-Perl-*--
  2. @echo off
  3. perl -x -S %0 %1 %2 %3 %4 %5 %6 %7 %8 %9
  4. goto endofperl
  5. @rem ';
  6. #!perl
  7. #line 8
  8. #perl -w
  9.     eval 'exec perl -S $0 "$@"'
  10.     if 0;
  11.  
  12. =head1 NAME
  13.  
  14. lwp-rget - Retrieve WWW documents recursively
  15.  
  16. =head1 SYNOPSIS
  17.  
  18.  lwp-rget [--verbose] [--depth=N] [--limit=N] [--prefix=URL] <URL>
  19.  lwp-rget --version
  20.  
  21. =head1 DESCRIPTION
  22.  
  23. This program will retrieve a document and store it in a local file.  It
  24. will follow any links found in the document and store these documents
  25. as well, patching links so that they refer to these local copies.
  26. This process continues until there are no more unvisited links or the
  27. process is stopped by the one or more of the limits which can be
  28. controlled by the command line arguments.
  29.  
  30. This program is useful if you want to make a local copy of a
  31. collection of documents or want to do web reading off-line.
  32.  
  33. All documents are stored as plain files in the current directory. The
  34. file names chosen are derived from the last component of URL paths.
  35.  
  36. The options are:
  37.  
  38. =over 3
  39.  
  40. =item --depth=I<n>
  41.  
  42. Limit the recursive level. Embedded images are always loaded, even if
  43. they fall outside the I<--depth>. This means that one can use
  44. I<--depth=0> in order to fetch a single document together with all
  45. inline graphics.
  46.  
  47. The default depth is 5.
  48.  
  49. =item --limit=I<n>
  50.  
  51. Limit the number of documents to get.  The default limit is 50.
  52.  
  53. =item --prefix=I<url_prefix>
  54.  
  55. Limit the links to follow. Only URLs that start the prefix string are
  56. followed.
  57.  
  58. The default prefix is set as the "directory" of the initial URL to
  59. follow.  For instance if we start lwp-rget with the URL
  60. C<http://www.sn.no/foo/bar.html>, then prefix will be set to
  61. C<http://www.sn.no/foo/>.
  62.  
  63. Use C<--prefix=''> if you don't want the fetching to be limited by any
  64. prefix.
  65.  
  66. =item --sleep=I<n>
  67.  
  68. Sleep I<n> seconds before retrieving each document. This options allows
  69. you to go slowly, not loading the server you visiting too much.
  70.  
  71. =item --verbose
  72.  
  73. Make more noise while running.
  74.  
  75. =item --quiet
  76.  
  77. Don't make any noise.
  78.  
  79. =item --version
  80.  
  81. Print program version number and quit.
  82.  
  83. =item --help
  84.  
  85. Print the usage message and quit.
  86.  
  87. =back
  88.  
  89. Before the program exits the name of the file, where the initial URL
  90. is stored, is printed on stdout.  All used filenames are also printed
  91. on stderr as they are loaded.  This printing can be suppressed with
  92. the I<--quiet> option.
  93.  
  94. =head1 SEE ALSO
  95.  
  96. L<lwp-request>, L<LWP>
  97.  
  98. =head1 AUTHOR
  99.  
  100. Gisle Aas <aas@sn.no>
  101.  
  102. =cut
  103.  
  104. use strict;
  105.  
  106. use Getopt::Long;
  107. use URI::URL;
  108. use LWP::MediaTypes qw(media_suffix);
  109.  
  110. use vars qw($VERSION);
  111. use vars qw($MAX_DEPTH $MAX_DOCS $PREFIX $VERBOSE $QUIET $SLEEP);
  112.  
  113. $0 =~ s|.*/||;  # only basename left
  114. $VERSION = sprintf("%d.%02d", q$Revision: 1.10 $ =~ /(\d+)\.(\d+)/);
  115.  
  116. #$Getopt::Long::debug = 1;
  117. #$Getopt::Long::ignorecase = 0;
  118.  
  119. # Defaults
  120. $MAX_DEPTH = 5;
  121. $MAX_DOCS  = 50;
  122.  
  123. GetOptions('version'  => \&print_version,
  124.        'help'     => \&usage,
  125.        'depth=i'  => \$MAX_DEPTH,
  126.        'limit=i'  => \$MAX_DOCS,
  127.        'verbose!' => \$VERBOSE,
  128.            'quiet!'   => \$QUIET,
  129.        'sleep=i'  => \$SLEEP,
  130.        'prefix:s' => \$PREFIX,
  131.       ) || usage();
  132.  
  133. sub print_version {
  134.     require LWP;
  135.     my $DISTNAME = 'libwww-perl-' . LWP::Version();
  136.     print <<"EOT";
  137. This is lwp-rget version $VERSION ($DISTNAME)
  138.  
  139. Copyright 1996, Gisle Aas.
  140.  
  141. This program is free software; you can redistribute it and/or
  142. modify it under the same terms as Perl itself.
  143. EOT
  144.     exit 0;
  145. }
  146.  
  147. my $start_url = shift || usage();
  148. usage() if @ARGV;
  149.  
  150. require LWP::UserAgent;
  151. my $ua = new LWP::UserAgent;
  152. $ua->agent("$0/$VERSION " . $ua->agent);
  153. $ua->env_proxy;
  154.  
  155. unless (defined $PREFIX) {
  156.     $PREFIX = url($start_url);   # limit to URLs below this one
  157.     eval {
  158.     $PREFIX->eparams(undef);
  159.     $PREFIX->equery(undef);
  160.     };
  161.  
  162.     $_ = $PREFIX->epath;
  163.     s|[^/]+$||;
  164.     $PREFIX->epath($_);
  165.     $PREFIX = $PREFIX->as_string;
  166. }
  167.  
  168.  
  169. print <<"" if $VERBOSE;
  170. START     = $start_url
  171. MAX_DEPTH = $MAX_DEPTH
  172. MAX_DOCS  = $MAX_DOCS
  173. PREFIX    = $PREFIX
  174.  
  175.  
  176. my $no_docs = 0;
  177. my %seen = ();     # mapping from URL => local_file
  178.  
  179. my $filename = fetch($start_url);
  180. print "$filename\n" unless $QUIET;
  181.  
  182. sub fetch
  183. {
  184.     my($url, $type, $depth) = @_;
  185.     $url = url($url) unless ref($url);
  186.     $type  ||= 'a';
  187.     $type = 'img' if $type eq 'body';  # might be the background attribute
  188.     $depth ||= 0;
  189.  
  190.     # Print the URL before we start checking...
  191.     my $out = (" " x $depth) . $url . " ";
  192.     $out .= "." x (60 - length($out));
  193.     print STDERR $out . " " if $VERBOSE;
  194.  
  195.     # Can't get mailto things
  196.     if ($url->scheme eq 'mailto') {
  197.     print STDERR "*skipping mailto*\n" if $VERBOSE;
  198.     return $url->as_string;
  199.     }
  200.  
  201.     # The $plain_url is a URL without the fragment part
  202.     my $plain_url = $url->clone;
  203.     $plain_url->frag(undef);
  204.  
  205.     # If we already have it, then there is nothing to be done
  206.     my $seen = $seen{$plain_url->as_string};
  207.     if ($seen) {
  208.     my $frag = $url->frag;
  209.     $seen .= "#$frag" if defined($frag);
  210.     print STDERR "$seen (again)\n" if $VERBOSE;
  211.     return $seen;
  212.     }
  213.  
  214.     # Too much or too deep
  215.     if ($depth > $MAX_DEPTH and $type ne 'img') {
  216.     print STDERR "*too deep*\n" if $VERBOSE;
  217.     return $url;
  218.     }
  219.     if ($no_docs > $MAX_DOCS) {
  220.     print STDERR "*too many*\n" if $VERBOSE;
  221.     return $url;
  222.     }
  223.  
  224.     # Check PREFIX, but not for <IMG ...> links
  225.     if ($type ne 'img' and  $url->as_string !~ /^\Q$PREFIX/o) {
  226.     print STDERR "*outsider*\n" if $VERBOSE;
  227.     return $url->as_string;
  228.     }
  229.  
  230.     # Fetch document 
  231.     $no_docs++;
  232.     sleep($SLEEP) if $SLEEP;
  233.     my $res = $ua->request(HTTP::Request->new(GET => $url));
  234.  
  235.     # Check outcome
  236.     if ($res->is_success) {
  237.     my $doc = $res->content;
  238.     my $ct = $res->content_type;
  239.     my $name = find_name($res->request->url, $ct);
  240.     print STDERR "$name\n" unless $QUIET;
  241.     $seen{$plain_url->as_string} = $name;
  242.  
  243.     # If the file is HTML, then we look for internal links
  244.     if ($ct eq "text/html") {
  245.         # Save an unprosessed version of the HTML document.  This
  246.         # both reserves the name used, and it also ensures that we
  247.         # don't loose everything if this program is killed before
  248.         # we finish.
  249.         save($name, $doc);
  250.         my $base = $res->base;
  251.         # Follow and substitute links...
  252.         $doc =~ s/(<\s*(img|a|body)\b[^>]+\b(?:src|href|background)\s*=\s*)(["']?)([^>\s]+)\3/new_link($1, lc($2), $3, $4, $base, $depth+1)/gie;       #"; # help emacs
  253.     }
  254.     save($name, $doc);
  255.     return $name;                      
  256.     } else {
  257.     print STDERR $res->code . " " . $res->message . "\n" if $VERBOSE;
  258.     $seen{$plain_url->as_string} = "*BAD*";
  259.     return "*BAD*";
  260.     }
  261. }
  262.  
  263. sub new_link
  264. {
  265.     my($pre, $type, $quote, $url, $base, $depth) = @_;
  266.     $url = url($url, $base)->abs;
  267.     $pre . $quote . fetch($url, $type, $depth) . $quote;
  268. }
  269.  
  270. sub find_name
  271. {
  272.     my($url, $type) = @_;
  273.     #print "find_name($url, $type)\n";
  274.     $url = url($url) unless ref($url);
  275.  
  276.     my $path = $url->path;
  277.  
  278.     # trim path until only the basename is left
  279.     $path =~ s|.*/||;
  280.     $path =~ s|\..*||;
  281.     $path = "index" unless length($path);
  282.  
  283.     my $extra = "";  # something to make the name unique
  284.     my $suffix = media_suffix($type);
  285.  
  286.     while (1) {
  287.     # Construct a new file name
  288.     my $file = $path . $extra;
  289.     $file .= ".$suffix" if $suffix;
  290.     # Check if it is unique
  291.     return $file unless -f $file;
  292.  
  293.     # Try something extra
  294.     unless ($extra) {
  295.         $extra = "001";
  296.         next;
  297.     }
  298.     $extra++;
  299.     }
  300. }
  301.  
  302.  
  303. sub save
  304. {
  305.     my $name = shift;
  306.     #print "save($name,...)\n";
  307.     open(FILE, ">$name") || die "Can't save $name: $!";
  308.     print FILE $_[0];
  309.     close(FILE);
  310. }
  311.  
  312. sub usage
  313. {
  314.     die <<"";
  315. Usage: $0 [options] <URL>
  316. Allowed options are:
  317.   --depth=N         Maximum depth to traverse (default is $MAX_DEPTH)
  318.   --limit=N         A limit on the number documents to get (default is $MAX_DOCS)
  319.   --version         Print version number and quit
  320.   --verbose         More output
  321.   --quiet           No output
  322.   --sleep=SECS      Sleep between gets, ie. go slowly
  323.   --prefix=PREFIX   Limit URLs to follow to those which begin with PREFIX
  324.  
  325. }
  326. __END__
  327. :endofperl
  328.