| 1 | from django.utils.feedgenerator import rfc2822_date, SyndicationFeed
|
|---|
| 2 | from django.utils.xmlutils import SimplerXMLGenerator
|
|---|
| 3 |
|
|---|
| 4 | class WellFormedWebRss(SyndicationFeed):
|
|---|
| 5 | mime_type = 'application/rss+xml'
|
|---|
| 6 | def write(self, outfile, encoding):
|
|---|
| 7 | handler = SimplerXMLGenerator(outfile, encoding)
|
|---|
| 8 | handler.startDocument()
|
|---|
| 9 | handler.startElement(u"rss", {u"version": self._version, u"xmlns:wfw": u"http://wellformedweb.org/CommentAPI/"})
|
|---|
| 10 | handler.startElement(u"channel", {})
|
|---|
| 11 | handler.addQuickElement(u"title", self.feed['title'])
|
|---|
| 12 | handler.addQuickElement(u"link", self.feed['link'])
|
|---|
| 13 | handler.addQuickElement(u"description", self.feed['description'])
|
|---|
| 14 | if self.feed['language'] is not None:
|
|---|
| 15 | handler.addQuickElement(u"language", self.feed['language'])
|
|---|
| 16 | for cat in self.feed['categories']:
|
|---|
| 17 | handler.addQuickElement(u"category", cat)
|
|---|
| 18 | if self.feed['feed_copyright'] is not None:
|
|---|
| 19 | handler.addQuickElement(u"copyright", self.feed['feed_copyright'])
|
|---|
| 20 | handler.addQuickElement(u"lastBuildDate", rfc2822_date(self.latest_post_date()).decode('ascii'))
|
|---|
| 21 | self.write_items(handler)
|
|---|
| 22 | self.endChannelElement(handler)
|
|---|
| 23 | handler.endElement(u"rss")
|
|---|
| 24 |
|
|---|
| 25 | def endChannelElement(self, handler):
|
|---|
| 26 | handler.endElement(u"channel")
|
|---|
| 27 |
|
|---|
| 28 | _version = u"2.0"
|
|---|
| 29 | def write_items(self, handler):
|
|---|
| 30 | for item in self.items:
|
|---|
| 31 | handler.startElement(u"item", {})
|
|---|
| 32 | handler.addQuickElement(u"title", item['title'])
|
|---|
| 33 | handler.addQuickElement(u"link", item['link'])
|
|---|
| 34 | if item['description'] is not None:
|
|---|
| 35 | handler.addQuickElement(u"description", item['description'])
|
|---|
| 36 |
|
|---|
| 37 | # Author information.
|
|---|
| 38 | if item["author_name"] and item["author_email"]:
|
|---|
| 39 | handler.addQuickElement(u"author", "%s (%s)" % \
|
|---|
| 40 | (item['author_email'], item['author_name']))
|
|---|
| 41 | elif item["author_email"]:
|
|---|
| 42 | handler.addQuickElement(u"author", item["author_email"])
|
|---|
| 43 | elif item["author_name"]:
|
|---|
| 44 | handler.addQuickElement(u"dc:creator", item["author_name"], {"xmlns:dc": u"http://purl.org/dc/elements/1.1/"})
|
|---|
| 45 |
|
|---|
| 46 | if item['pubdate'] is not None:
|
|---|
| 47 | handler.addQuickElement(u"pubDate", rfc2822_date(item['pubdate']).decode('ascii'))
|
|---|
| 48 | if item['comments'] is not None:
|
|---|
| 49 | handler.addQuickElement(u"comments", item['comments'])
|
|---|
| 50 | if item['wfw_commentRss'] is not None:
|
|---|
| 51 | handler.addQuickElement(u"wfw:commentRss", item['wfw_commentRss'])
|
|---|
| 52 | if item['unique_id'] is not None:
|
|---|
| 53 | handler.addQuickElement(u"guid", item['unique_id'])
|
|---|
| 54 |
|
|---|
| 55 | # Enclosure.
|
|---|
| 56 | if item['enclosure'] is not None:
|
|---|
| 57 | handler.addQuickElement(u"enclosure", '',
|
|---|
| 58 | {u"url": item['enclosure'].url, u"length": item['enclosure'].length,
|
|---|
| 59 | u"type": item['enclosure'].mime_type})
|
|---|
| 60 |
|
|---|
| 61 | # Categories.
|
|---|
| 62 | for cat in item['categories']:
|
|---|
| 63 | handler.addQuickElement(u"category", cat)
|
|---|
| 64 |
|
|---|
| 65 | handler.endElement(u"item")
|
|---|
| 66 |
|
|---|
| 67 | def add_commentRss(self, c):
|
|---|
| 68 | self.items[-1]['wfw_commentRss']=c
|
|---|