#! /usr/bin/perl -w # addurl, copyright (c) 1998 Neil Moore # and Michael Slone # # This program may be distributed under the terms of the GNU General # Public License. # use strict; use vars qw($filename $total $opt_t $opt_l $opt_f $opt_u $opt_p); use IO::File; use LWP::UserAgent; use HTTP::Request; use HTML::HeadParser; use Getopt::Std; =pod =head1 NAME addurl - Bookmark page update utility =head1 SYNOPSIS B [B<-t>] [B<-l>] [B<-f> F] [I ...] =head1 DESCRIPTION B takes as arguments URLs of some sort (ftp://, file://, and http:// are accepted), then visits the URL and adds a link, with a fetched title, to the caller's bookmarks page. If no URLs are specified on the command line, they are read from stdin, one per line. =head1 OPTIONS =over 4 =item I<-f> F Use the specified filename instead of the default value. =item I<-l> Assume link is part of a list and use the ELIE element. =item I<-p> Assume link is not part of a list; use EPE as a separator. I<-p> overrides I<-l> and ${ADDURL_LIST} (see below). =item I<-t> Add link below the E!--TOP--E comment in the bookmarks file. The default action is to add below the E!--INSERT HERE--E comment. =back =head1 EXAMPLES =over 4 =item B -t http://www.cs.uky.edu/~neil/ This will add the page named ``La Page de Neil Moore'' to the bookmarks file as near the top of the bookmarks page as possible. =item B -l http://www.w3.org/ This will add the WWW Consortium's home page to the bookmarks file as part of a list. =item B -f lynx.html http://lynx.browser.org/ This example adds the Lynx browser's home page to an alternate links file named lynx.html. =back =cut sub callback($$$) { my($data, $response, $protocol) = @_; $total .= $data; die if ($data =~ /<\/title>/i); } sub urltoline($) { my $url = shift; my $ua = new LWP::UserAgent; my $hp = new HTML::HeadParser; my $request = new HTTP::Request 'GET', $url ; my $title; my $result; $total = ""; $ua->request($request, \&callback, 512); $hp->parse($total); $title = $hp->header('Title'); if (!defined $title || ! $title) { $result = "$url"; } elsif ($title =~ /^404/) { warn "Received 404 file not found on $url"; $result = ""; } else { $result = "" . $title . ""; } if ($result && $opt_l) { $result = "
  • $result\n"; } elsif ($result) { $result = "$result

    \n" } print $result; return $result; } sub geturls() { my @result; if (@ARGV) { @result = @ARGV; } else { while (<>) { chomp; push @result, $_; } } return @result; } sub readlist($\@) { my $fh = shift; my $urls = shift; my @result; my $pattern = "^"; if ($opt_t) { $pattern = "^"; } while (<$fh>) { push @result, $_; if (/$pattern/) { foreach (@$urls) { push @result, urltoline $_; } } } return @result; } MAIN: { my @urls; $filename = "$ENV{HOME}/public_html/urls.html"; if ($ENV{URL_FILE}) { $filename=$ENV{URL_FILE}; } if ($ENV{ADDURL_LIST}) { $opt_l = 1; } getopts('tlpf:'); if ($opt_t) { print "Adding to top of page.\n"; } if ($opt_p) { $opt_l = 0; } if ($opt_l) { print "Using

  • .\n"; } if ($opt_f) { print "Using $opt_f instead of $filename.\n"; $filename = $opt_f; } @urls = geturls; if (! @urls) { exit 0; } my $fh = new IO::File; $fh->open("< $filename") or die "Could not open $filename: $!"; my @lines = readlist $fh, @urls; $fh->open("> $filename") or die "Could not open $filename: $!"; $fh->print(@lines); $fh->close; } =pod =head1 ENVIRONMENT B uses ${HOME} to determine where to look for the default bookmarks file. If the environment variable ${URL_FILE} exists, the file pointed to by it is used as the default bookmarks file. If the environment variable ${ADDURL_LIST} exists, I<-l> is assumed. =head1 FILES ${HOME}/public_html/urls.html - the bookmarks file. See OPTIONS for a way to modify this. =head1 AUTHOR =over 4 =item Neil Moore wrote the original version of B. =item Michael Slone modified it to use various options and wrote L. =item Neil Moore modified it some more. =back =cut