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.
#!/usr/bin/perl -w

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

# Get this from
use XML::RSSLite;

# Where do we save the processed info (if called
# with a command line argument)


$table_start="<table cellspacing=\"0\" cellpadding=\"0\">";
$table_cell_start="<td><font face=\"verdana,arial,helvetica\" size=\"-2\">";
$line_prefix="ยป ";

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;

my $out=STDOUT;

print $out "Content-Type: text/html\n\n";

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;


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