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