Previous topic

webhelpers – helper functions for web applications

Next topic

webob – WebOb

Generating RSS and Atom Feeds

The webhelpers.feedgenerator module provides an API for programmatically generating syndication feeds from a Pylons application (your TurboGears 2.2.2 application is a particular configuration of Pylons).

The feed generator is intended for use in controllers, and generates an output stream. Currently the following feeds can be created by using the appropriate class:

  • RssFeed
  • RssUserland091Feed
  • Rss201rev2Feed
  • Atom1Feed

All of these format specific Feed generators inherit from the SyndicationFeed() class and you use the same API to interact with them.

Example controller method:

from helloworld.lib.base import BaseController
from tg.controllers import CUSTOM_CONTENT_TYPE
from webhelpers.feedgenerator import Atom1Feed
from pylons import response
from pylons.controllers.util import url_for

class CommentsController(BaseController):

    @expose(content_type=CUSTOM_CONTENT_TYPE)
    def atom1( self ):
        """Produce an atom-1.0 feed via feedgenerator module"""
        feed = Atom1Feed(
            title=u"An excellent Sample Feed",
            link=url_for(),
            description=u"A sample feed, showing how to make and add entries",
            language=u"en",
        )
        feed.add_item(title="Sample post",
                      link=u"http://example.com/posts/sample",
                      description="Testing.")
        response.content_type = 'application/atom+xml'
        return feed.writeString('utf-8')

To have your feed automatically discoverable by your user’s browser, you will need to include a link tag to your template/document’s head. Most browsers render this as a small RSS icon next to the address bar on which the user can click to subscribe.

<head>
    <link rel="alternate" type="application/atom+xml" href="./atom1" />
</head>

Normally you will also want to include an in-page link to the RSS page so that users who are not aware of or familiar with the automatic discovery can find the RSS feed. FeedIcons has a downloadable set of icons suitable for use in links.

<a href="./atom1"><img src="/images/feed-icon-14x14.png" /> Subscribe</a>

The various feed generators will escape your content appropriately for the particular type of feed.

class webhelpers.feedgenerator.SyndicationFeed(title, link, description, language=None, author_email=None, author_name=None, author_link=None, subtitle=None, categories=None, feed_url=None, feed_copyright=None, feed_guid=None, ttl=None, **kwargs)

Base class for all syndication feeds. Subclasses should provide write()

__init__(title, link, description, language=None, author_email=None, author_name=None, author_link=None, subtitle=None, categories=None, feed_url=None, feed_copyright=None, feed_guid=None, ttl=None, **kwargs)
add_item(title, link, description, author_email=None, author_name=None, author_link=None, pubdate=None, comments=None, unique_id=None, enclosure=None, categories=(), item_copyright=None, ttl=None, **kwargs)

Adds an item to the feed. All args are expected to be Python Unicode objects except pubdate, which is a datetime.datetime object, and enclosure, which is an instance of the Enclosure class.

add_item_elements(handler, item)

Add elements on each item (i.e. item/entry) element.

add_root_elements(handler)

Add elements in the root (i.e. feed/channel) element. Called from write().

item_attributes(item)

Return extra attributes to place on each item (i.e. item/entry) element.

latest_post_date()

Returns the latest item’s pubdate. If none of them have a pubdate, this returns the current date/time.

root_attributes()

Return extra attributes to place on the root (i.e. feed/channel) element. Called from write().

write(outfile, encoding)

Outputs the feed in the given encoding to outfile, which is a file-like object. Subclasses should override this.

writeString(encoding)

Returns the feed in the given encoding as a string.

class webhelpers.feedgenerator.Enclosure(url, length, mime_type)

Represents an RSS enclosure