One of the must-have necessities for any blogging or other web publishing software is the RSS (Really Simple Syndication) feed. RSS, or its younger and sexier sibling Atom, is the file format used by news aggregators, which give you a frequent listing of posts and stories published by your favorite blogs, newspapers, journals, whatever you want, without you having to actually go to their website. Usually the RSS feed is generated by some library of code where you construct your feed as an array of objects or data structures. Here's how you put together an RSS feed with PyRSS2Gen, for instance:
rss = PyRSS2Gen.RSS2( title = "Andrew's PyRSS2Gen feed", link = "http://www.dalkescientific.com/Python/PyRSS2Gen.html", description = "The latest news about PyRSS2Gen, a Python library for generating RSS2 feeds", lastBuildDate = datetime.datetime.now(), items = [ PyRSS2Gen.RSSItem( title = "PyRSS2Gen-0.0 released", link = "http://www.dalkescientific.com/news/030906-PyRSS2Gen.html", description = "Dalke Scientific today announced PyRSS2Gen-0.0, a library for generating RSS feeds for Python. ", guid = PyRSS2Gen.Guid("http://www.dalkescientific.com/news/030906-PyRSS2Gen.html"), pubDate = datetime.datetime(2003, 9, 6, 21, 31)), PyRSS2Gen.RSSItem( title = "Thoughts on RSS feeds for bioinformatics", link = "http://www.dalkescientific.com/writings/diary/archive/2003/09/06/RSS.html", description = "One of the reasons I wrote PyRSS2Gen was to experiment with RSS for data collection in " "bioinformatics. Last year I came across...", guid = PyRSS2Gen.Guid("http://www.dalkescientific.com/writings/diary/archive/2003/09/06/RSS.html"), pubDate = datetime.datetime(2003, 9, 6, 21, 49)), ])
But here's another solution I like: use a template. After all, the whole point of templating is to separate the way you generate your content from the way you present it. Among other things, this makes it easy to produce multiple views of the same data, since you can reuse the code that generates it and just stick in a different template. And since each RSS feed is supposed to correspond to a web page, half the work the generation code — should be done already. You can just create an outline of an RSS feed in your favorite templating system (just read right off the spec line by line) and pick either the web page template or the RSS template depending on the URL. Look at this Clearsilver example I made:
<?cs each:item = feed.items ?><item> <?cs if:item.title ?><title><?cs var:item.title ?></title><?cs /if ?> <?cs if:item.link ?><link><?cs var:item.link ?></link><?cs /if ?> <?cs if:item.description ?><description><?cs var:item.description ?></description><?cs /if ?> <?cs if:item.author ?><author><?cs var:item.author ?></author><?cs /if ?> <?cs each:category = item.categories ?> <category<?cs if:category.domain ?> domain="<?cs var:category.domain ?>"<?cs /if ?>><?cs var:category ?></category> <?cs /each ?> <?cs if:item.comments ?><comments><?cs var:item.comments ?></comments><?cs /if ?> <?cs if:item.enclosure ?> <enclosure url="<?cs var:item.enclosure.url ?>" length="<?cs var:item.enclosure.length ?>" type="<?cs var:item.enclosure.type ?>"/> <?cs /if ?> <?cs if:item.publication_date ?><pubDate><?cs var:item.publication_date ?></pubDate><?cs /if ?> <source url="<?cs var:feed.url ?>"><?cs var:feed.title ?></source> </item><?cs /each ?>
As a bonus, that actually looks like an RSS feed, so now you have a handy reference for all the different fields you can put in.
I decided it's about time to start syncing the posts I make here with Facebook to get some exposure for this site. Not because I think anyone will really care about my content, but everyone else seems to be doing it. I guess it puts pressure on me to write more interesting stuff, that's probably good.
Anyway, to test out the system and also as sort of an unsolicited "kickback" to what seems like it might be a good program, I wanted to mention RSS Graffiti. I picked up on it from a mention on the Webapps Stack Exchange site. Basically it's an RSS/Atom feed aggregator: you configure it with the URLs of any number of feeds, and specify which ones should show up on your profile and the profiles of any groups, events, or pages you administer. The app checks periodically for new entries and posts them up to the destinations you specified. Nice and simple, which is actually just what I've been looking for in a Facebook RSS aggregator for a while. Here's hoping it works as well as it looks!
I've been working on this for a while now, and finally my blog has a functional RSS feed. It's the super-ghetto RSS2 format, rather than the newer, slicker (and more complex) Atom, though, so nice things like, I don't know, HTML(!!) are a no-go, and it's not even W3C standard compliant, but it works with my RSS reader which is good enough to start with. Use at your own risk.
And not to worry, someday I'll do this properly and set up an Atom feed.