Section 10.3. Daily Doonesbury


10.3. Daily Doonesbury

The morning isn't quite the same without the daily dose of Doonesbury, the Pulitzer Prize winning cartoon strip by Garry Trudeau. But, frankly, the nearest newspaper stand is far too far to go before I've had morning coffee and typing in http://www.doonesbury.com on an empty stomach is a bit much. Why not have it delivered via RSS? You just need a web server, or a proper operating system capable of running CGI scripts, and the following code. This is tremendously simple stuffbut distinctly pleasing to have set up for your morning coffee.

10.3.1. Walking Through the Code

We'll go with the traditional start: strict;, warnings;, CGI with DATE::Manip, and the joy that is XML::RSS all being loaded for your coding pleasure. So far, so simple.

use strict; use warnings; use CGI qw(:standard); use Date::Manip; use XML::RSS;

The Doonesbury site, thankfully, names its image files after the date, in the format YYMMDD. So, first, you work out the date, then add a single entry containing escaped HTML that displays the image file you want.

my $todays_date = &UnixDate( "today", "%y%m%d" ); my $this_year = &UnixDate("today","%Y"); my $rss = new XML::RSS( version => '2.0' ); $rss->add_item(     title => "Doonesbury for $todays_date",     link  => "http://images.ucomics.com/comics/db/$this_year/db$todays_date.gif",     description => '<img src="/books/1/114/1/html/2/http://images.ucomics.com/comics/db/2004/'       . "db$todays_date.gif"       . '"/>' );

Then it's just a matter of adding in the channel information and serving the thing out with the correct MIME type:

$rss->channel(     title       => "Today's Doonesbury",     link        => "http://www.doonesbury.com",     description => "Doonesbury, by Garry Trudeau" ); print header('application/xml+rss'); print $rss->as_string;

You can obviously use this sort of code to produce RSS feeds of any online comic strip. Do the author a favor, however: don't make the feed public, visit his site once in a while, and buy some schwag. You're getting great stuff for freewith the added convenience you've added, for sureand it's nice to give something back.

10.3.2. The Entire Listing

#!/usr/bin/perl use strict; use warnings; use CGI qw(:standard); use Date::Manip; use XML::RSS; my $todays_date = &UnixDate( "today", "%y%m%d" ); my $rss = new XML::RSS( version => '2.0' ); $rss->add_item(     title => "Doonesbury for $todays_date",     link  => "http://images.ucomics.com/comics/db/2004/db$todays_date.gif",     description => '&lt;img src="/books/1/114/1/html/2/http://images.ucomics.com/comics/db/2004/'       . "db$todays_date.gif"       . '"/&gt;' ); $rss->channel(     title       => "Today's Doonesbury",     link        => "http://www.doonesbury.com",     description => "Doonesbury, by Garry Trudeau" ); print header('application/xml+rss'); print $rss->as_string;



    Developing Feeds with RSS and Atom
    Developing Feeds with Rss and Atom
    ISBN: 0596008813
    EAN: 2147483647
    Year: 2003
    Pages: 118

    Similar book on Amazon

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net