1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/macros/RSSReader.py Sun Feb 24 21:06:46 2008 +0100
1.3 @@ -0,0 +1,125 @@
1.4 +# Import a RSS Feed into MoinMoin
1.5 +# Ian Wienand <ianw@ieee.org>
1.6 +# (C) 2006 - Public Domain
1.7 +
1.8 +# Using this macro
1.9 +# [[RSSReader(url[,allow_html])]]
1.10 +# where
1.11 +# * url is the url of the RSS/ATOM feed to read
1.12 +# * allow_html is an optional argument if you trust the feed to put
1.13 +# the HTML directly into the page
1.14 +
1.15 +# CAUTION: this could be an attack vector, although feedparser should
1.16 +# strip most "bad" HTML.
1.17 +
1.18 +# this tells MoinMoin not to cache the page, as we don't know when it
1.19 +# changes.
1.20 +Dependencies = ["time"]
1.21 +
1.22 +from MoinMoin import util, wikiutil, config
1.23 +from MoinMoin.Page import Page
1.24 +
1.25 +class RSStoWiki:
1.26 + def __init__(self, macro, url, allow_html):
1.27 + self.macro = macro
1.28 + self.fmt = macro.formatter
1.29 + self.allow_html = allow_html
1.30 + # in debian package python-feedparser
1.31 + import feedparser
1.32 + self.f = feedparser.parse(url)
1.33 + self.result = []
1.34 + if self.f.feed == {}:
1.35 + self.result.append (self.fmt.icon('info') + \
1.36 + self.fmt.strong(1) + \
1.37 + self.fmt.text(' Unable to retreive feed %s' % url) + \
1.38 + self.fmt.strong(0))
1.39 + self.valid = False
1.40 + else:
1.41 + self.valid = True
1.42 +
1.43 +
1.44 + def get_title(self):
1.45 + if not self.f.feed.has_key('title'):
1.46 + return
1.47 + self.result.append(self.fmt.heading(on=1, depth=1) + \
1.48 + self.fmt.text(self.f.feed.title) + \
1.49 + self.fmt.heading(on=0, depth=1))
1.50 +
1.51 + def get_subtitle(self):
1.52 + if not self.f.feed.has_key('subtitle'):
1.53 + return
1.54 + self.result.append(self.fmt.heading(on=1, depth=2) + \
1.55 + self.fmt.text(self.f.feed.subtitle) + \
1.56 + self.fmt.heading(on=0, depth=2))
1.57 +
1.58 + def get_paragraph(self, text):
1.59 + self.result.append(self.fmt.paragraph(on=1) + \
1.60 + self.fmt.text(text) + \
1.61 + self.fmt.paragraph(on=0))
1.62 +
1.63 + def get_link(self, link):
1.64 + self.result.append(self.fmt.url(on=1, href=link) + \
1.65 + self.fmt.icon('www') + \
1.66 + self.fmt.text(" "+link) + \
1.67 + self.fmt.url(on=0))
1.68 +
1.69 + def get_feedlink(self):
1.70 + if not self.f.feed.has_key('link'):
1.71 + return
1.72 + self.get_link(self.f.feed.link)
1.73 +
1.74 + def get_description(self):
1.75 + if not self.f.feed.has_key('description'):
1.76 + return
1.77 + self.get_paragraph(self.f.feed.description)
1.78 +
1.79 + def get_rule(self):
1.80 + self.result.append(self.fmt.rule(size=1))
1.81 +
1.82 + def get_entry_header(self, title):
1.83 + self.result.append(self.fmt.heading(on=1, depth=3) + \
1.84 + self.fmt.text(title) + \
1.85 + self.fmt.heading(on=0, depth=3))
1.86 +
1.87 + def get_entry_body(self, body):
1.88 + self.result.append(self.fmt.paragraph(on=1))
1.89 + if (self.allow_html):
1.90 + self.result.append(self.fmt.rawHTML(body))
1.91 + else:
1.92 + self.result.append(self.fmt.text(body))
1.93 + self.result.append(self.fmt.paragraph(on=0))
1.94 +
1.95 + def get_entries(self):
1.96 + for entry in self.f.entries:
1.97 + if entry.has_key('title'):
1.98 + self.get_entry_header(entry.title)
1.99 + if entry.has_key('updated'):
1.100 + self.get_paragraph(entry.updated)
1.101 + if entry.has_key('description'):
1.102 + self.get_entry_body(entry.description)
1.103 + if entry.has_key('link'):
1.104 + self.get_link(entry.link)
1.105 +
1.106 + def get_output(self):
1.107 + if self.valid:
1.108 + self.get_title()
1.109 + self.get_subtitle()
1.110 + self.get_description()
1.111 + self.get_feedlink()
1.112 + self.get_rule()
1.113 + self.get_entries()
1.114 + self.get_rule()
1.115 + return ''.join(self.result)
1.116 +
1.117 +def execute(macro, args):
1.118 + macro_args = args.split(",")
1.119 + try:
1.120 + if macro_args[1].strip() == "allow_html":
1.121 + allow_html = True
1.122 + else:
1.123 + allow_html = False
1.124 + except:
1.125 + allow_html = False
1.126 +
1.127 + rss = RSStoWiki(macro, macro_args[0], allow_html)
1.128 + return rss.get_output()