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(
|
2012-06-08 00:08:47 +08:00
|
|
|
... title="Poynter E-Media Tidbits",
|
|
|
|
... link="http://www.poynter.org/column.asp?id=31",
|
|
|
|
... description="A group Weblog by the sharpest minds in online media/journalism/publishing.",
|
|
|
|
... language="en",
|
2005-07-13 09:25:57 +08:00
|
|
|
... )
|
2010-05-09 05:38:27 +08:00
|
|
|
>>> feed.add_item(
|
|
|
|
... title="Hello",
|
2012-06-08 00:08:47 +08:00
|
|
|
... link="http://www.holovaty.com/test/",
|
2010-05-09 05:38:27 +08:00
|
|
|
... description="Testing."
|
|
|
|
... )
|
2012-05-05 20:01:38 +08:00
|
|
|
>>> with open('test.rss', 'w') as fp:
|
2012-05-05 22:54:30 +08:00
|
|
|
... feed.write(fp, 'utf-8')
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
For definitions of the different versions of RSS, see:
|
2012-07-14 18:32:05 +08:00
|
|
|
http://web.archive.org/web/20110718035220/http://diveintomark.org/archives/2004/02/04/incompatible-rss
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2008-08-12 06:22:26 +08:00
|
|
|
import datetime
|
2005-07-13 09:25:57 +08:00
|
|
|
from django.utils.xmlutils import SimplerXMLGenerator
|
2012-07-21 16:00:10 +08:00
|
|
|
from django.utils.encoding import force_text, iri_to_uri
|
2011-02-13 03:11:57 +08:00
|
|
|
from django.utils import datetime_safe
|
2012-08-04 17:05:13 +08:00
|
|
|
from django.utils import six
|
|
|
|
from django.utils.six import StringIO
|
2013-09-06 03:38:59 +08:00
|
|
|
from django.utils.six.moves.urllib.parse import urlparse
|
2011-11-18 21:01:06 +08:00
|
|
|
from django.utils.timezone import is_aware
|
2005-11-12 11:44:53 +08:00
|
|
|
|
|
|
|
def rfc2822_date(date):
|
2010-12-30 05:23:21 +08:00
|
|
|
# We can't use strftime() because it produces locale-dependant results, so
|
|
|
|
# we have to map english month and day names manually
|
|
|
|
months = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec',)
|
|
|
|
days = ('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun')
|
2011-02-13 03:11:57 +08:00
|
|
|
# Support datetime objects older than 1900
|
|
|
|
date = datetime_safe.new_datetime(date)
|
2008-08-06 01:38:49 +08:00
|
|
|
# We do this ourselves to be timezone aware, email.Utils is not tz aware.
|
2010-12-30 05:23:21 +08:00
|
|
|
dow = days[date.weekday()]
|
|
|
|
month = months[date.month - 1]
|
|
|
|
time_str = date.strftime('%s, %%d %s %%Y %%H:%%M:%%S ' % (dow, month))
|
2013-09-02 18:06:32 +08:00
|
|
|
if six.PY2: # strftime returns a byte string in Python 2
|
2012-08-04 17:05:13 +08:00
|
|
|
time_str = time_str.decode('utf-8')
|
2011-11-18 21:01:06 +08:00
|
|
|
if is_aware(date):
|
2008-08-06 01:38:49 +08:00
|
|
|
offset = date.tzinfo.utcoffset(date)
|
2011-09-10 05:45:58 +08:00
|
|
|
timezone = (offset.days * 24 * 60) + (offset.seconds // 60)
|
2008-08-06 01:38:49 +08:00
|
|
|
hour, minute = divmod(timezone, 60)
|
2012-08-04 17:05:13 +08:00
|
|
|
return time_str + '%+03d%02d' % (hour, minute)
|
2008-08-06 01:38:49 +08:00
|
|
|
else:
|
2010-12-30 05:23:21 +08:00
|
|
|
return time_str + '-0000'
|
2005-11-12 11:44:53 +08:00
|
|
|
|
2005-11-14 12:28:31 +08:00
|
|
|
def rfc3339_date(date):
|
2011-02-13 03:11:57 +08:00
|
|
|
# Support datetime objects older than 1900
|
|
|
|
date = datetime_safe.new_datetime(date)
|
2012-08-04 17:05:13 +08:00
|
|
|
time_str = date.strftime('%Y-%m-%dT%H:%M:%S')
|
2013-09-02 18:06:32 +08:00
|
|
|
if six.PY2: # strftime returns a byte string in Python 2
|
2012-08-04 17:05:13 +08:00
|
|
|
time_str = time_str.decode('utf-8')
|
2011-11-18 21:01:06 +08:00
|
|
|
if is_aware(date):
|
2008-08-06 01:38:49 +08:00
|
|
|
offset = date.tzinfo.utcoffset(date)
|
2011-09-10 05:45:58 +08:00
|
|
|
timezone = (offset.days * 24 * 60) + (offset.seconds // 60)
|
2008-08-06 01:38:49 +08:00
|
|
|
hour, minute = divmod(timezone, 60)
|
2012-08-04 17:05:13 +08:00
|
|
|
return time_str + '%+03d:%02d' % (hour, minute)
|
2007-09-15 06:29:37 +08:00
|
|
|
else:
|
2012-08-04 17:05:13 +08:00
|
|
|
return time_str + 'Z'
|
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.
|
|
|
|
|
2012-07-14 18:32:05 +08:00
|
|
|
See http://web.archive.org/web/20110514113830/http://diveintomark.org/archives/2004/05/28/howto-atom-id
|
2010-01-28 21:46:18 +08:00
|
|
|
"""
|
2012-07-20 21:36:52 +08:00
|
|
|
bits = urlparse(url)
|
2010-01-28 21:46:18 +08:00
|
|
|
d = ''
|
2005-11-12 11:44:53 +08:00
|
|
|
if date is not None:
|
2011-02-13 03:11:57 +08:00
|
|
|
d = ',%s' % datetime_safe.new_datetime(date).strftime('%Y-%m-%d')
|
2012-06-08 00:08:47 +08:00
|
|
|
return 'tag:%s%s:%s/%s' % (bits.hostname, d, bits.path, bits.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):
|
2012-07-21 16:00:10 +08:00
|
|
|
to_unicode = lambda s: force_text(s, strings_only=True)
|
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
|
|
|
if categories:
|
2012-07-21 16:00:10 +08:00
|
|
|
categories = [force_text(c) for c in categories]
|
2010-01-28 21:46:18 +08:00
|
|
|
if ttl is not None:
|
|
|
|
# Force ints to unicode
|
2012-07-21 16:00:10 +08:00
|
|
|
ttl = force_text(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,
|
2013-02-06 18:25:35 +08:00
|
|
|
unique_id=None, unique_id_is_permalink=None, enclosure=None,
|
2013-07-18 03:20:20 +08:00
|
|
|
categories=(), item_copyright=None, ttl=None, updateddate=None, **kwargs):
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
|
|
|
Adds an item to the feed. All args are expected to be Python Unicode
|
2013-07-18 03:20:20 +08:00
|
|
|
objects except pubdate and updateddate, which are datetime.datetime
|
|
|
|
objects, and enclosure, which is an instance of the Enclosure class.
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
2012-07-21 16:00:10 +08:00
|
|
|
to_unicode = lambda s: force_text(s, strings_only=True)
|
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
|
|
|
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
|
2012-07-21 16:00:10 +08:00
|
|
|
ttl = force_text(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,
|
2013-07-18 03:20:20 +08:00
|
|
|
'updateddate': updateddate,
|
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),
|
2013-02-06 18:25:35 +08:00
|
|
|
'unique_id_is_permalink': unique_id_is_permalink,
|
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.
|
|
|
|
"""
|
2013-09-07 02:24:52 +08:00
|
|
|
raise NotImplementedError('subclasses of SyndicationFeed must provide a write() method')
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def writeString(self, encoding):
|
|
|
|
"""
|
|
|
|
Returns the feed in the given encoding as a string.
|
|
|
|
"""
|
2012-08-04 17:05:13 +08:00
|
|
|
s = StringIO()
|
2005-07-13 09:25:57 +08:00
|
|
|
self.write(s, encoding)
|
|
|
|
return s.getvalue()
|
|
|
|
|
2005-11-12 11:44:53 +08:00
|
|
|
def latest_post_date(self):
|
|
|
|
"""
|
2013-07-18 03:20:20 +08:00
|
|
|
Returns the latest item's pubdate or updateddate. If no items
|
|
|
|
have either of these attributes this returns the current date/time.
|
2005-11-12 11:44:53 +08:00
|
|
|
"""
|
2013-07-18 03:20:20 +08:00
|
|
|
latest_date = None
|
|
|
|
date_keys = ('updateddate', 'pubdate')
|
|
|
|
|
|
|
|
for item in self.items:
|
|
|
|
for date_key in date_keys:
|
|
|
|
item_date = item.get(date_key)
|
|
|
|
if item_date:
|
|
|
|
if latest_date is None or item_date > latest_date:
|
|
|
|
latest_date = item_date
|
|
|
|
|
|
|
|
return latest_date or datetime.datetime.now()
|
2005-11-12 11:44:53 +08:00
|
|
|
|
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):
|
2012-02-11 07:45:22 +08:00
|
|
|
mime_type = 'application/rss+xml; charset=utf-8'
|
2005-07-13 09:25:57 +08:00
|
|
|
def write(self, outfile, encoding):
|
|
|
|
handler = SimplerXMLGenerator(outfile, encoding)
|
|
|
|
handler.startDocument()
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.startElement("rss", self.rss_attributes())
|
|
|
|
handler.startElement("channel", self.root_attributes())
|
2008-08-12 06:22:26 +08:00
|
|
|
self.add_root_elements(handler)
|
|
|
|
self.write_items(handler)
|
|
|
|
self.endChannelElement(handler)
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.endElement("rss")
|
2008-08-12 06:22:26 +08:00
|
|
|
|
2008-08-13 06:12:14 +08:00
|
|
|
def rss_attributes(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
return {"version": self._version,
|
|
|
|
"xmlns:atom": "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:
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.startElement('item', self.item_attributes(item))
|
2008-08-12 06:22:26 +08:00
|
|
|
self.add_item_elements(handler, item)
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.endElement("item")
|
2008-08-12 06:22:26 +08:00
|
|
|
|
|
|
|
def add_root_elements(self, handler):
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.addQuickElement("title", self.feed['title'])
|
|
|
|
handler.addQuickElement("link", self.feed['link'])
|
|
|
|
handler.addQuickElement("description", self.feed['description'])
|
2011-10-23 23:19:09 +08:00
|
|
|
if self.feed['feed_url'] is not None:
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.addQuickElement("atom:link", None,
|
|
|
|
{"rel": "self", "href": self.feed['feed_url']})
|
2005-11-12 11:44:53 +08:00
|
|
|
if self.feed['language'] is not None:
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.addQuickElement("language", self.feed['language'])
|
2006-06-19 09:38:06 +08:00
|
|
|
for cat in self.feed['categories']:
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.addQuickElement("category", cat)
|
2007-02-10 16:36:39 +08:00
|
|
|
if self.feed['feed_copyright'] is not None:
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.addQuickElement("copyright", self.feed['feed_copyright'])
|
2012-08-04 17:05:13 +08:00
|
|
|
handler.addQuickElement("lastBuildDate", rfc2822_date(self.latest_post_date()))
|
2007-10-20 22:54:38 +08:00
|
|
|
if self.feed['ttl'] is not None:
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.addQuickElement("ttl", self.feed['ttl'])
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def endChannelElement(self, handler):
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.endElement("channel")
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
class RssUserland091Feed(RssFeed):
|
2012-06-08 00:08:47 +08:00
|
|
|
_version = "0.91"
|
2008-08-12 06:22:26 +08:00
|
|
|
def add_item_elements(self, handler, item):
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.addQuickElement("title", item['title'])
|
|
|
|
handler.addQuickElement("link", item['link'])
|
2008-08-12 06:22:26 +08:00
|
|
|
if item['description'] is not None:
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.addQuickElement("description", item['description'])
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
class Rss201rev2Feed(RssFeed):
|
|
|
|
# Spec: http://blogs.law.harvard.edu/tech/rss
|
2012-06-08 00:08:47 +08:00
|
|
|
_version = "2.0"
|
2008-08-12 06:22:26 +08:00
|
|
|
def add_item_elements(self, handler, item):
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.addQuickElement("title", item['title'])
|
|
|
|
handler.addQuickElement("link", item['link'])
|
2008-08-12 06:22:26 +08:00
|
|
|
if item['description'] is not None:
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.addQuickElement("description", item['description'])
|
2008-08-12 06:22:26 +08:00
|
|
|
|
|
|
|
# Author information.
|
|
|
|
if item["author_name"] and item["author_email"]:
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.addQuickElement("author", "%s (%s)" % \
|
2008-08-12 06:22:26 +08:00
|
|
|
(item['author_email'], item['author_name']))
|
|
|
|
elif item["author_email"]:
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.addQuickElement("author", item["author_email"])
|
2008-08-12 06:22:26 +08:00
|
|
|
elif item["author_name"]:
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.addQuickElement("dc:creator", item["author_name"], {"xmlns:dc": "http://purl.org/dc/elements/1.1/"})
|
2008-08-12 06:22:26 +08:00
|
|
|
|
|
|
|
if item['pubdate'] is not None:
|
2012-08-04 17:05:13 +08:00
|
|
|
handler.addQuickElement("pubDate", rfc2822_date(item['pubdate']))
|
2008-08-12 06:22:26 +08:00
|
|
|
if item['comments'] is not None:
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.addQuickElement("comments", item['comments'])
|
2008-08-12 06:22:26 +08:00
|
|
|
if item['unique_id'] is not None:
|
2013-02-06 18:25:35 +08:00
|
|
|
guid_attrs = {}
|
|
|
|
if isinstance(item.get('unique_id_is_permalink'), bool):
|
|
|
|
guid_attrs['isPermaLink'] = str(
|
|
|
|
item['unique_id_is_permalink']).lower()
|
|
|
|
handler.addQuickElement("guid", item['unique_id'], guid_attrs)
|
2008-08-12 06:22:26 +08:00
|
|
|
if item['ttl'] is not None:
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.addQuickElement("ttl", item['ttl'])
|
2008-08-12 06:22:26 +08:00
|
|
|
|
|
|
|
# Enclosure.
|
|
|
|
if item['enclosure'] is not None:
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.addQuickElement("enclosure", '',
|
|
|
|
{"url": item['enclosure'].url, "length": item['enclosure'].length,
|
|
|
|
"type": item['enclosure'].mime_type})
|
2008-08-12 06:22:26 +08:00
|
|
|
|
|
|
|
# Categories.
|
|
|
|
for cat in item['categories']:
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.addQuickElement("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
|
2011-09-10 02:34:33 +08:00
|
|
|
mime_type = 'application/atom+xml; charset=utf-8'
|
2012-06-08 00:08:47 +08:00
|
|
|
ns = "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()
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.startElement('feed', self.root_attributes())
|
2008-08-12 06:22:26 +08:00
|
|
|
self.add_root_elements(handler)
|
|
|
|
self.write_items(handler)
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.endElement("feed")
|
2008-08-12 06:22:26 +08:00
|
|
|
|
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:
|
2012-06-08 00:08:47 +08:00
|
|
|
return {"xmlns": self.ns, "xml:lang": self.feed['language']}
|
2005-11-12 11:44:53 +08:00
|
|
|
else:
|
2012-06-08 00:08:47 +08:00
|
|
|
return {"xmlns": self.ns}
|
2008-08-12 06:22:26 +08:00
|
|
|
|
|
|
|
def add_root_elements(self, handler):
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.addQuickElement("title", self.feed['title'])
|
|
|
|
handler.addQuickElement("link", "", {"rel": "alternate", "href": self.feed['link']})
|
2005-11-14 12:59:20 +08:00
|
|
|
if self.feed['feed_url'] is not None:
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.addQuickElement("link", "", {"rel": "self", "href": self.feed['feed_url']})
|
|
|
|
handler.addQuickElement("id", self.feed['id'])
|
2012-08-04 17:05:13 +08:00
|
|
|
handler.addQuickElement("updated", rfc3339_date(self.latest_post_date()))
|
2005-11-12 11:44:53 +08:00
|
|
|
if self.feed['author_name'] is not None:
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.startElement("author", {})
|
|
|
|
handler.addQuickElement("name", self.feed['author_name'])
|
2005-11-12 11:44:53 +08:00
|
|
|
if self.feed['author_email'] is not None:
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.addQuickElement("email", self.feed['author_email'])
|
2005-11-12 11:44:53 +08:00
|
|
|
if self.feed['author_link'] is not None:
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.addQuickElement("uri", self.feed['author_link'])
|
|
|
|
handler.endElement("author")
|
2005-11-12 11:44:53 +08:00
|
|
|
if self.feed['subtitle'] is not None:
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.addQuickElement("subtitle", self.feed['subtitle'])
|
2005-11-12 11:44:53 +08:00
|
|
|
for cat in self.feed['categories']:
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.addQuickElement("category", "", {"term": cat})
|
2007-02-10 16:36:39 +08:00
|
|
|
if self.feed['feed_copyright'] is not None:
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.addQuickElement("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:
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.startElement("entry", self.item_attributes(item))
|
2008-08-12 06:22:26 +08:00
|
|
|
self.add_item_elements(handler, item)
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.endElement("entry")
|
2008-08-16 06:13:50 +08:00
|
|
|
|
2008-08-12 06:22:26 +08:00
|
|
|
def add_item_elements(self, handler, item):
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.addQuickElement("title", item['title'])
|
|
|
|
handler.addQuickElement("link", "", {"href": item['link'], "rel": "alternate"})
|
2013-07-18 03:20:20 +08:00
|
|
|
|
2008-08-12 06:22:26 +08:00
|
|
|
if item['pubdate'] is not None:
|
2013-07-18 03:20:20 +08:00
|
|
|
handler.addQuickElement('published', rfc3339_date(item['pubdate']))
|
|
|
|
|
|
|
|
if item['updateddate'] is not None:
|
|
|
|
handler.addQuickElement('updated', rfc3339_date(item['updateddate']))
|
2008-08-12 06:22:26 +08:00
|
|
|
|
|
|
|
# Author information.
|
|
|
|
if item['author_name'] is not None:
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.startElement("author", {})
|
|
|
|
handler.addQuickElement("name", item['author_name'])
|
2008-08-12 06:22:26 +08:00
|
|
|
if item['author_email'] is not None:
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.addQuickElement("email", item['author_email'])
|
2008-08-12 06:22:26 +08:00
|
|
|
if item['author_link'] is not None:
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.addQuickElement("uri", item['author_link'])
|
|
|
|
handler.endElement("author")
|
2008-08-12 06:22:26 +08:00
|
|
|
|
|
|
|
# Unique ID.
|
|
|
|
if item['unique_id'] is not None:
|
|
|
|
unique_id = item['unique_id']
|
|
|
|
else:
|
|
|
|
unique_id = get_tag_uri(item['link'], item['pubdate'])
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.addQuickElement("id", unique_id)
|
2008-08-12 06:22:26 +08:00
|
|
|
|
|
|
|
# Summary.
|
|
|
|
if item['description'] is not None:
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.addQuickElement("summary", item['description'], {"type": "html"})
|
2008-08-12 06:22:26 +08:00
|
|
|
|
|
|
|
# Enclosure.
|
|
|
|
if item['enclosure'] is not None:
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.addQuickElement("link", '',
|
|
|
|
{"rel": "enclosure",
|
|
|
|
"href": item['enclosure'].url,
|
|
|
|
"length": item['enclosure'].length,
|
|
|
|
"type": item['enclosure'].mime_type})
|
2008-08-12 06:22:26 +08:00
|
|
|
|
|
|
|
# Categories.
|
|
|
|
for cat in item['categories']:
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.addQuickElement("category", "", {"term": cat})
|
2008-08-12 06:22:26 +08:00
|
|
|
|
|
|
|
# Rights.
|
|
|
|
if item['item_copyright'] is not None:
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.addQuickElement("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
|