root/trunk/plugins/Rss.py

Revision 188 (checked in by llimllib, 1 year ago)

updated Rss module to use cb_feed_story protocol

Line 
1 import re, os, time
2 import cherrypy as cpy
3 from urllib import basejoin as urljoin
4 from FileCabinet import get_most_recent, get_entries_by_meta
5 from utils import config, run_callback
6
7 #regex to strip HTML
8 #HACK FIXME
9 HTML_RE = re.compile('(<.*?>)', re.DOTALL)
10 PARTIAL_HTML_RE = re.compile('(<[^>]$)', re.DOTALL)
11
12 class EntryStruct(object):
13     def __init__(self):
14         self.desc = ''
15         self.link = ''
16         self.relpath = ''
17         self.text = ''
18         self.time = ''
19         self.title = ''
20
21 class Rss(object):
22     #turn on etags, so we get last-modified
23     _cp_config = {"tools.etags.on": True,
24         "tools.etags.autotags": True,
25         "tools.response_headers.on": True,
26         "tools.response_headers.headers": [('Content-Type', 'application/xml')]}
27
28     def __init__(self, parent):
29         self.parent = parent
30
31     @cpy.expose
32     def index(self):
33         datadir = config('datadir')
34         num_entries = config('num_entries', 10)
35         entries = get_most_recent(datadir, num_entries)
36         return self.prepare_rss_template(entries)
37    
38     @cpy.expose
39     def default(self, *args, **kwargs):
40         if args[0].lower().startswith('keyword') and len(args) > 1:
41             return self.keyword_rss(args[1])
42         else: return self.index()
43
44     def keyword_rss(self, kw):
45         num_entries = config('num_entries', 10)
46         entries = get_entries_by_meta('keywords')
47         entries = [e for e in entries if kw in e.metadata['keywords']]
48         return self.prepare_rss_template(entries[:num_entries])
49
50     def prepare_rss_template(self, entries):
51         ns = cpy.config.get('/').copy()
52         entry_structs = []
53         for e in entries:
54             #XXX: what exactly is the <guid> element?
55             #XXX: what is the category tag? should keywords go here?
56             es = EntryStruct()
57             es.title = e.title
58
59             #this callback gives any interested plugins the chance to change
60             #the text of a story, as presented in a feed. It gives an Entry
61             #object, and ignores any return value
62             run_callback(self.parent.plugins, "cb_feed_story", e)
63
64             #because <style> messed me up, I'm going to stop stripping
65             #HTML out of my description. The RSS spec sucks.
66             es.desc = e.text
67             es.link = urljoin(config('base_url'), e.relpath + '.html')
68             es.relpath = e.relpath
69             es.time = time.strftime('%Y-%m-%dT%H:%M:%SZ', e.time_tuple)
70             es.text = e.text
71             entry_structs.append(es)
72         ns['entries'] = entry_structs
73         return ('rss', ns)
74
75     def strip_html(self, text):
76         text = HTML_RE.sub('', text)
77         return PARTIAL_HTML_RE.sub('', text).replace('&', '&amp;')
Note: See TracBrowser for help on using the browser.