the further adventures of

Mike Pirnat

a leaf on the wind

« Previous Post Next Post »

Fun With RSS and Python

I should have looked into this stuff a while ago, because it's entirely too handy and, in a word, pythonic. I will probably rewrite my crufty home-brew LiveJournal RSS parser using Mark Pilgrim's excellent Universal Feed Parser. Additionally, since I'm not sure if Planet allows easy filtering based on category, and I might like my occasional TurboGears rambling to show up there, I think I will combine feedparser with PyRSS2Gen to build a simple little program to pull, filter, and re-emit RSS feeds.

Of course, knowing my luck, something like that already exists, but at least it'll be fun. ;-)

Basically, it would be something like this:

import feedparser
import datetime
import PyRSS2Gen

# get the data
d = feedparser.parse('http://exilejedi.livejournal.com/data/rss/')

# do the filtering & build a list of RSSItem objects
items = [PyRSS2Gen.RSSItem(
    title = x.title,
    link = x.link,
    description = x.summary,
    guid = x.link,
    pubDate = datetime.datetime(
        x.modified_parsed[0],
        x.modified_parsed[1],
        x.modified_parsed[2],
        x.modified_parsed[3],
        x.modified_parsed[4],
        x.modified_parsed[5]))
    for x in d.entries if some_criteria(x)]

# make the RSS2 object
rss = PyRSS2Gen.RSS2(
    title = d.feed.title,
    link = d.feed.link,
    description = "ExileJedi's Filtered RSS Feed",
    lastBuildDate = datetime.datetime.now(),
    items = items)

#emit the feed
xml = rss.to_xml() # or perhaps do rss.write_xml(some_file)

blog comments powered by Disqus

« Previous Post Next Post »