#!/usr/bin/perl use File::Path qw/ mkpath /; use LWP::Simple qw/ getstore is_success $ua /; use URI; use strict; use warnings; # ---------------------------------------------------- # my %config = ( 'agent' => 'cjf/0.0.1', 'archive' => 'archive', ); # ---------------------------------------------------- # my $url = shift || 'http://www.perlmonks.org/'; # :-) # Convert $url into a file path. my $file = join '/', $config{'archive'}, url_as_path($url); die "Couldn't parse $url\n" if $file eq $config{'archive'}; # Determine and create the directory (and any # missing directories above) that $file will # be saved to. Errors get propagated via die(). my($path) = $file =~ m<(.*)/[^/]+$>; mkpath( [$path], 0, 0755 ); # Configure our 'browser', and fetch/archive the # requested URL. $ua->agent( $config{'agent'} ); my $rc = getstore( $url => $file ); die "Couldn't archive $url: $rc\n" unless is_success($rc); # ---------------------------------------------------- # sub url_as_path { # Convert an URI to a Unix-style path, sans # any protocol my $url = URI->new(shift); (my $path = $url->host) =~ tr[.][/]; $path .= $url->path; # Assign default name if $url appears to be a # directory instead of a file. $path .= 'index.html' if substr( $url->path, -1 ) eq "/" ; $path; }