RSS.py - RSS 2.0 builder


The rss.py is a small open-source library written in Python 3 that helps generating RSS 2.0 feeds in easy and convenient way.

1. Purpose
While building this site, I looked for a simple standalone Python 3 RSS 2.0 feed generating library. After a while, it looked like that I had to write the library from a scratch. Fortunately, RSS is a well-described standard, and there are lots of usage examples in the internet (e.g. on w3schools).
The result of my efforts is an object-oriented, Python3-compatible, well-documented library.

2.Key features
  • Full RSS 2.0 specification compliance
  • All RSS 2.0 elements are represented via corresponding classes
  • Automatically create CDATA nodes, for elements that contain markup characters
  • Various XML-formatting methods
  • Inline documentation with usage examples
  • Compatible with Python 3 only
  • Distributed under GPL v3

3. Using the library
The library usage is very simple, and I'm sure that you'll catch up the basic idea, just looking at the code below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import rss
from datetime import datetime

channel = rss.Channel('About programming',
                      'http://www.example.com/rss.xml',
                      'This is my news feed about...',
                      generator = 'rss.py',
                      pubdate = datetime.now(),
                      category = rss.Category('Development/Web development', 
                                              ('RFC','http://www.example.com/rfc')),
                      language = 'en-US')

item1 = rss.Item(channel, 
                 'What\'s new in python 3', 
                 'http://www.example.com/whats-new-in-python.html',
                 'This is a blog post about Python 3',
                 pubdate = 'Sun, 14 June 2009 01:39:47 GMT',
                 enclosure = rss.Enclosure('http://www.example.com/antigravity.mp3', 
                                          202400, 'audio/mpeg'))
item2 = rss.Item(channel, 
                 'Strings in Python', 
                 'http://www.example.com/strings-in-python.html',
                 'Working with Python strings',
                 category = 'Development/Python/Strings',
                 pubdate = datetime.now())

channel.additem(item1)
channel.additem(item2)
print(channel.toprettyxml())

The necessary imports for this example are the rss module and datetime object from Python's standard library.
Then RSS 2.0 channel is constructed. The required RSS 2.0 Channel and Item elements are:
  1. Title
  2. Link
  3. Description

These elements have to be passed both to rss.Channel's and rss.Item's constructors.
All optional arguments should be passed as keyword arguments. The arguments' names correspond to the RSS 2.0 optional channel/item elements' names in lower case. All arguments (including the required ones) could be passed as objects of corresponding type, e.g. as
1
generator = 'rss.py'
or
1
generator = rss.Generator('rss.py')

Most arguments (generator, language, category, pubdate, webmaster, copyright etc.) could be passed as string.
Some arguments of rss.Channel/rss.Item constructors should be passed as objects (enclosure, guid, source, cloud, image etc.).

The items are added to a channel via Channel object's additem() method.

The last step is to get the feed as XML. The toxml() and toprettyxml() methods are formatting the feed's code to XML (utf-8 encoding is used by default) in "tight" or "pretty" way. Here is the result of the code snippet described above.

Download
You can download the rss.py library here.
Any questions, comments and suggestions are welcome.

Leave a comment

Introduce yourself:
Comment: BB-codes
Captcha:
captcha