paul@30 | 1 | # -*- coding: iso-8859-1 -*- |
paul@30 | 2 | """ |
paul@30 | 3 | Import a RSS Feed into MoinMoin |
paul@30 | 4 | |
paul@30 | 5 | @copyright: 2006 by Ian Wienand <ianw@ieee.org> (apparently assigned to the public domain) |
paul@30 | 6 | @copyright: 2008 by Paul Boddie <paul@boddie.org.uk> |
paul@30 | 7 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@30 | 8 | """ |
paul@29 | 9 | |
paul@29 | 10 | # Using this macro |
paul@29 | 11 | # [[RSSReader(url[,allow_html])]] |
paul@29 | 12 | # where |
paul@29 | 13 | # * url is the url of the RSS/ATOM feed to read |
paul@29 | 14 | # * allow_html is an optional argument if you trust the feed to put |
paul@29 | 15 | # the HTML directly into the page |
paul@29 | 16 | |
paul@29 | 17 | # CAUTION: this could be an attack vector, although feedparser should |
paul@29 | 18 | # strip most "bad" HTML. |
paul@29 | 19 | |
paul@29 | 20 | # this tells MoinMoin not to cache the page, as we don't know when it |
paul@29 | 21 | # changes. |
paul@29 | 22 | Dependencies = ["time"] |
paul@29 | 23 | |
paul@29 | 24 | from MoinMoin import util, wikiutil, config |
paul@29 | 25 | from MoinMoin.Page import Page |
paul@29 | 26 | |
paul@29 | 27 | class RSStoWiki: |
paul@30 | 28 | def __init__(self, macro, url, allow_html, compact_list): |
paul@29 | 29 | self.macro = macro |
paul@29 | 30 | self.fmt = macro.formatter |
paul@30 | 31 | self.url = url |
paul@29 | 32 | self.allow_html = allow_html |
paul@30 | 33 | self.compact_list = compact_list |
paul@29 | 34 | # in debian package python-feedparser |
paul@29 | 35 | import feedparser |
paul@29 | 36 | self.f = feedparser.parse(url) |
paul@29 | 37 | self.result = [] |
paul@29 | 38 | if self.f.feed == {}: |
paul@29 | 39 | self.result.append (self.fmt.icon('info') + \ |
paul@29 | 40 | self.fmt.strong(1) + \ |
paul@30 | 41 | self.fmt.text(' Unable to retrieve feed %s' % url) + \ |
paul@29 | 42 | self.fmt.strong(0)) |
paul@29 | 43 | self.valid = False |
paul@29 | 44 | else: |
paul@29 | 45 | self.valid = True |
paul@29 | 46 | |
paul@29 | 47 | |
paul@29 | 48 | def get_title(self): |
paul@29 | 49 | if not self.f.feed.has_key('title'): |
paul@29 | 50 | return |
paul@29 | 51 | self.result.append(self.fmt.heading(on=1, depth=1) + \ |
paul@29 | 52 | self.fmt.text(self.f.feed.title) + \ |
paul@29 | 53 | self.fmt.heading(on=0, depth=1)) |
paul@29 | 54 | |
paul@29 | 55 | def get_subtitle(self): |
paul@29 | 56 | if not self.f.feed.has_key('subtitle'): |
paul@29 | 57 | return |
paul@29 | 58 | self.result.append(self.fmt.heading(on=1, depth=2) + \ |
paul@29 | 59 | self.fmt.text(self.f.feed.subtitle) + \ |
paul@29 | 60 | self.fmt.heading(on=0, depth=2)) |
paul@29 | 61 | |
paul@29 | 62 | def get_paragraph(self, text): |
paul@29 | 63 | self.result.append(self.fmt.paragraph(on=1) + \ |
paul@29 | 64 | self.fmt.text(text) + \ |
paul@29 | 65 | self.fmt.paragraph(on=0)) |
paul@29 | 66 | |
paul@29 | 67 | def get_link(self, link): |
paul@29 | 68 | self.result.append(self.fmt.url(on=1, href=link) + \ |
paul@29 | 69 | self.fmt.icon('www') + \ |
paul@29 | 70 | self.fmt.text(" "+link) + \ |
paul@29 | 71 | self.fmt.url(on=0)) |
paul@29 | 72 | |
paul@29 | 73 | def get_feedlink(self): |
paul@29 | 74 | if not self.f.feed.has_key('link'): |
paul@29 | 75 | return |
paul@29 | 76 | self.get_link(self.f.feed.link) |
paul@29 | 77 | |
paul@29 | 78 | def get_description(self): |
paul@29 | 79 | if not self.f.feed.has_key('description'): |
paul@29 | 80 | return |
paul@29 | 81 | self.get_paragraph(self.f.feed.description) |
paul@29 | 82 | |
paul@29 | 83 | def get_rule(self): |
paul@29 | 84 | self.result.append(self.fmt.rule(size=1)) |
paul@29 | 85 | |
paul@29 | 86 | def get_entry_header(self, title): |
paul@29 | 87 | self.result.append(self.fmt.heading(on=1, depth=3) + \ |
paul@29 | 88 | self.fmt.text(title) + \ |
paul@29 | 89 | self.fmt.heading(on=0, depth=3)) |
paul@29 | 90 | |
paul@29 | 91 | def get_entry_body(self, body): |
paul@29 | 92 | self.result.append(self.fmt.paragraph(on=1)) |
paul@29 | 93 | if (self.allow_html): |
paul@29 | 94 | self.result.append(self.fmt.rawHTML(body)) |
paul@29 | 95 | else: |
paul@29 | 96 | self.result.append(self.fmt.text(body)) |
paul@29 | 97 | self.result.append(self.fmt.paragraph(on=0)) |
paul@29 | 98 | |
paul@29 | 99 | def get_entries(self): |
paul@29 | 100 | for entry in self.f.entries: |
paul@29 | 101 | if entry.has_key('title'): |
paul@29 | 102 | self.get_entry_header(entry.title) |
paul@29 | 103 | if entry.has_key('updated'): |
paul@29 | 104 | self.get_paragraph(entry.updated) |
paul@29 | 105 | if entry.has_key('description'): |
paul@29 | 106 | self.get_entry_body(entry.description) |
paul@29 | 107 | if entry.has_key('link'): |
paul@29 | 108 | self.get_link(entry.link) |
paul@29 | 109 | |
paul@30 | 110 | def get_compact_title(self): |
paul@30 | 111 | if not self.f.feed.has_key('title') or not self.f.feed.has_key('link'): |
paul@30 | 112 | return |
paul@30 | 113 | self.result.append(self.fmt.url(on=1, href=self.f.feed.link) + \ |
paul@30 | 114 | self.fmt.text(self.f.feed.title) + \ |
paul@30 | 115 | self.fmt.url(on=0) + \ |
paul@30 | 116 | self.fmt.text(" ") + \ |
paul@30 | 117 | self.fmt.url(on=1, href=self.url) + \ |
paul@30 | 118 | self.fmt.icon('rss') + \ |
paul@30 | 119 | self.fmt.url(on=0)) |
paul@30 | 120 | |
paul@30 | 121 | def get_compact_entry(self, entry): |
paul@30 | 122 | if entry.has_key('link') and entry.has_key('title'): |
paul@30 | 123 | self.result.append(self.fmt.listitem(on=1)) |
paul@30 | 124 | #self.result.append(self.fmt.paragraph(on=1)) |
paul@30 | 125 | self.result.append(self.fmt.url(on=1, href=entry.link) + \ |
paul@30 | 126 | self.fmt.icon('www') + \ |
paul@30 | 127 | self.fmt.text(" "+entry.title) + \ |
paul@30 | 128 | self.fmt.url(on=0)) |
paul@30 | 129 | #self.result.append(self.fmt.paragraph(on=0)) |
paul@30 | 130 | self.result.append(self.fmt.listitem(on=0)) |
paul@30 | 131 | |
paul@30 | 132 | def get_compact_entries(self): |
paul@30 | 133 | self.result.append(self.fmt.bullet_list(on=1)) |
paul@30 | 134 | for entry in self.f.entries: |
paul@30 | 135 | self.get_compact_entry(entry) |
paul@30 | 136 | self.result.append(self.fmt.bullet_list(on=0)) |
paul@30 | 137 | |
paul@29 | 138 | def get_output(self): |
paul@29 | 139 | if self.valid: |
paul@30 | 140 | if self.compact_list: |
paul@30 | 141 | self.get_compact_title() |
paul@30 | 142 | self.get_compact_entries() |
paul@30 | 143 | else: |
paul@30 | 144 | self.get_title() |
paul@30 | 145 | self.get_subtitle() |
paul@30 | 146 | self.get_description() |
paul@30 | 147 | self.get_feedlink() |
paul@30 | 148 | self.get_rule() |
paul@30 | 149 | self.get_entries() |
paul@30 | 150 | self.get_rule() |
paul@29 | 151 | return ''.join(self.result) |
paul@29 | 152 | |
paul@29 | 153 | def execute(macro, args): |
paul@29 | 154 | macro_args = args.split(",") |
paul@30 | 155 | allow_html = False |
paul@30 | 156 | compact_list = False |
paul@29 | 157 | try: |
paul@29 | 158 | if macro_args[1].strip() == "allow_html": |
paul@29 | 159 | allow_html = True |
paul@30 | 160 | if macro_args[1].strip() == "compact_list": |
paul@30 | 161 | compact_list = True |
paul@29 | 162 | except: |
paul@30 | 163 | pass |
paul@29 | 164 | |
paul@30 | 165 | rss = RSStoWiki(macro, macro_args[0], allow_html, compact_list) |
paul@29 | 166 | return rss.get_output() |