2005-07-13 09:25:57 +08:00
|
|
|
"""
|
|
|
|
Syndication feed generation library -- used for generating RSS, etc.
|
|
|
|
|
|
|
|
Sample usage:
|
|
|
|
|
2007-09-15 16:29:56 +08:00
|
|
|
>>> from django.utils import feedgenerator
|
2005-07-13 09:25:57 +08:00
|
|
|
>>> feed = feedgenerator.Rss201rev2Feed(
|
|
|
|
... title=u"Poynter E-Media Tidbits",
|
|
|
|
... link=u"http://www.poynter.org/column.asp?id=31",
|
2010-10-09 16:12:50 +08:00
|
|
|
... description=u"A group Weblog by the sharpest minds in online media/journalism/publishing.",
|
2005-07-13 09:25:57 +08:00
|
|
|
... language=u"en",
|
|
|
|
... )
|
2010-05-09 05:38:27 +08:00
|
|
|
>>> feed.add_item(
|
|
|
|
... title="Hello",
|
|
|
|
... link=u"http://www.holovaty.com/test/",
|
|
|
|
... description="Testing."
|
|
|
|
... )
|
2005-07-13 09:25:57 +08:00
|
|
|
>>> fp = open('test.rss', 'w')
|
|
|
|
>>> feed.write(fp, 'utf-8')
|
|
|
|
>>> fp.close()
|
|
|
|
|
|
|
|
For definitions of the different versions of RSS, see:
|
|
|
|
http://diveintomark.org/archives/2004/02/04/incompatible-rss
|
|
|
|
"""
|
|
|
|
|
2008-08-12 06:22:26 +08:00
|
|
|
import datetime
|
2010-01-28 21:46:18 +08:00
|
|
|
import urlparse
|
2005-07-13 09:25:57 +08:00
|
|
|
from django.utils.xmlutils import SimplerXMLGenerator
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
from django.utils.encoding import force_unicode, iri_to_uri
|
2005-11-12 11:44:53 +08:00
|
|
|
|
|
|
|
def rfc2822_date(date):
|
2008-08-06 01:38:49 +08:00
|
|
|
# We do this ourselves to be timezone aware, email.Utils is not tz aware.
|
|
|
|
if date.tzinfo:
|
|
|
|
time_str = date.strftime('%a, %d %b %Y %H:%M:%S ')
|
|
|
|
offset = date.tzinfo.utcoffset(date)
|
|
|
|
timezone = (offset.days * 24 * 60) + (offset.seconds / 60)
|
|
|
|
hour, minute = divmod(timezone, 60)
|
|
|
|
return time_str + "%+03d%02d" % (hour, minute)
|
|
|
|
else:
|
|
|
|
return date.strftime('%a, %d %b %Y %H:%M:%S -0000')
|
2005-11-12 11:44:53 +08:00
|
|
|
|
2005-11-14 12:28:31 +08:00
|
|
|
def rfc3339_date(date):
|
2007-09-15 06:29:37 +08:00
|
|
|
if date.tzinfo:
|
2008-08-06 01:38:49 +08:00
|
|
|
time_str = date.strftime('%Y-%m-%dT%H:%M:%S')
|
|
|
|
offset = date.tzinfo.utcoffset(date)
|
|
|
|
timezone = (offset.days * 24 * 60) + (offset.seconds / 60)
|
|
|
|
hour, minute = divmod(timezone, 60)
|
|
|
|
return time_str + "%+03d:%02d" % (hour, minute)
|
2007-09-15 06:29:37 +08:00
|
|
|
else:
|
|
|
|
return date.strftime('%Y-%m-%dT%H:%M:%SZ')
|
2005-11-14 12:28:31 +08:00
|
|
|
|
2005-11-12 11:44:53 +08:00
|
|
|
def get_tag_uri(url, date):
|
2010-01-28 21:46:18 +08:00
|
|
|
"""
|
|
|
|
Creates a TagURI.
|
|
|
|
|
|
|
|
See http://diveintomark.org/archives/2004/05/28/howto-atom-id
|
|
|
|
"""
|
|
|
|
url_split = urlparse.urlparse(url)
|
2010-02-15 04:29:42 +08:00
|
|
|
|
|
|
|
# Python 2.4 didn't have named attributes on split results or the hostname.
|
|
|
|
hostname = getattr(url_split, 'hostname', url_split[1].split(':')[0])
|
|
|
|
path = url_split[2]
|
|
|
|
fragment = url_split[5]
|
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
d = ''
|
2005-11-12 11:44:53 +08:00
|
|
|
if date is not None:
|
2010-01-28 21:46:18 +08:00
|
|
|
d = ',%s' % date.strftime('%Y-%m-%d')
|
2010-02-15 04:29:42 +08:00
|
|
|
return u'tag:%s%s:%s/%s' % (hostname, d, path, fragment)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2006-06-08 13:00:13 +08:00
|
|
|
class SyndicationFeed(object):
|
2005-07-13 09:25:57 +08:00
|
|
|
"Base class for all syndication feeds. Subclasses should provide write()"
|
2005-11-12 11:44:53 +08:00
|
|
|
def __init__(self, title, link, description, language=None, author_email=None,
|
2005-11-14 12:59:20 +08:00
|
|
|
author_name=None, author_link=None, subtitle=None, categories=None,
|
2008-08-12 06:22:26 +08:00
|
|
|
feed_url=None, feed_copyright=None, feed_guid=None, ttl=None, **kwargs):
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
to_unicode = lambda s: force_unicode(s, strings_only=True)
|
|
|
|
if categories:
|
|
|
|
categories = [force_unicode(c) for c in categories]
|
2010-01-28 21:46:18 +08:00
|
|
|
if ttl is not None:
|
|
|
|
# Force ints to unicode
|
|
|
|
ttl = force_unicode(ttl)
|
2005-11-12 11:44:53 +08:00
|
|
|
self.feed = {
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
'title': to_unicode(title),
|
|
|
|
'link': iri_to_uri(link),
|
|
|
|
'description': to_unicode(description),
|
2008-02-03 11:23:10 +08:00
|
|
|
'language': to_unicode(language),
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
'author_email': to_unicode(author_email),
|
|
|
|
'author_name': to_unicode(author_name),
|
|
|
|
'author_link': iri_to_uri(author_link),
|
|
|
|
'subtitle': to_unicode(subtitle),
|
2005-11-12 11:44:53 +08:00
|
|
|
'categories': categories or (),
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
'feed_url': iri_to_uri(feed_url),
|
|
|
|
'feed_copyright': to_unicode(feed_copyright),
|
2007-07-10 20:33:55 +08:00
|
|
|
'id': feed_guid or link,
|
2007-10-20 22:54:38 +08:00
|
|
|
'ttl': ttl,
|
2005-07-13 09:25:57 +08:00
|
|
|
}
|
2008-08-12 06:22:26 +08:00
|
|
|
self.feed.update(kwargs)
|
2005-07-13 09:25:57 +08:00
|
|
|
self.items = []
|
|
|
|
|
|
|
|
def add_item(self, title, link, description, author_email=None,
|
2005-11-14 13:15:40 +08:00
|
|
|
author_name=None, author_link=None, pubdate=None, comments=None,
|
2008-08-16 06:13:50 +08:00
|
|
|
unique_id=None, enclosure=None, categories=(), item_copyright=None,
|
2008-08-12 06:22:26 +08:00
|
|
|
ttl=None, **kwargs):
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
|
|
|
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.
|
|
|
|
"""
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
to_unicode = lambda s: force_unicode(s, strings_only=True)
|
|
|
|
if categories:
|
|
|
|
categories = [to_unicode(c) for c in categories]
|
2010-01-28 21:46:18 +08:00
|
|
|
if ttl is not None:
|
|
|
|
# Force ints to unicode
|
|
|
|
ttl = force_unicode(ttl)
|
2008-08-12 06:22:26 +08:00
|
|
|
item = {
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
'title': to_unicode(title),
|
|
|
|
'link': iri_to_uri(link),
|
|
|
|
'description': to_unicode(description),
|
|
|
|
'author_email': to_unicode(author_email),
|
|
|
|
'author_name': to_unicode(author_name),
|
|
|
|
'author_link': iri_to_uri(author_link),
|
2005-07-13 09:25:57 +08:00
|
|
|
'pubdate': pubdate,
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
'comments': to_unicode(comments),
|
|
|
|
'unique_id': to_unicode(unique_id),
|
2005-07-13 09:25:57 +08:00
|
|
|
'enclosure': enclosure,
|
2005-11-12 11:44:53 +08:00
|
|
|
'categories': categories or (),
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
'item_copyright': to_unicode(item_copyright),
|
2007-10-20 22:54:38 +08:00
|
|
|
'ttl': ttl,
|
2008-08-12 06:22:26 +08:00
|
|
|
}
|
|
|
|
item.update(kwargs)
|
|
|
|
self.items.append(item)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def num_items(self):
|
|
|
|
return len(self.items)
|
|
|
|
|
2008-08-12 06:22:26 +08:00
|
|
|
def root_attributes(self):
|
|
|
|
"""
|
|
|
|
Return extra attributes to place on the root (i.e. feed/channel) element.
|
|
|
|
Called from write().
|
|
|
|
"""
|
|
|
|
return {}
|
2008-08-16 06:13:50 +08:00
|
|
|
|
2008-08-12 06:22:26 +08:00
|
|
|
def add_root_elements(self, handler):
|
|
|
|
"""
|
2009-05-18 00:45:28 +08:00
|
|
|
Add elements in the root (i.e. feed/channel) element. Called
|
2008-08-12 06:22:26 +08:00
|
|
|
from write().
|
|
|
|
"""
|
|
|
|
pass
|
2008-08-16 06:13:50 +08:00
|
|
|
|
2008-08-12 06:22:26 +08:00
|
|
|
def item_attributes(self, item):
|
|
|
|
"""
|
|
|
|
Return extra attributes to place on each item (i.e. item/entry) element.
|
|
|
|
"""
|
|
|
|
return {}
|
2008-08-16 06:13:50 +08:00
|
|
|
|
2008-08-12 06:22:26 +08:00
|
|
|
def add_item_elements(self, handler, item):
|
|
|
|
"""
|
|
|
|
Add elements on each item (i.e. item/entry) element.
|
|
|
|
"""
|
|
|
|
pass
|
2008-08-16 06:13:50 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
def write(self, outfile, encoding):
|
|
|
|
"""
|
|
|
|
Outputs the feed in the given encoding to outfile, which is a file-like
|
|
|
|
object. Subclasses should override this.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def writeString(self, encoding):
|
|
|
|
"""
|
|
|
|
Returns the feed in the given encoding as a string.
|
|
|
|
"""
|
|
|
|
from StringIO import StringIO
|
|
|
|
s = StringIO()
|
|
|
|
self.write(s, encoding)
|
|
|
|
return s.getvalue()
|
|
|
|
|
2005-11-12 11:44:53 +08:00
|
|
|
def latest_post_date(self):
|
|
|
|
"""
|
|
|
|
Returns the latest item's pubdate. If none of them have a pubdate,
|
|
|
|
this returns the current date/time.
|
|
|
|
"""
|
|
|
|
updates = [i['pubdate'] for i in self.items if i['pubdate'] is not None]
|
|
|
|
if len(updates) > 0:
|
|
|
|
updates.sort()
|
|
|
|
return updates[-1]
|
|
|
|
else:
|
|
|
|
return datetime.datetime.now()
|
|
|
|
|
2006-06-08 13:00:13 +08:00
|
|
|
class Enclosure(object):
|
2005-07-13 09:25:57 +08:00
|
|
|
"Represents an RSS enclosure"
|
|
|
|
def __init__(self, url, length, mime_type):
|
|
|
|
"All args are expected to be Python Unicode objects"
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
self.length, self.mime_type = length, mime_type
|
|
|
|
self.url = iri_to_uri(url)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
class RssFeed(SyndicationFeed):
|
2005-11-13 02:32:12 +08:00
|
|
|
mime_type = 'application/rss+xml'
|
2005-07-13 09:25:57 +08:00
|
|
|
def write(self, outfile, encoding):
|
|
|
|
handler = SimplerXMLGenerator(outfile, encoding)
|
|
|
|
handler.startDocument()
|
2008-08-13 06:12:14 +08:00
|
|
|
handler.startElement(u"rss", self.rss_attributes())
|
2008-08-12 06:22:26 +08:00
|
|
|
handler.startElement(u"channel", self.root_attributes())
|
|
|
|
self.add_root_elements(handler)
|
|
|
|
self.write_items(handler)
|
|
|
|
self.endChannelElement(handler)
|
|
|
|
handler.endElement(u"rss")
|
|
|
|
|
2008-08-13 06:12:14 +08:00
|
|
|
def rss_attributes(self):
|
2010-01-28 21:46:18 +08:00
|
|
|
return {u"version": self._version,
|
|
|
|
u"xmlns:atom": u"http://www.w3.org/2005/Atom"}
|
2008-08-13 06:12:14 +08:00
|
|
|
|
2008-08-12 06:22:26 +08:00
|
|
|
def write_items(self, handler):
|
|
|
|
for item in self.items:
|
|
|
|
handler.startElement(u'item', self.item_attributes(item))
|
|
|
|
self.add_item_elements(handler, item)
|
|
|
|
handler.endElement(u"item")
|
|
|
|
|
|
|
|
def add_root_elements(self, handler):
|
2005-11-12 11:44:53 +08:00
|
|
|
handler.addQuickElement(u"title", self.feed['title'])
|
|
|
|
handler.addQuickElement(u"link", self.feed['link'])
|
|
|
|
handler.addQuickElement(u"description", self.feed['description'])
|
2010-01-28 21:46:18 +08:00
|
|
|
handler.addQuickElement(u"atom:link", None, {u"rel": u"self", u"href": self.feed['feed_url']})
|
2005-11-12 11:44:53 +08:00
|
|
|
if self.feed['language'] is not None:
|
|
|
|
handler.addQuickElement(u"language", self.feed['language'])
|
2006-06-19 09:38:06 +08:00
|
|
|
for cat in self.feed['categories']:
|
|
|
|
handler.addQuickElement(u"category", cat)
|
2007-02-10 16:36:39 +08:00
|
|
|
if self.feed['feed_copyright'] is not None:
|
|
|
|
handler.addQuickElement(u"copyright", self.feed['feed_copyright'])
|
2008-08-16 06:13:50 +08:00
|
|
|
handler.addQuickElement(u"lastBuildDate", rfc2822_date(self.latest_post_date()).decode('utf-8'))
|
2007-10-20 22:54:38 +08:00
|
|
|
if self.feed['ttl'] is not None:
|
|
|
|
handler.addQuickElement(u"ttl", self.feed['ttl'])
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def endChannelElement(self, handler):
|
|
|
|
handler.endElement(u"channel")
|
|
|
|
|
|
|
|
class RssUserland091Feed(RssFeed):
|
2005-11-12 11:44:53 +08:00
|
|
|
_version = u"0.91"
|
2008-08-12 06:22:26 +08:00
|
|
|
def add_item_elements(self, handler, item):
|
|
|
|
handler.addQuickElement(u"title", item['title'])
|
|
|
|
handler.addQuickElement(u"link", item['link'])
|
|
|
|
if item['description'] is not None:
|
|
|
|
handler.addQuickElement(u"description", item['description'])
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
class Rss201rev2Feed(RssFeed):
|
|
|
|
# Spec: http://blogs.law.harvard.edu/tech/rss
|
2005-11-12 11:44:53 +08:00
|
|
|
_version = u"2.0"
|
2008-08-12 06:22:26 +08:00
|
|
|
def add_item_elements(self, handler, item):
|
|
|
|
handler.addQuickElement(u"title", item['title'])
|
|
|
|
handler.addQuickElement(u"link", item['link'])
|
|
|
|
if item['description'] is not None:
|
|
|
|
handler.addQuickElement(u"description", item['description'])
|
|
|
|
|
|
|
|
# Author information.
|
|
|
|
if item["author_name"] and item["author_email"]:
|
|
|
|
handler.addQuickElement(u"author", "%s (%s)" % \
|
|
|
|
(item['author_email'], item['author_name']))
|
|
|
|
elif item["author_email"]:
|
|
|
|
handler.addQuickElement(u"author", item["author_email"])
|
|
|
|
elif item["author_name"]:
|
2010-01-28 21:46:18 +08:00
|
|
|
handler.addQuickElement(u"dc:creator", item["author_name"], {u"xmlns:dc": u"http://purl.org/dc/elements/1.1/"})
|
2008-08-12 06:22:26 +08:00
|
|
|
|
|
|
|
if item['pubdate'] is not None:
|
2008-08-16 06:13:50 +08:00
|
|
|
handler.addQuickElement(u"pubDate", rfc2822_date(item['pubdate']).decode('utf-8'))
|
2008-08-12 06:22:26 +08:00
|
|
|
if item['comments'] is not None:
|
|
|
|
handler.addQuickElement(u"comments", item['comments'])
|
|
|
|
if item['unique_id'] is not None:
|
|
|
|
handler.addQuickElement(u"guid", item['unique_id'])
|
|
|
|
if item['ttl'] is not None:
|
|
|
|
handler.addQuickElement(u"ttl", item['ttl'])
|
|
|
|
|
|
|
|
# Enclosure.
|
|
|
|
if item['enclosure'] is not None:
|
|
|
|
handler.addQuickElement(u"enclosure", '',
|
|
|
|
{u"url": item['enclosure'].url, u"length": item['enclosure'].length,
|
|
|
|
u"type": item['enclosure'].mime_type})
|
|
|
|
|
|
|
|
# Categories.
|
|
|
|
for cat in item['categories']:
|
|
|
|
handler.addQuickElement(u"category", cat)
|
2005-11-12 11:44:53 +08:00
|
|
|
|
|
|
|
class Atom1Feed(SyndicationFeed):
|
|
|
|
# Spec: http://atompub.org/2005/07/11/draft-ietf-atompub-format-10.html
|
2005-11-13 02:32:12 +08:00
|
|
|
mime_type = 'application/atom+xml'
|
2005-11-12 11:44:53 +08:00
|
|
|
ns = u"http://www.w3.org/2005/Atom"
|
2008-08-12 06:22:26 +08:00
|
|
|
|
2005-11-12 11:44:53 +08:00
|
|
|
def write(self, outfile, encoding):
|
|
|
|
handler = SimplerXMLGenerator(outfile, encoding)
|
|
|
|
handler.startDocument()
|
2008-08-12 06:22:26 +08:00
|
|
|
handler.startElement(u'feed', self.root_attributes())
|
|
|
|
self.add_root_elements(handler)
|
|
|
|
self.write_items(handler)
|
|
|
|
handler.endElement(u"feed")
|
|
|
|
|
2008-08-13 06:12:14 +08:00
|
|
|
def root_attributes(self):
|
2005-11-12 11:44:53 +08:00
|
|
|
if self.feed['language'] is not None:
|
2008-08-12 06:22:26 +08:00
|
|
|
return {u"xmlns": self.ns, u"xml:lang": self.feed['language']}
|
2005-11-12 11:44:53 +08:00
|
|
|
else:
|
2008-08-12 06:22:26 +08:00
|
|
|
return {u"xmlns": self.ns}
|
|
|
|
|
|
|
|
def add_root_elements(self, handler):
|
2005-11-12 11:44:53 +08:00
|
|
|
handler.addQuickElement(u"title", self.feed['title'])
|
2005-11-14 12:59:20 +08:00
|
|
|
handler.addQuickElement(u"link", "", {u"rel": u"alternate", u"href": self.feed['link']})
|
|
|
|
if self.feed['feed_url'] is not None:
|
|
|
|
handler.addQuickElement(u"link", "", {u"rel": u"self", u"href": self.feed['feed_url']})
|
2007-07-10 20:33:55 +08:00
|
|
|
handler.addQuickElement(u"id", self.feed['id'])
|
2008-08-16 06:13:50 +08:00
|
|
|
handler.addQuickElement(u"updated", rfc3339_date(self.latest_post_date()).decode('utf-8'))
|
2005-11-12 11:44:53 +08:00
|
|
|
if self.feed['author_name'] is not None:
|
|
|
|
handler.startElement(u"author", {})
|
|
|
|
handler.addQuickElement(u"name", self.feed['author_name'])
|
|
|
|
if self.feed['author_email'] is not None:
|
|
|
|
handler.addQuickElement(u"email", self.feed['author_email'])
|
|
|
|
if self.feed['author_link'] is not None:
|
|
|
|
handler.addQuickElement(u"uri", self.feed['author_link'])
|
|
|
|
handler.endElement(u"author")
|
|
|
|
if self.feed['subtitle'] is not None:
|
|
|
|
handler.addQuickElement(u"subtitle", self.feed['subtitle'])
|
|
|
|
for cat in self.feed['categories']:
|
|
|
|
handler.addQuickElement(u"category", "", {u"term": cat})
|
2007-02-10 16:36:39 +08:00
|
|
|
if self.feed['feed_copyright'] is not None:
|
|
|
|
handler.addQuickElement(u"rights", self.feed['feed_copyright'])
|
2008-08-16 06:13:50 +08:00
|
|
|
|
2005-11-12 11:44:53 +08:00
|
|
|
def write_items(self, handler):
|
|
|
|
for item in self.items:
|
2008-08-12 06:22:26 +08:00
|
|
|
handler.startElement(u"entry", self.item_attributes(item))
|
|
|
|
self.add_item_elements(handler, item)
|
2005-11-12 11:44:53 +08:00
|
|
|
handler.endElement(u"entry")
|
2008-08-16 06:13:50 +08:00
|
|
|
|
2008-08-12 06:22:26 +08:00
|
|
|
def add_item_elements(self, handler, item):
|
|
|
|
handler.addQuickElement(u"title", item['title'])
|
|
|
|
handler.addQuickElement(u"link", u"", {u"href": item['link'], u"rel": u"alternate"})
|
|
|
|
if item['pubdate'] is not None:
|
2008-08-16 06:13:50 +08:00
|
|
|
handler.addQuickElement(u"updated", rfc3339_date(item['pubdate']).decode('utf-8'))
|
2008-08-12 06:22:26 +08:00
|
|
|
|
|
|
|
# Author information.
|
|
|
|
if item['author_name'] is not None:
|
|
|
|
handler.startElement(u"author", {})
|
|
|
|
handler.addQuickElement(u"name", item['author_name'])
|
|
|
|
if item['author_email'] is not None:
|
|
|
|
handler.addQuickElement(u"email", item['author_email'])
|
|
|
|
if item['author_link'] is not None:
|
|
|
|
handler.addQuickElement(u"uri", item['author_link'])
|
|
|
|
handler.endElement(u"author")
|
|
|
|
|
|
|
|
# Unique ID.
|
|
|
|
if item['unique_id'] is not None:
|
|
|
|
unique_id = item['unique_id']
|
|
|
|
else:
|
|
|
|
unique_id = get_tag_uri(item['link'], item['pubdate'])
|
|
|
|
handler.addQuickElement(u"id", unique_id)
|
|
|
|
|
|
|
|
# Summary.
|
|
|
|
if item['description'] is not None:
|
|
|
|
handler.addQuickElement(u"summary", item['description'], {u"type": u"html"})
|
|
|
|
|
|
|
|
# Enclosure.
|
|
|
|
if item['enclosure'] is not None:
|
|
|
|
handler.addQuickElement(u"link", '',
|
|
|
|
{u"rel": u"enclosure",
|
|
|
|
u"href": item['enclosure'].url,
|
|
|
|
u"length": item['enclosure'].length,
|
|
|
|
u"type": item['enclosure'].mime_type})
|
|
|
|
|
|
|
|
# Categories.
|
|
|
|
for cat in item['categories']:
|
|
|
|
handler.addQuickElement(u"category", u"", {u"term": cat})
|
|
|
|
|
|
|
|
# Rights.
|
|
|
|
if item['item_copyright'] is not None:
|
|
|
|
handler.addQuickElement(u"rights", item['item_copyright'])
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
# This isolates the decision of what the system default is, so calling code can
|
2005-11-12 11:44:53 +08:00
|
|
|
# do "feedgenerator.DefaultFeed" instead of "feedgenerator.Rss201rev2Feed".
|
|
|
|
DefaultFeed = Rss201rev2Feed
|