Attachment 'RSSReader-1.py'

Download

   1 # Import a RSS Feed into MoinMoin
   2 # Ian Wienand <ianw@ieee.org>
   3 # (C) 2006 - Public Domain
   4 
   5 # Using this macro
   6 # [[RSSReader(url[,allow_html])]]
   7 # where
   8 # * url is the url of the RSS/ATOM feed to read
   9 # * allow_html is an optional argument if you trust the feed to put
  10 #   the HTML directly into the page
  11 
  12 # CAUTION: this could be an attack vector, although feedparser should
  13 # strip most "bad" HTML.
  14 
  15 # this tells MoinMoin not to cache the page, as we don't know when it
  16 # changes.
  17 Dependencies = ["time"]
  18 
  19 from MoinMoin import util, wikiutil, config
  20 from MoinMoin.Page import Page
  21 
  22 class RSStoWiki:
  23     def __init__(self, macro, url, allow_html):
  24         self.macro = macro
  25         self.fmt = macro.formatter
  26         self.allow_html = allow_html
  27         # in debian package python-feedparser
  28         import feedparser
  29         self.f = feedparser.parse(url)
  30         self.result = []
  31         if self.f.feed == {}:
  32             self.result.append (self.fmt.icon('info') + \
  33                                 self.fmt.strong(1) + \
  34                                 self.fmt.text(' Unable to retreive feed %s' % url) + \
  35                                 self.fmt.strong(0))
  36             self.valid = False
  37         else:
  38             self.valid = True
  39                           
  40 
  41     def get_title(self):
  42         if not self.f.feed.has_key('title'):
  43             return
  44         self.result.append(self.fmt.heading(on=1, depth=1) + \
  45                            self.fmt.text(self.f.feed.title) + \
  46                            self.fmt.heading(on=0, depth=1))
  47 
  48     def get_subtitle(self):
  49         if not self.f.feed.has_key('subtitle'):
  50             return
  51         self.result.append(self.fmt.heading(on=1, depth=2) + \
  52                            self.fmt.text(self.f.feed.subtitle) + \
  53                            self.fmt.heading(on=0, depth=2))
  54 
  55     def get_paragraph(self, text):
  56         self.result.append(self.fmt.paragraph(on=1) + \
  57                            self.fmt.text(text) + \
  58                            self.fmt.paragraph(on=0))
  59 
  60     def get_link(self, link):
  61         self.result.append(self.fmt.url(on=1, href=link) + \
  62                            self.fmt.icon('www') + \
  63                            self.fmt.text(" "+link) + \
  64                            self.fmt.url(on=0))
  65         
  66     def get_feedlink(self):
  67         if not self.f.feed.has_key('link'):
  68             return
  69         self.get_link(self.f.feed.link)
  70 
  71     def get_description(self):
  72         if not self.f.feed.has_key('description'):
  73             return
  74         self.get_paragraph(self.f.feed.description)
  75 
  76     def get_rule(self):
  77         self.result.append(self.fmt.rule(size=1))
  78 
  79     def get_entry_header(self, title):
  80         self.result.append(self.fmt.heading(on=1, depth=3) + \
  81                            self.fmt.text(title) + \
  82                            self.fmt.heading(on=0, depth=3))
  83 
  84     def get_entry_body(self, body):
  85         self.result.append(self.fmt.paragraph(on=1))
  86         if (self.allow_html):
  87             self.result.append(self.fmt.rawHTML(body))
  88         else:
  89             self.result.append(self.fmt.text(body))
  90         self.result.append(self.fmt.paragraph(on=0))
  91 
  92     def get_entries(self):
  93         for entry in self.f.entries:
  94             if entry.has_key('title'):
  95                 self.get_entry_header(entry.title)
  96             if entry.has_key('updated'):
  97                 self.get_paragraph(entry.updated)
  98             if entry.has_key('description'):
  99                 self.get_entry_body(entry.description)
 100             if entry.has_key('link'):
 101                 self.get_link(entry.link)
 102 
 103     def get_output(self):
 104         if self.valid:
 105             self.get_title()
 106             self.get_subtitle()
 107             self.get_description()
 108             self.get_feedlink()
 109             self.get_rule()
 110             self.get_entries()
 111             self.get_rule()
 112         return ''.join(self.result)
 113 
 114 def execute(macro, args):
 115     macro_args = args.split(",")
 116     try:
 117         if macro_args[1].strip() == "allow_html":
 118             allow_html = True
 119         else:
 120             allow_html = False
 121     except:
 122         allow_html = False
 123 
 124     rss = RSStoWiki(macro, macro_args[0], allow_html)
 125     return rss.get_output()

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2006-02-22 00:46:41, 4.1 KB) [[attachment:RSSReader-1.py]]
  • [get | view] (2010-06-03 17:14:44, 8.4 KB) [[attachment:RSSReader-2.0.py]]
  • [get | view] (2010-06-03 17:06:24, 35.2 KB) [[attachment:RSSReader.png]]
  • [get | view] (2012-12-30 22:16:51, 9.5 KB) [[attachment:RSSReader.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.