This is a remarkably simple trick which I’ve found very handy. With a few lines of Perl you can take any RSS feed and format it to your liking.
Get the Feed
You can do this using LWP::Simple:
use LWP::Simple;
my $feed_url = 'http://feeds.bbci.co.uk/news/rss.xml';
my $feed = get($feed_url)
or die ("Failed to fetch feed.");
Process the Raw Result
Using XML::RSS, convert the raw feed into a more manageable hash.
use XML::RSS;
my $rss = XML::RSS->new();
$rss->parse($feed);
Format to Your Liking
Template::Toolkit can take in a template and a hash reference of values to substitute into the template.
# Define a template
my $template = <<"TEMPLATE";
[% channel.title %]
Headlines:
[% FOREACH item = items %]
[% item.pubDate %]t[% item.title %]
[% END %]
TEMPLATE
This simple template will take the BBC news feed from above and print out a list of headlines with publication dates.
my $tt = Template->new()
or die ("Failed to load template: $Template::ERRORn");
# Combine the template with the processed RSS feed.
$tt->process ( $template, $rss )
or die $tt->error();
Putting it All Together
#!/usr/bin/perl
use strict;
use warnings;
use XML::RSS;
use LWP::Simple;
use Template;
##################
# Configuration:
#
##################
my $feed_url = 'http://feeds.bbci.co.uk/news/rss.xml';
my $template = <<"TEMPLATE";
[% channel.title %]
Headlines:
[% FOREACH item = items %]
[% item.pubDate %]t[% item.title %]
[% END %]
TEMPLATE
##################
##################
my $tt = Template->new()
or die ("Failed to load template: $Template::ERRORn");
my $feed = get($feed_url)
or die ('Failed to fetch feed.');
my $rss = XML::RSS->new();
$rss->parse($feed);
$tt->process ( $template, $rss )
or die $tt->error();
rob@arrakis:~/public_html/rss-reader$ perl rss-reader.pl
BBC News - Home
Headlines:
Fri, 14 Sep 2012 12:15:22 GMT Kate privacy invasion 'grotesque'
Fri, 14 Sep 2012 11:49:10 GMT US missions on film protest alert
Fri, 14 Sep 2012 12:25:25 GMT Alps attack girl returning to UK
Fri, 14 Sep 2012 10:17:03 GMT Woman is held after car body find
In this example the RSS feed and template are defined in code but they can just as easily be defined in files or a database allowing for changes/additions without deploying new code.