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

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