Complete Geek Boutique

Nothing in cyberspace is sacred

Add RSS feeds to your Web site with Perl XML::RSS

This little perl module works like a charm to add RSS feeds without a lot of effort. You’ll find the most work in initial setup, but things get automatic after that.

The XML::RSS package is specifically designed to read and parse RSS feeds. When you give XML::RSS an RSS feed, it converts the various <item>s in the feed into array elements, and exposes numerous methods and properties to access the data in the feed. XML::RSS currently supports versions 0.9, 0.91, and 1.0 of RSS.

Written entirely in Perl, XML::RSS isn’t included with Perl by default, and one way to install it is from CPAN. Detailed installation instructions are provided in the download archive, but by far the simplest way to install it is to use the CPAN shell, as follows:

shell> perl -MCPAN -e shellcpan> install XML::RSS

If you use the CPAN shell, dependencies will be automatically downloaded for you (unless you told the shell not to download dependent modules). If you manually download and install the module, you may need to download and install the XML::Parser module before XML::RSS can be installed. If you don’t already have it, you’ll also need to download the LWP::Simple package, too. The latest version of XML::RSS is currently 1.31.

Other ways to download XML-RSS: If you’re using a Mac, it’s a good idea to use Fink to download XML-RSS, and all of the dependencies will build. Caution: If you’re using Leopard, first download the LATEST Fink, then download the XML-RSS module for Perl 5.8.6, since the module won’t build properly for Perl 5.8.8. If you’re using MacPorts, it freezes if you try to install XML-RSS on Leopard, so use Fink (in fact, MacPorts is pretty much useless on Leopard. Period). If you’re using another flavor of Linux, go to your package manager and download XML-RSS according to your system’s specifications.

Browser display

This first script is really designed to show a styled RSS feed in your web browser using XML-RSS. Personally, I find it of limited value, but it’s useful for demonstration purposes. You’ll need to place the script into your cgi-bin, and chmod the script as executable (755). This script will gather RSS from SlashDot and you can call on it in your web browser.

#!/usr/bin/perl# import packages
use XML::RSS;
use LWP::Simple;

# initialize object
$rss = new XML::RSS();

# get RSS data
$raw = get('http://www.slashdot.org/index.rss');

# parse RSS feed
$rss->parse($raw);

# print HTML header and page
print "Content-Type: text/html\n\n";
print "<html><head><basefont face=Arial size=8></head><body>"; print "<table
width=300 border=1 cellspacing=0 cellpadding=5>"; print "<tr><td
align=center bgcolor=Silver>" . $rss->channel('title') .
"</td></tr>";
print "<tr><td>";

Remember to make the script executable (chmod 755). Now, go to your web browser and call on the script. You’ll see a feed from SlashDot.

Writing a feed to a file

Now to create a shell script to capture a feed and write it to a file. This uses wget (available in nearly all Linux distributions) to get the RSS feed from a site and write it to a file on your computer. Now, this script is useful. You can then choose which program to display the saved feed in — ranging from a browser to a program like rss2html or feed2js.

Here’s an example of such a script that writes an RSS feed to a file using wget:

#!/bin/bash

/bin/wget http://www.freshmeat.net/backend/fm.rdf -O /filelocation/freshmeat.rdf

“File location” is where you want to save the file, and “freshmeat.rdf” is the name of the file. No, it doesn’t matter whether the feed ends in .rss or .rdf — the browser or feed reader reads each type of file the same; i.e., as a RSS feed.

You can, of course, put several feeds in this script, with the same or different file destinations, and then schedule a cron job to update all feeds at set intervals.

That’s the crux of how XML-RSS works. If you want to see a more detailed tutorial on XML-RSS, you can visit TechRepublic.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Information

This entry was posted on November 29, 2007 by in Free RSS programs.
%d bloggers like this: