Now that you know how RSSFile objects look like, you’ll understand that creating a feed is all about filling the dictionaries of a new RSSFile object.
The basis of the channel dictionary is set:
>>> mysample = RSSFile()
>>> pprint(mysample.channel)
{'description': None,
'lastBuildDate': datetime.datetime(2007, 12, 6, 20, 24, 29, 765501),
'link': None,
'title': None}
Required elements were added. The lastBuildDate element is the datetime when you create the object. Of course you can replace it. Remember it is expressed in your local time zone, and will be encoded into GMT (UTC actually) representation.
No image nor item are created by default:
>>> pprint(mysample.image)
None
>>> pprint(mysample.items)
[]
As when loading a feed without an image element, the image attribute is set to None by default (and will not be written in the output feed if not set).
No default dictionary is set for items. Created one from scratch and append it to the items list.
To turn your channel, maybe your image, and your items into an XML file for RSS agregators to digest, simply use the itools.handlers API:
>>> mysample.to_str()
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>[...]
Notice the default encoding is UTF-8. You can change it throught the encoding keyword parameter.
The idea is to interpret the IF-Modified-Since request header, and set the Last-Modified response header when replying with the feed. Or send 304 Not Modified if nothing changed meanwhile.
The itools.web server automatically deals with these headers if you help it. It can even reply to agregators having the latest version.
To help it, you need to set a method__mtime__ method along with your method returning the feed.
For instance, if you implement the feed method in your Web application to return the RSS feed, set the feed__mtime__ method to return a datetime object. You will basically return the date of the last modified article.
The server will call this method and compare the value with the date sent by the agregator.
An example to clarify it:
from itools.web import Node
class News(Node):
def feed__mtime__(self):
# Returb for example the modification date
# of the last published article.
def feed(self, context):
# The server already replied to agregators having the latest
# version. So there is something to send.
feed = RSSFile()
# Fill in the feed with articles modified after the date given in
# the request header
request = context.request
# Set the content type, important!
response = context.response
response.set_header('content-type', "application/rss+xml")
# Send the feed, the server will set "Last-Modified"
return feed.to_str()
More details can be found in the itools.web chapter.