JASON COX

ARCHIVE

COMICBOT

Originally published February 2002

Purpose -> Hands-free automated web comics.Design -> Quick and dirty.

Freedom to read my comics and eat lunch at the same time.

So I got sick of needlessly putting up with banner ads, useless web graphics and separate web sites, just to get my daily comic fix. Seeing as I'm all about efficiency, I wasted a couple of hours at work writing a simple script to retrieve my favorite comics from the web and display them on my local web page. Sometimes you just need the latest userfriendly and don't have time to waste waiting for it to load!

I know that there are utilities for doing this sort of thing out there (netcomic via freshmeat.net comes to mind), but I had never screwed around with perl's http interface, and was itching to write my first webbot.

The below script is really quite simple: I use perl's LWP::UserAgent module to gork a webpage (http link provided from a control file), parse the returned HTML with perl's HTML::TreeBuilder to extract the page's links, and scan the links for key words that uniquely describe the comics filename.

#!/usr/bin/perl -w

#make an agent
use lib "/packages/lib/perl5/site_perl";
use LWP::UserAgent;
use HTML::TreeBuilder;
use FileHandle;

$ua = new LWP::UserAgent;
$ua->agent("ComicGrab/0.1 " . $ua->agent);

unlink </u/carohde/public_html/comics/*>;
$maindir = "/u/carohde/public_html/comics/";
open(LIST, "/u/carohde/scripts/comicslist");
open(HOUT, ">/u/carohde/public_html/comics/index.html");

$tmp = "<html><head><title>Chuck\'s AutoComics Page</title></head>\n<body background=../newspaper.jpg>\n";
#$title = "<h1>Comics for " . timelocal() . "</h1><br>";
print HOUT $tmp;

while ( <LIST> ) {
    chomp;
        @ComicInfo = split /,/;
    print "Grabbing ", $ComicInfo[0], '....';
        autoflush STDOUT 1;
        grab_comics( @ComicInfo );
        print "Done.\n";
        autoflush STDOUT 1;
        print HOUT "<h1>" . $ComicInfo[0] . "</h1><br>\n";
        print HOUT "<img src=" . $ComicInfo[4] . "><br>\n";
}
    print "Grabbing Doctor FUN....";
        autoflush STDOUT 1;
        grab_DrFun( 'df.jpg' );
        print "Done.\n";
        autoflush STDOUT 1;
        print HOUT "<h1>Dr. Fun</h1><br>\n";
        print HOUT "<img src=df.jpg><br>\n";

    print "Grabbing RedMeat....";
        autoflush STDOUT 1;
        grab_RedMeat( 'redmeat.gif' );
        print "Done.\n";
        autoflush STDOUT 1;
        print HOUT "<h1>Red Meat</h1><br>\n";
        print HOUT "<img src=redmeat.gif><br>\n";

print HOUT "</body></html>\n";
close(HOUT);
close(LIST);

sub grab_comics {

my @parms = @_;
my $url = $parms[1];
my $unique = $parms[2];
my $filetype = $parms[3];
my $filename = $maindir.$parms[4];

#make a request
my $req = new HTTP::Request GET => $url;

#pass it off to the agent
my $res = $ua->request($req);

#see if it worked
if ($res->is_success) {

        #parse the html doc into logical parts
        my $htmltree = new HTML::TreeBuilder;
        $htmltree->parse($res->content);

        #examine all of the image links for the one we want
        for( @{$htmltree->extract_links( qw(img) )} ){
            my($link, $linkelem) = @$_;
            if ( $link =~ /$unique\d+?/ && $link =~ /$filetype/ ) {
            my @fetchargs = ("wget", "-O", $filename, "-q", $link);
            system(@fetchargs);
            }
        }
} else {
        print "Bummer, didn't work\n";
}
}

sub grab_DrFun {

my @parms = @_;
my $filename = $maindir.$parms[0];

my $link = "http://www.ibiblio.org/Dave/Dr-Fun/latest.jpg";
my @fetchargs = ("wget", "-O", $filename, "-q", $link);
system(@fetchargs);

}

sub grab_RedMeat {

my @parms = @_;
my $filename = $maindir.$parms[0];

my $link = "http://www.redmeat.com/redmeat/current/index-1.gif";
my @fetchargs = ("wget", "-O", $filename, "-q", $link);
system(@fetchargs);

}

Once I have the name of the link I just use the wget utility to pull the picture and store it locally & build a HTML page around the files. Pretty simple really. Then by sticking it in my daily crontab, I'm greeted each morning by my favorite comics. Cool, hey?

Fellow researchers here at the lab requested a version of the script that would just give them their daily RedMeat fix when they logged in. This seemed reasonable. Just one more item on the list of things needed to start the day: pants, coffee, RedMeat, etc. So I pulled the section of the above script that grabs RM and modified it slightly to display RM whenever it is run.

[NuX]

[the ubiquitous mailto]