Simple Perl RSS Processor

Every one and their wife is using RSS these days, not least me. Here's the code for a really simple Perl RSS processor. It uses XML::RSSLite to do the XML processing, so it's really light weight. It's incredibly simple, and needs modification of code to change the output HTML. Still, it's useful for fetching someone's RSS and making it into HTML that you can paste into something else (perhaps by an SSI?). I use it to fetch my Blogroll via RSS and SSI include it, for example.
<--break-->
#!/usr/bin/perl -w

use HTTP::Headers;
use HTTP::Request;
use HTTP::Response;
use LWP::UserAgent;
use HTTP::Status;

# Get this from http://www.cpan.org/authors/id/J/JP/JPIERCE/
use XML::RSSLite;

# Where do we save the processed info (if called
# with a command line argument)
$tempfile="/tmp/rssfeed.html";

$rssurl="http://www.coofercat.com/rss.xml";

$table_start="<table cellspacing=\"0\" cellpadding=\"0\">";
$table_stop="</table>";
$table_row_start="<tr>";
$table_row_stop="</tr>";
$table_cell_start="<td><font face=\"verdana,arial,helvetica\" size=\"-2\">";
$table_cell_stop="</font></td>";
$title_start="<b>";
$title_stop="</b>";
$line_prefix="ยป ";
$line_suffix="";

$default_text="
Whoops! Couldn't fetch the RSS feed!";

# do_request: Use Perl's LWP to fetch the RSS data from the remote site
sub do_request
{
my $ua=new LWP::UserAgent;

# Make the actual request...
my $request = new HTTP::Request( "GET", $main::rssurl );

my $response=$ua->request($request);

return $response;
}

MAIN:
{
my $out=STDOUT;

if(defined($ENV{'REQUEST_METHOD'}))
{
print $out "Content-Type: text/html\n\n";
}

if(defined($ARGV[0]))
{
$out=FH;
unless(open($out, "> $tempfile"))
{
print STDERR "Failed to open temp file\n";
exit 2;
}
}

my $response=&do_request;

if($response->code() != 200)
{
print $out "<!-- failed to fetch RSS feed -->\n";
print $out $default_text;
exit 1;
}

my %result=();
my $content=$response->as_string;
my $item;

parseRSS(\%result,\$content);

print $out $table_start . "\n";
print $out $table_row_start . $table_cell_start . "\n";
print $out $title_start . $result{'title'} . $title_stop . "\n";
# Also available: $request{'link'} $request{'description'}
print $out $table_cell_stop . $table_row_stop. "\n";

foreach $item (@{$result{'item'}})
{
print $out $table_row_start . $table_cell_start;
print $out $line_prefix . "<a href=\"$item->{'link'}\">";
print $out "$item->{'title'}";
print $out "</a>" . $line_suffix . "\n";
print $out $table_cell_stop . $table_row_stop . "\n";
# Could also do something with $item->{'description'}
}
print $out $table_stop . "\n";

exit 0;
}

Submitted by coofercat on Sat, 2003-08-02 13:31