1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/macros/FeedReader.py Tue Feb 26 02:13:09 2008 +0100
1.3 @@ -0,0 +1,106 @@
1.4 +# -*- coding: iso-8859-1 -*-
1.5 +"""
1.6 + MoinMoin - FeedReader Macro
1.7 +
1.8 + @copyright: 2008 by Paul Boddie <paul@boddie.org.uk>
1.9 + @license: GNU GPL (v2 or later), see COPYING.txt for details.
1.10 +"""
1.11 +
1.12 +from MoinMoin.Page import Page
1.13 +import urllib
1.14 +import xml.dom.pulldom
1.15 +
1.16 +Dependencies = ["time"]
1.17 +
1.18 +MAX_ENTRIES = 5
1.19 +
1.20 +def text(element):
1.21 + nodes = []
1.22 + for node in element.childNodes:
1.23 + if node.nodeType == node.TEXT_NODE:
1.24 + nodes.append(node.nodeValue)
1.25 + return "".join(nodes)
1.26 +
1.27 +def execute(macro, args):
1.28 + request = macro.request
1.29 + fmt = macro.formatter
1.30 +
1.31 + max_entries = MAX_ENTRIES
1.32 + args = args.split(",")
1.33 + if args:
1.34 + try:
1.35 + feed_url = args[0]
1.36 + max_entries = int(args[1])
1.37 + except ValueError:
1.38 + pass
1.39 +
1.40 + feed = urllib.urlopen(feed_url)
1.41 + output = []
1.42 + nentries = 0
1.43 + try:
1.44 + events = xml.dom.pulldom.parse(feed)
1.45 +
1.46 + title = None
1.47 + link = None
1.48 + channel_title_text = None
1.49 + channel_link_text = None
1.50 +
1.51 + output.append(fmt.bullet_list(on=1))
1.52 +
1.53 + in_item = 0
1.54 + for event, value in events:
1.55 + if event == xml.dom.pulldom.START_ELEMENT:
1.56 + if value.nodeName == "item":
1.57 + in_item = 1
1.58 +
1.59 + if value.nodeName == "title":
1.60 + events.expandNode(value)
1.61 + title = value
1.62 + elif value.nodeName == "link":
1.63 + events.expandNode(value)
1.64 + link = value
1.65 +
1.66 + if title is not None and link is not None:
1.67 + link_text = text(link)
1.68 + title_text = text(title)
1.69 +
1.70 + if nentries >= max_entries:
1.71 + title = None
1.72 + link = None
1.73 + continue
1.74 +
1.75 + if in_item:
1.76 + output.append(fmt.listitem(on=1))
1.77 + output.append(fmt.url(on=1, href=link_text))
1.78 + output.append(fmt.icon('www'))
1.79 + output.append(fmt.text(" " + title_text))
1.80 + output.append(fmt.url(on=0))
1.81 + output.append(fmt.listitem(on=0))
1.82 + nentries += 1
1.83 + else:
1.84 + channel_title_text = title_text
1.85 + channel_link_text = link_text
1.86 +
1.87 + title = None
1.88 + link = None
1.89 +
1.90 + elif event == xml.dom.pulldom.END_ELEMENT:
1.91 + if value.nodeName == "item":
1.92 + in_item = 0
1.93 +
1.94 + output.append(fmt.bullet_list(on=0))
1.95 +
1.96 + if channel_title_text is not None and channel_link_text is not None:
1.97 + output.append(fmt.paragraph(on=1))
1.98 + output.append(fmt.url(on=1, href=channel_link_text))
1.99 + output.append(fmt.text(channel_title_text + " "))
1.100 + output.append(fmt.icon('rss'))
1.101 + output.append(fmt.url(on=0))
1.102 + output.append(fmt.paragraph(on=0))
1.103 +
1.104 + finally:
1.105 + feed.close()
1.106 +
1.107 + return ''.join(output)
1.108 +
1.109 +# vim: tabstop=4 expandtab shiftwidth=4
2.1 --- a/macros/RSSReader.py Sun Feb 24 21:06:46 2008 +0100
2.2 +++ b/macros/RSSReader.py Tue Feb 26 02:13:09 2008 +0100
2.3 @@ -1,6 +1,11 @@
2.4 -# Import a RSS Feed into MoinMoin
2.5 -# Ian Wienand <ianw@ieee.org>
2.6 -# (C) 2006 - Public Domain
2.7 +# -*- coding: iso-8859-1 -*-
2.8 +"""
2.9 + Import a RSS Feed into MoinMoin
2.10 +
2.11 + @copyright: 2006 by Ian Wienand <ianw@ieee.org> (apparently assigned to the public domain)
2.12 + @copyright: 2008 by Paul Boddie <paul@boddie.org.uk>
2.13 + @license: GNU GPL (v2 or later), see COPYING.txt for details.
2.14 +"""
2.15
2.16 # Using this macro
2.17 # [[RSSReader(url[,allow_html])]]
2.18 @@ -20,10 +25,12 @@
2.19 from MoinMoin.Page import Page
2.20
2.21 class RSStoWiki:
2.22 - def __init__(self, macro, url, allow_html):
2.23 + def __init__(self, macro, url, allow_html, compact_list):
2.24 self.macro = macro
2.25 self.fmt = macro.formatter
2.26 + self.url = url
2.27 self.allow_html = allow_html
2.28 + self.compact_list = compact_list
2.29 # in debian package python-feedparser
2.30 import feedparser
2.31 self.f = feedparser.parse(url)
2.32 @@ -31,7 +38,7 @@
2.33 if self.f.feed == {}:
2.34 self.result.append (self.fmt.icon('info') + \
2.35 self.fmt.strong(1) + \
2.36 - self.fmt.text(' Unable to retreive feed %s' % url) + \
2.37 + self.fmt.text(' Unable to retrieve feed %s' % url) + \
2.38 self.fmt.strong(0))
2.39 self.valid = False
2.40 else:
2.41 @@ -100,26 +107,60 @@
2.42 if entry.has_key('link'):
2.43 self.get_link(entry.link)
2.44
2.45 + def get_compact_title(self):
2.46 + if not self.f.feed.has_key('title') or not self.f.feed.has_key('link'):
2.47 + return
2.48 + self.result.append(self.fmt.url(on=1, href=self.f.feed.link) + \
2.49 + self.fmt.text(self.f.feed.title) + \
2.50 + self.fmt.url(on=0) + \
2.51 + self.fmt.text(" ") + \
2.52 + self.fmt.url(on=1, href=self.url) + \
2.53 + self.fmt.icon('rss') + \
2.54 + self.fmt.url(on=0))
2.55 +
2.56 + def get_compact_entry(self, entry):
2.57 + if entry.has_key('link') and entry.has_key('title'):
2.58 + self.result.append(self.fmt.listitem(on=1))
2.59 + #self.result.append(self.fmt.paragraph(on=1))
2.60 + self.result.append(self.fmt.url(on=1, href=entry.link) + \
2.61 + self.fmt.icon('www') + \
2.62 + self.fmt.text(" "+entry.title) + \
2.63 + self.fmt.url(on=0))
2.64 + #self.result.append(self.fmt.paragraph(on=0))
2.65 + self.result.append(self.fmt.listitem(on=0))
2.66 +
2.67 + def get_compact_entries(self):
2.68 + self.result.append(self.fmt.bullet_list(on=1))
2.69 + for entry in self.f.entries:
2.70 + self.get_compact_entry(entry)
2.71 + self.result.append(self.fmt.bullet_list(on=0))
2.72 +
2.73 def get_output(self):
2.74 if self.valid:
2.75 - self.get_title()
2.76 - self.get_subtitle()
2.77 - self.get_description()
2.78 - self.get_feedlink()
2.79 - self.get_rule()
2.80 - self.get_entries()
2.81 - self.get_rule()
2.82 + if self.compact_list:
2.83 + self.get_compact_title()
2.84 + self.get_compact_entries()
2.85 + else:
2.86 + self.get_title()
2.87 + self.get_subtitle()
2.88 + self.get_description()
2.89 + self.get_feedlink()
2.90 + self.get_rule()
2.91 + self.get_entries()
2.92 + self.get_rule()
2.93 return ''.join(self.result)
2.94
2.95 def execute(macro, args):
2.96 macro_args = args.split(",")
2.97 + allow_html = False
2.98 + compact_list = False
2.99 try:
2.100 if macro_args[1].strip() == "allow_html":
2.101 allow_html = True
2.102 - else:
2.103 - allow_html = False
2.104 + if macro_args[1].strip() == "compact_list":
2.105 + compact_list = True
2.106 except:
2.107 - allow_html = False
2.108 + pass
2.109
2.110 - rss = RSStoWiki(macro, macro_args[0], allow_html)
2.111 + rss = RSStoWiki(macro, macro_args[0], allow_html, compact_list)
2.112 return rss.get_output()