#!/usr/bin/perl ########################################### # msa - Access Mike's script archive from # the command line. # Mike Schilli, 2002 (m@perlmeister.com) ########################################### use warnings; use strict; use LWP::UserAgent; use HTTP::Request::Common; use HTML::Parser; use File::Basename; our $VERSION = "1.0"; our $CVSVERSION = '$Revision: 1.7 $'; our $MSA_URL = "http://perlmeister.com/scripts/index.html"; my $ua = LWP::UserAgent->new(); my $resp = $ua->request(GET $MSA_URL); die "Cannot connect to $MSA_URL" if $resp->is_error(); my @items; my $parser = HTML::Parser->new( start_h => [ sub { if($_[0] eq "a") { return if $_[1]->{href} !~ m#^/scripts#; my $script = basename($_[1]->{href}); $script =~ s/\.html$//; push @items, $script; } }, 'tagname,attr' ], ); $parser->parse($resp->content()) || die $!; $|=1; while(1) { print ">"; my $in = ; chomp $in; if($in =~ /^ls$/i) { print "@items\n"; } elsif($in =~ /^get\s+(.*)/i) { get_one($1); } elsif($in =~ /^getall/) { get_one($_) for @items; } elsif($in =~ /^q/i) { last; } elsif($in =~ /^h/i) { print "Commands: ls get q\n"; } } ################################ sub get_one { ################################ my($script) = @_; my $url = dirname($MSA_URL) . "/download/$script"; print "Getting $url ...\n"; my $resp = $ua->request(GET $url); if($resp->is_error()) { print "Failed to get $url\n"; next; } if(-f $script) { print "$script already exists. Ok to overwrite (y/n)"; my $in = ; if($in !~ /y/i) { print "Not fetched.\n"; next; } } open F, ">$script" or die "Can't open $script"; print F $resp->content(); close F; if($script !~ /\.pm$/) { chmod 0755, $script or die "Cannot chmod $script"; } print "$script ready\n"; } __END__ =head1 NAME msa -- Access Mike's script archive interactively =head1 DOWNLOAD _SRC_HERE_ =head1 SYNOPSIS msa =head1 DESCRIPTION For convenient command line access to Mike's Script archive, here's a simple ftp-like utility to browse and download from it. Just call C from the command line with no arguments, and it will listen to one of the following commands: =over 4 =item I Help. Display all available commands. =item I List all available scripts and modules on Mike's Script Archive. =item I Fetch the script from Mike's Script Archive and save it in a file with the same name in the current directory. If a file with this name exists already, C will have the user confirm to overwrite it. =item I Get all scripts in msa at once. They'll all going to be copied into the current directory and made executable, so you might want to call it like mkdir ~/bin cd ~/bin msa msa> getall ... =item I Quit the shell. =back =head1 EXAMPLES $ msa >h Commands: ls get q >ls CronDaemon.pm Pick.pm htmlstylize relay limitmail f amazon >get Pick.pm Getting http://perlmeister.com/scripts/Pick.pm ... Pick.pm ready >q $ =head1 LEGALESE Copyright 2002 by Mike Schilli, all rights reserved. This program is free software, you can redistribute it and/or modify it under the same terms as Perl itself. =head1 AUTHOR 2002, Mike Schilli