Getting content from an RSS feed

February 20th, 2005

At http://wiley.ed.usu.edu/index_html/2004092301 David Wiley explains how to parse an RSS feed, extract content from it and render that content as HTML in a page in your Plone site.

His explanation tells you all you need to know. All you then need to do is get feedparser from Sourceforge and install it. I used wget to download it onto my Linux machine, but then found I couldn't unzip it with gunzip. So I had to use WinZip on my Windows PC and then copy the unzipped folder across to the Linux machine via SSH.

The install instructions in the README file are as follows:

To install:
$ python setup.py install

and it really is as simple as that. It works.

I then modified David Wiley's Python script a little (see below) because I wanted to use it for several pages of RSS-fed content. So I took the description out of the Python file (which I called rssfeed.py) and put it in the index.html page template instead. And that was about it. All I had to do then was choose a suitable RSS feed. The one I chose to try out was the list of most popular web pages in the "Programming" category at http://del.icio.us.

You can see the resulting dynamically generated page at www.itauthor.com/programming/popproglinks.

External method: doRSS

External method ID: doRSS
Title: Popular programming links
Module name: rssfeed
Function name: rssMagic

Python script: rssfeed.py

def rssMagic(myParam):
    import feedparser
    feed = feedparser.parse(myParam)
    html = ""
    for x in feed["items"]:
        html = html + "<a href='" + unicode(x[ "link" ]).encode("iso-8859-2", "replace") +"'>"
        html = html + unicode(x[ "title" ]).encode("iso-8859-2", "replace") + "</a><br /><br />\n"
    return html

Page template: index.html

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
      lang="en"
      metal:use-macro="here/main_template/macros/master"
      i18n:domain="plone">
<title>Popular programming links</title>
<body>
<metal:main fill-slot="main">
        <h1 class="documentFirstHeading">
          Popular programming links
        </h1> 
        <div metal:use-macro="here/document_actions/macros/document_actions">
            Document actions (print, sendto etc)
        </div> 
        <div class="documentDescription">
            The following links are taken from an RSS feed of the Programming category at
            <a href='http://del.icio.us/rss/opencontent'>del.icio.us</a>.
        </div> 
            <div tal:replace="structure python: here.doRSS(myParam='http://del.icio.us/rss/popular/programming')"/>

        <div metal:use-macro="here/document_byline/macros/byline">
          Get the byline - contains details about author and modification date.
        </div>
</metal:main>
</body>
</html>

Leave a comment