1 | # Copyright (c) 2005, the Lawrence Journal-World |
---|
2 | # All rights reserved. |
---|
3 | # |
---|
4 | # Redistribution and use in source and binary forms, with or without modification, |
---|
5 | # are permitted provided that the following conditions are met: |
---|
6 | # |
---|
7 | # 1. Redistributions of source code must retain the above copyright notice, |
---|
8 | # this list of conditions and the following disclaimer. |
---|
9 | # |
---|
10 | # 2. Redistributions in binary form must reproduce the above copyright |
---|
11 | # notice, this list of conditions and the following disclaimer in the |
---|
12 | # documentation and/or other materials provided with the distribution. |
---|
13 | # |
---|
14 | # 3. Neither the name of Django nor the names of its contributors may be used |
---|
15 | # to endorse or promote products derived from this software without |
---|
16 | # specific prior written permission. |
---|
17 | # |
---|
18 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
---|
19 | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
---|
20 | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
---|
21 | # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
---|
22 | # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
---|
23 | # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
---|
24 | # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
---|
25 | # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
26 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
27 | # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
28 | |
---|
29 | # LAST SYNCED WITH DJANGO SOURCE - JULY 12th, 2006 - DJANGO REVISION 3143 |
---|
30 | # http://code.djangoproject.com/log/django/trunk/django/utils/feedgenerator.py |
---|
31 | """Syndication feed generation library -- used for generating RSS, etc. |
---|
32 | |
---|
33 | Sample usage: |
---|
34 | |
---|
35 | >>> feed = feedgenerator.Rss201rev2Feed( |
---|
36 | ... title=u"Poynter E-Media Tidbits", |
---|
37 | ... link=u"http://www.poynter.org/column.asp?id=31", |
---|
38 | ... description=u"A group weblog by the sharpest minds in online media/journalism/publishing.", |
---|
39 | ... language=u"en", |
---|
40 | ... ) |
---|
41 | >>> feed.add_item(title="Hello", link=u"http://www.holovaty.com/test/", description="Testing.") |
---|
42 | >>> fp = open('test.rss', 'w') |
---|
43 | >>> feed.write(fp, 'utf-8') |
---|
44 | >>> fp.close() |
---|
45 | |
---|
46 | For definitions of the different versions of RSS, see: |
---|
47 | http://diveintomark.org/archives/2004/02/04/incompatible-rss |
---|
48 | |
---|
49 | """ |
---|
50 | |
---|
51 | from util import SimplerXMLGenerator |
---|
52 | import datetime, re, time |
---|
53 | import email.Utils |
---|
54 | from xml.dom import minidom |
---|
55 | from xml.parsers.expat import ExpatError |
---|
56 | |
---|
57 | def rfc2822_date(date): |
---|
58 | return email.Utils.formatdate(time.mktime(date.timetuple())) |
---|
59 | |
---|
60 | def rfc3339_date(date): |
---|
61 | return date.strftime('%Y-%m-%dT%H:%M:%SZ') |
---|
62 | |
---|
63 | def get_tag_uri(url, date): |
---|
64 | """Creates a TagURI. See http://diveintomark.org/archives/2004/05/28/howto-atom-id""" |
---|
65 | tag = re.sub('^http://', '', url) |
---|
66 | if date is not None: |
---|
67 | tag = re.sub('/', ',%s:/' % date.strftime('%Y-%m-%d'), tag, 1) |
---|
68 | tag = re.sub('#', '/', tag) |
---|
69 | return 'tag:' + tag |
---|
70 | |
---|
71 | class SyndicationFeed(object): |
---|
72 | """Base class for all syndication feeds. Subclasses should provide write()""" |
---|
73 | def __init__(self, title, link, description, language=None, author_email=None, |
---|
74 | author_name=None, author_link=None, subtitle=None, categories=None, |
---|
75 | feed_url=None): |
---|
76 | self.feed = { |
---|
77 | 'title': title, |
---|
78 | 'link': link, |
---|
79 | 'description': description, |
---|
80 | 'language': language, |
---|
81 | 'author_email': author_email, |
---|
82 | 'author_name': author_name, |
---|
83 | 'author_link': author_link, |
---|
84 | 'subtitle': subtitle, |
---|
85 | 'categories': categories or (), |
---|
86 | 'feed_url': feed_url, |
---|
87 | } |
---|
88 | self.items = [] |
---|
89 | |
---|
90 | def add_item(self, title, link, description, author_email=None, |
---|
91 | author_name=None, author_link=None, pubdate=None, comments=None, |
---|
92 | unique_id=None, enclosure=None, categories=()): |
---|
93 | """Adds an item to the feed. |
---|
94 | |
---|
95 | All args are expected to be Python Unicode |
---|
96 | objects except pubdate, which is a datetime.datetime object, and |
---|
97 | enclosure, which is an instance of the Enclosure class. |
---|
98 | |
---|
99 | """ |
---|
100 | self.items.append({ |
---|
101 | 'title': title, |
---|
102 | 'link': link, |
---|
103 | 'description': description, |
---|
104 | 'author_email': author_email, |
---|
105 | 'author_name': author_name, |
---|
106 | 'author_link': author_link, |
---|
107 | 'pubdate': pubdate, |
---|
108 | 'comments': comments, |
---|
109 | 'unique_id': unique_id, |
---|
110 | 'enclosure': enclosure, |
---|
111 | 'categories': categories or (), |
---|
112 | }) |
---|
113 | |
---|
114 | def num_items(self): |
---|
115 | return len(self.items) |
---|
116 | |
---|
117 | def write(self, outfile, encoding): |
---|
118 | """Outputs the feed in the given encoding to outfile, which is a file-like |
---|
119 | object. |
---|
120 | |
---|
121 | Subclasses should override this. |
---|
122 | |
---|
123 | """ |
---|
124 | raise NotImplementedError |
---|
125 | |
---|
126 | def writeString(self, encoding): |
---|
127 | """Returns the feed in the given encoding as a string.""" |
---|
128 | from StringIO import StringIO |
---|
129 | s = StringIO() |
---|
130 | self.write(s, encoding) |
---|
131 | return s.getvalue() |
---|
132 | |
---|
133 | def latest_post_date(self): |
---|
134 | """Returns the latest item's pubdate. |
---|
135 | |
---|
136 | If none of them have a pubdate, this returns the current date/time. |
---|
137 | |
---|
138 | """ |
---|
139 | updates = [i['pubdate'] for i in self.items if i['pubdate'] is not None] |
---|
140 | if len(updates) > 0: |
---|
141 | updates.sort() |
---|
142 | return updates[-1] |
---|
143 | else: |
---|
144 | return datetime.datetime.now() |
---|
145 | |
---|
146 | class Enclosure(object): |
---|
147 | """Represents an RSS enclosure""" |
---|
148 | def __init__(self, url, length, mime_type): |
---|
149 | "All args are expected to be Python Unicode objects" |
---|
150 | self.url, self.length, self.mime_type = url, length, mime_type |
---|
151 | |
---|
152 | class RssFeed(SyndicationFeed): |
---|
153 | mime_type = 'application/rss+xml' |
---|
154 | def write(self, outfile, encoding): |
---|
155 | handler = SimplerXMLGenerator(outfile, encoding) |
---|
156 | handler.startDocument() |
---|
157 | handler.startElement(u"rss", {u"version": self._version}) |
---|
158 | handler.startElement(u"channel", {}) |
---|
159 | handler.addQuickElement(u"title", self.feed['title']) |
---|
160 | handler.addQuickElement(u"link", self.feed['link']) |
---|
161 | handler.addQuickElement(u"description", self.feed['description']) |
---|
162 | if self.feed['language'] is not None: |
---|
163 | handler.addQuickElement(u"language", self.feed['language']) |
---|
164 | for cat in self.feed['categories']: |
---|
165 | handler.addQuickElement(u"category", cat) |
---|
166 | self.write_items(handler) |
---|
167 | self.endChannelElement(handler) |
---|
168 | handler.endElement(u"rss") |
---|
169 | |
---|
170 | def endChannelElement(self, handler): |
---|
171 | handler.endElement(u"channel") |
---|
172 | |
---|
173 | class RssUserland091Feed(RssFeed): |
---|
174 | _version = u"0.91" |
---|
175 | def write_items(self, handler): |
---|
176 | for item in self.items: |
---|
177 | handler.startElement(u"item", {}) |
---|
178 | handler.addQuickElement(u"title", item['title']) |
---|
179 | handler.addQuickElement(u"link", item['link']) |
---|
180 | if item['description'] is not None: |
---|
181 | handler.addQuickElement(u"description", item['description']) |
---|
182 | handler.endElement(u"item") |
---|
183 | |
---|
184 | class Rss201rev2Feed(RssFeed): |
---|
185 | # Spec: http://blogs.law.harvard.edu/tech/rss |
---|
186 | _version = u"2.0" |
---|
187 | def write_items(self, handler): |
---|
188 | for item in self.items: |
---|
189 | handler.startElement(u"item", {}) |
---|
190 | handler.addQuickElement(u"title", item['title']) |
---|
191 | handler.addQuickElement(u"link", item['link']) |
---|
192 | if item['description'] is not None: |
---|
193 | handler.addQuickElement(u"description", item['description']) |
---|
194 | |
---|
195 | # Author information. |
---|
196 | if item["author_name"] and item["author_email"]: |
---|
197 | handler.addQuickElement(u"author", "%s (%s)" % \ |
---|
198 | (item['author_email'], item['author_name'])) |
---|
199 | elif item["author_email"]: |
---|
200 | handler.addQuickElement(u"author", item["author_email"]) |
---|
201 | |
---|
202 | if item['pubdate'] is not None: |
---|
203 | handler.addQuickElement(u"pubDate", rfc2822_date(item['pubdate']).decode('ascii')) |
---|
204 | if item['comments'] is not None: |
---|
205 | handler.addQuickElement(u"comments", item['comments']) |
---|
206 | if item['unique_id'] is not None: |
---|
207 | handler.addQuickElement(u"guid", item['unique_id']) |
---|
208 | |
---|
209 | # Enclosure. |
---|
210 | if item['enclosure'] is not None: |
---|
211 | handler.addQuickElement(u"enclosure", '', |
---|
212 | {u"url": item['enclosure'].url, u"length": item['enclosure'].length, |
---|
213 | u"type": item['enclosure'].mime_type}) |
---|
214 | |
---|
215 | # Categories. |
---|
216 | for cat in item['categories']: |
---|
217 | handler.addQuickElement(u"category", cat) |
---|
218 | |
---|
219 | handler.endElement(u"item") |
---|
220 | |
---|
221 | class Atom1Feed(SyndicationFeed): |
---|
222 | # Spec: http://atompub.org/2005/07/11/draft-ietf-atompub-format-10.html |
---|
223 | mime_type = 'application/atom+xml' |
---|
224 | ns = u"http://www.w3.org/2005/Atom" |
---|
225 | def write(self, outfile, encoding): |
---|
226 | handler = SimplerXMLGenerator(outfile, encoding) |
---|
227 | handler.startDocument() |
---|
228 | if self.feed['language'] is not None: |
---|
229 | handler.startElement(u"feed", {u"xmlns": self.ns, u"xml:lang": self.feed['language']}) |
---|
230 | else: |
---|
231 | handler.startElement(u"feed", {u"xmlns": self.ns}) |
---|
232 | handler.addQuickElement(u"title", self.feed['title']) |
---|
233 | handler.addQuickElement(u"link", "", {u"rel": u"alternate", u"href": self.feed['link']}) |
---|
234 | if self.feed['feed_url'] is not None: |
---|
235 | handler.addQuickElement(u"link", "", {u"rel": u"self", u"href": self.feed['feed_url']}) |
---|
236 | handler.addQuickElement(u"id", self.feed['link']) |
---|
237 | handler.addQuickElement(u"updated", rfc3339_date(self.latest_post_date()).decode('ascii')) |
---|
238 | if self.feed['author_name'] is not None: |
---|
239 | handler.startElement(u"author", {}) |
---|
240 | handler.addQuickElement(u"name", self.feed['author_name']) |
---|
241 | if self.feed['author_email'] is not None: |
---|
242 | handler.addQuickElement(u"email", self.feed['author_email']) |
---|
243 | if self.feed['author_link'] is not None: |
---|
244 | handler.addQuickElement(u"uri", self.feed['author_link']) |
---|
245 | handler.endElement(u"author") |
---|
246 | if self.feed['subtitle'] is not None: |
---|
247 | handler.addQuickElement(u"subtitle", self.feed['subtitle']) |
---|
248 | for cat in self.feed['categories']: |
---|
249 | handler.addQuickElement(u"category", "", {u"term": cat}) |
---|
250 | self.write_items(handler) |
---|
251 | handler.endElement(u"feed") |
---|
252 | |
---|
253 | def write_items(self, handler): |
---|
254 | for item in self.items: |
---|
255 | handler.startElement(u"entry", {}) |
---|
256 | handler.addQuickElement(u"title", item['title']) |
---|
257 | handler.addQuickElement(u"link", u"", {u"href": item['link'], u"rel": u"alternate"}) |
---|
258 | if item['pubdate'] is not None: |
---|
259 | handler.addQuickElement(u"updated", rfc3339_date(item['pubdate']).decode('ascii')) |
---|
260 | |
---|
261 | # Author information. |
---|
262 | if item['author_name'] is not None: |
---|
263 | handler.startElement(u"author", {}) |
---|
264 | handler.addQuickElement(u"name", item['author_name']) |
---|
265 | if item['author_email'] is not None: |
---|
266 | handler.addQuickElement(u"email", item['author_email']) |
---|
267 | if item['author_link'] is not None: |
---|
268 | handler.addQuickElement(u"uri", item['author_link']) |
---|
269 | handler.endElement(u"author") |
---|
270 | |
---|
271 | # Unique ID. |
---|
272 | if item['unique_id'] is not None: |
---|
273 | unique_id = item['unique_id'] |
---|
274 | else: |
---|
275 | unique_id = get_tag_uri(item['link'], item['pubdate']) |
---|
276 | handler.addQuickElement(u"id", unique_id) |
---|
277 | |
---|
278 | # Summary. |
---|
279 | if item['description'] is not None: |
---|
280 | handler.addQuickElement(u"summary", item['description'], {u"type": u"html"}) |
---|
281 | |
---|
282 | # Enclosure. |
---|
283 | if item['enclosure'] is not None: |
---|
284 | handler.addQuickElement(u"link", '', |
---|
285 | {u"rel": u"enclosure", |
---|
286 | u"href": item['enclosure'].url, |
---|
287 | u"length": item['enclosure'].length, |
---|
288 | u"type": item['enclosure'].mime_type}) |
---|
289 | |
---|
290 | # Categories: |
---|
291 | for cat in item['categories']: |
---|
292 | handler.addQuickElement(u"category", u"", {u"term": cat}) |
---|
293 | |
---|
294 | handler.endElement(u"entry") |
---|
295 | |
---|
296 | # This isolates the decision of what the system default is, so calling code can |
---|
297 | # do "feedgenerator.DefaultFeed" instead of "feedgenerator.Rss201rev2Feed". |
---|
298 | DefaultFeed = Rss201rev2Feed |
---|