paul@13 | 1 | # -*- coding: iso-8859-1 -*- |
paul@13 | 2 | """ |
paul@13 | 3 | MoinMoin - SharedContent macro, based on the FeedReader macro |
paul@13 | 4 | |
paul@18 | 5 | @copyright: 2008, 2012, 2013 by Paul Boddie <paul@boddie.org.uk> |
paul@13 | 6 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@13 | 7 | """ |
paul@13 | 8 | |
paul@24 | 9 | from DateSupport import getDateTimeFromISO8601, DateTime |
paul@13 | 10 | from MoinMoin.Page import Page |
paul@14 | 11 | from MoinRemoteSupport import * |
paul@20 | 12 | from MoinSupport import parseMacroArguments |
paul@22 | 13 | from email.utils import parsedate |
paul@13 | 14 | import xml.dom.pulldom |
paul@13 | 15 | |
paul@14 | 16 | try: |
paul@14 | 17 | from cStringIO import StringIO |
paul@14 | 18 | except ImportError: |
paul@14 | 19 | from StringIO import StringIO |
paul@14 | 20 | |
paul@13 | 21 | Dependencies = ["time"] |
paul@13 | 22 | |
paul@13 | 23 | MAX_ENTRIES = 5 |
paul@13 | 24 | ATOM_NS = "http://www.w3.org/2005/Atom" |
paul@13 | 25 | |
paul@22 | 26 | # Utility functions. |
paul@22 | 27 | |
paul@13 | 28 | def text(element): |
paul@13 | 29 | nodes = [] |
paul@13 | 30 | for node in element.childNodes: |
paul@13 | 31 | if node.nodeType == node.TEXT_NODE: |
paul@13 | 32 | nodes.append(node.nodeValue) |
paul@13 | 33 | return "".join(nodes) |
paul@13 | 34 | |
paul@20 | 35 | def unescape(text): |
paul@20 | 36 | return text.replace("<", "<").replace(">", ">").replace("&", "&") |
paul@20 | 37 | |
paul@13 | 38 | def linktext(element, feed_type): |
paul@13 | 39 | if feed_type == "rss": |
paul@13 | 40 | return text(element) |
paul@13 | 41 | else: |
paul@13 | 42 | return element.getAttribute("href") |
paul@13 | 43 | |
paul@22 | 44 | # Error classes. |
paul@22 | 45 | |
paul@22 | 46 | class FeedError(Exception): |
paul@22 | 47 | pass |
paul@22 | 48 | |
paul@22 | 49 | class FeedMissingError(FeedError): |
paul@22 | 50 | pass |
paul@13 | 51 | |
paul@22 | 52 | class FeedContentTypeError(FeedError): |
paul@22 | 53 | pass |
paul@22 | 54 | |
paul@22 | 55 | # Entry/update classes. |
paul@22 | 56 | |
paul@22 | 57 | class Update: |
paul@22 | 58 | |
paul@22 | 59 | "A feed update entry." |
paul@20 | 60 | |
paul@22 | 61 | def __init__(self): |
paul@22 | 62 | self.title = None |
paul@22 | 63 | self.link = None |
paul@22 | 64 | self.content = None |
paul@22 | 65 | self.content_type = None |
paul@22 | 66 | self.updated = None |
paul@20 | 67 | |
paul@22 | 68 | def __cmp__(self, other): |
paul@22 | 69 | if self.updated is None and other.updated is not None: |
paul@22 | 70 | return 1 |
paul@22 | 71 | elif self.updated is not None and other.updated is None: |
paul@22 | 72 | return -1 |
paul@22 | 73 | else: |
paul@22 | 74 | return cmp(self.updated, other.updated) |
paul@22 | 75 | |
paul@22 | 76 | # Feed retrieval. |
paul@20 | 77 | |
paul@22 | 78 | def getUpdates(request, feed_url, max_entries): |
paul@22 | 79 | |
paul@22 | 80 | """ |
paul@22 | 81 | Using the given 'request', retrieve from 'feed_url' up to the given number |
paul@22 | 82 | 'max_entries' of update entries. |
paul@22 | 83 | |
paul@22 | 84 | A tuple of the form ((feed_type, channel_title, channel_link), updates) is |
paul@22 | 85 | returned. |
paul@22 | 86 | """ |
paul@22 | 87 | |
paul@22 | 88 | feed_updates = [] |
paul@13 | 89 | |
paul@14 | 90 | # Obtain the resource, using a cached version if appropriate. |
paul@13 | 91 | |
paul@14 | 92 | max_cache_age = int(getattr(request.cfg, "moin_share_max_cache_age", "300")) |
paul@14 | 93 | data = getCachedResource(request, feed_url, "MoinShare", "wiki", max_cache_age) |
paul@14 | 94 | if not data: |
paul@22 | 95 | raise FeedMissingError |
paul@22 | 96 | |
paul@22 | 97 | # Interpret the cached feed. |
paul@14 | 98 | |
paul@14 | 99 | feed = StringIO(data) |
paul@20 | 100 | _url, content_type, _encoding, _metadata = getCachedResourceMetadata(feed) |
paul@20 | 101 | |
paul@27 | 102 | if content_type not in ("application/atom+xml", "application/rss+xml", "application/xml"): |
paul@22 | 103 | raise FeedContentTypeError |
paul@13 | 104 | |
paul@13 | 105 | try: |
paul@13 | 106 | # Parse each node from the feed. |
paul@13 | 107 | |
paul@13 | 108 | channel_title = channel_link = None |
paul@13 | 109 | |
paul@13 | 110 | feed_type = None |
paul@22 | 111 | update = None |
paul@27 | 112 | in_source = False |
paul@13 | 113 | |
paul@13 | 114 | events = xml.dom.pulldom.parse(feed) |
paul@13 | 115 | |
paul@13 | 116 | for event, value in events: |
paul@13 | 117 | |
paul@27 | 118 | if not in_source and event == xml.dom.pulldom.START_ELEMENT: |
paul@13 | 119 | tagname = value.localName |
paul@13 | 120 | |
paul@13 | 121 | # Detect the feed type and items. |
paul@13 | 122 | |
paul@13 | 123 | if tagname == "feed" and value.namespaceURI == ATOM_NS: |
paul@13 | 124 | feed_type = "atom" |
paul@13 | 125 | |
paul@13 | 126 | elif tagname == "rss": |
paul@13 | 127 | feed_type = "rss" |
paul@13 | 128 | |
paul@13 | 129 | # Detect items. |
paul@13 | 130 | |
paul@13 | 131 | elif feed_type == "rss" and tagname == "item" or \ |
paul@13 | 132 | feed_type == "atom" and tagname == "entry": |
paul@13 | 133 | |
paul@22 | 134 | update = Update() |
paul@13 | 135 | |
paul@27 | 136 | # Detect source declarations. |
paul@27 | 137 | |
paul@27 | 138 | elif feed_type == "atom" and tagname == "source": |
paul@27 | 139 | in_source = True |
paul@27 | 140 | |
paul@27 | 141 | # Handle item elements. |
paul@27 | 142 | |
paul@13 | 143 | elif tagname == "title": |
paul@13 | 144 | events.expandNode(value) |
paul@22 | 145 | if update: |
paul@22 | 146 | update.title = text(value) |
paul@13 | 147 | else: |
paul@22 | 148 | channel_title = text(value) |
paul@13 | 149 | |
paul@13 | 150 | elif tagname == "link": |
paul@13 | 151 | events.expandNode(value) |
paul@22 | 152 | if update: |
paul@22 | 153 | update.link = linktext(value, feed_type) |
paul@13 | 154 | else: |
paul@22 | 155 | channel_link = linktext(value, feed_type) |
paul@13 | 156 | |
paul@20 | 157 | elif feed_type == "atom" and tagname == "content": |
paul@20 | 158 | events.expandNode(value) |
paul@22 | 159 | if update: |
paul@22 | 160 | update.content_type = value.getAttribute("type") |
paul@27 | 161 | if update.content_type in ("xhtml", "application/xhtml+xml", "application/xml"): |
paul@27 | 162 | update.content = value.toxml() |
paul@27 | 163 | else: |
paul@27 | 164 | update.content = text(value) |
paul@22 | 165 | |
paul@22 | 166 | elif feed_type == "atom" and tagname == "updated" or \ |
paul@22 | 167 | feed_type == "rss" and tagname == "pubDate": |
paul@22 | 168 | events.expandNode(value) |
paul@22 | 169 | |
paul@22 | 170 | if update: |
paul@22 | 171 | if feed_type == "atom": |
paul@24 | 172 | value = getDateTimeFromISO8601(text(value)) |
paul@22 | 173 | else: |
paul@22 | 174 | value = DateTime(parsedate(text(value))) |
paul@22 | 175 | update.updated = value |
paul@20 | 176 | |
paul@13 | 177 | elif event == xml.dom.pulldom.END_ELEMENT: |
paul@13 | 178 | tagname = value.localName |
paul@13 | 179 | |
paul@13 | 180 | if feed_type == "rss" and tagname == "item" or \ |
paul@13 | 181 | feed_type == "atom" and tagname == "entry": |
paul@13 | 182 | |
paul@23 | 183 | feed_updates.append(update) |
paul@20 | 184 | |
paul@22 | 185 | update = None |
paul@13 | 186 | |
paul@27 | 187 | elif feed_type == "atom" and tagname == "source": |
paul@27 | 188 | in_source = False |
paul@27 | 189 | |
paul@22 | 190 | finally: |
paul@22 | 191 | feed.close() |
paul@22 | 192 | |
paul@22 | 193 | return (feed_type, channel_title, channel_link), feed_updates |
paul@22 | 194 | |
paul@22 | 195 | # The macro itself. |
paul@22 | 196 | |
paul@22 | 197 | def execute(macro, args): |
paul@22 | 198 | request = macro.request |
paul@22 | 199 | fmt = macro.formatter |
paul@22 | 200 | _ = request.getText |
paul@22 | 201 | |
paul@22 | 202 | feed_urls = [] |
paul@22 | 203 | show_content = None |
paul@22 | 204 | max_entries = None |
paul@22 | 205 | |
paul@22 | 206 | for arg, value in parseMacroArguments(args): |
paul@22 | 207 | if arg == "url": |
paul@22 | 208 | feed_urls.append(value) |
paul@22 | 209 | elif arg == "show": |
paul@22 | 210 | show_content = value in ("true", "True", "yes") |
paul@22 | 211 | elif arg == "limit": |
paul@22 | 212 | try: |
paul@22 | 213 | max_entries = int(value) |
paul@22 | 214 | except ValueError: |
paul@22 | 215 | return fmt.text(_("SharedContent: limit must be set to the maximum number of entries to be shown")) |
paul@22 | 216 | |
paul@22 | 217 | if not feed_urls: |
paul@22 | 218 | return fmt.text(_("SharedContent: a feed URL must be specified")) |
paul@22 | 219 | |
paul@22 | 220 | show_content = show_content or False |
paul@22 | 221 | max_entries = max_entries or MAX_ENTRIES |
paul@22 | 222 | |
paul@23 | 223 | # Retrieve updates from feeds, classifying them as missing or bad and |
paul@23 | 224 | # excluding them if appropriate. |
paul@23 | 225 | |
paul@22 | 226 | updates = [] |
paul@22 | 227 | feeds = [] |
paul@22 | 228 | missing = [] |
paul@22 | 229 | bad_content = [] |
paul@13 | 230 | |
paul@22 | 231 | for feed_url in feed_urls: |
paul@22 | 232 | try: |
paul@22 | 233 | feed_info, feed_updates = getUpdates(request, feed_url, max_entries) |
paul@22 | 234 | updates += feed_updates |
paul@26 | 235 | feeds.append((feed_url, feed_info)) |
paul@22 | 236 | except FeedMissingError: |
paul@22 | 237 | missing.append(feed_url) |
paul@22 | 238 | except FeedContentTypeError: |
paul@22 | 239 | bad_content.append(feed_url) |
paul@22 | 240 | |
paul@22 | 241 | output = [] |
paul@22 | 242 | append = output.append |
paul@22 | 243 | |
paul@22 | 244 | # Show the updates. |
paul@22 | 245 | |
paul@22 | 246 | if not show_content: |
paul@22 | 247 | append(fmt.bullet_list(on=1)) |
paul@22 | 248 | |
paul@22 | 249 | # NOTE: Permit configurable sorting. |
paul@22 | 250 | |
paul@22 | 251 | updates.sort() |
paul@22 | 252 | updates.reverse() |
paul@22 | 253 | |
paul@23 | 254 | # Truncate the number of updates to the maximum number. |
paul@23 | 255 | |
paul@23 | 256 | updates = updates[:max_entries] |
paul@23 | 257 | |
paul@22 | 258 | for update in updates: |
paul@22 | 259 | |
paul@22 | 260 | # Emit content where appropriate. |
paul@27 | 261 | # NOTE: HTML and XHTML should be sanitised. |
paul@22 | 262 | |
paul@22 | 263 | if show_content: |
paul@22 | 264 | append(fmt.div(on=1, css_class="moinshare-update")) |
paul@24 | 265 | append(fmt.div(on=1, css_class="moinshare-content")) |
paul@27 | 266 | if update.content: |
paul@27 | 267 | if update.content_type in ("html", "text/html"): |
paul@27 | 268 | append(fmt.rawHTML(unescape(update.content))) |
paul@27 | 269 | elif update.content_type in ("xhtml", "application/xhtml+xml"): |
paul@27 | 270 | append(fmt.rawHTML(update.content)) |
paul@27 | 271 | elif update.content_type in ("text", "text/plain"): |
paul@27 | 272 | append(fmt.text(update.content)) |
paul@22 | 273 | append(fmt.div(on=0)) |
paul@24 | 274 | append(fmt.div(on=1, css_class="moinshare-date")) |
paul@24 | 275 | append(fmt.text(str(update.updated))) |
paul@24 | 276 | append(fmt.div(on=0)) |
paul@24 | 277 | append(fmt.div(on=0)) |
paul@22 | 278 | |
paul@22 | 279 | # Or emit title and link information for items. |
paul@22 | 280 | |
paul@22 | 281 | elif update.title and update.link: |
paul@22 | 282 | append(fmt.listitem(on=1, css_class="moinshare-update")) |
paul@22 | 283 | append(fmt.url(on=1, href=update.link)) |
paul@22 | 284 | append(fmt.icon('www')) |
paul@22 | 285 | append(fmt.text(" " + update.title)) |
paul@22 | 286 | append(fmt.url(on=0)) |
paul@22 | 287 | append(fmt.listitem(on=0)) |
paul@22 | 288 | |
paul@22 | 289 | if not show_content: |
paul@22 | 290 | append(fmt.bullet_list(on=0)) |
paul@22 | 291 | |
paul@22 | 292 | # Show the feeds. |
paul@22 | 293 | |
paul@26 | 294 | for feed_url, (feed_type, channel_title, channel_link) in feeds: |
paul@13 | 295 | if channel_title and channel_link: |
paul@22 | 296 | append(fmt.paragraph(on=1, css_class="moinshare-feed")) |
paul@22 | 297 | append(fmt.url(on=1, href=channel_link)) |
paul@22 | 298 | append(fmt.text(channel_title)) |
paul@13 | 299 | append(fmt.url(on=0)) |
paul@13 | 300 | append(fmt.text(" ")) |
paul@13 | 301 | append(fmt.url(on=1, href=feed_url)) |
paul@13 | 302 | append(fmt.icon('rss')) |
paul@13 | 303 | append(fmt.url(on=0)) |
paul@13 | 304 | append(fmt.paragraph(on=0)) |
paul@13 | 305 | |
paul@22 | 306 | # Show errors. |
paul@22 | 307 | |
paul@22 | 308 | for feed_url in missing: |
paul@22 | 309 | append(fmt.paragraph(on=1, css_class="moinshare-missing-feed-error")) |
paul@22 | 310 | append(fmt.text(_("SharedContent: updates could not be retrieved for %s") % feed_url)) |
paul@22 | 311 | append(fmt.paragraph(on=0)) |
paul@22 | 312 | |
paul@22 | 313 | for feed_url in bad_content: |
paul@22 | 314 | append(fmt.paragraph(on=1, css_class="moinshare-content-type-feed-error")) |
paul@22 | 315 | return fmt.text(_("SharedContent: updates for %s were not provided in Atom or RSS format") % feed_url) |
paul@22 | 316 | append(fmt.paragraph(on=0)) |
paul@13 | 317 | |
paul@13 | 318 | return ''.join(output) |
paul@13 | 319 | |
paul@13 | 320 | # vim: tabstop=4 expandtab shiftwidth=4 |