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@13 | 5 | @copyright: 2008, 2012 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@13 | 9 | from MoinMoin.Page import Page |
paul@13 | 10 | import urllib |
paul@13 | 11 | import xml.dom.pulldom |
paul@13 | 12 | |
paul@13 | 13 | Dependencies = ["time"] |
paul@13 | 14 | |
paul@13 | 15 | MAX_ENTRIES = 5 |
paul@13 | 16 | ATOM_NS = "http://www.w3.org/2005/Atom" |
paul@13 | 17 | |
paul@13 | 18 | def text(element): |
paul@13 | 19 | nodes = [] |
paul@13 | 20 | for node in element.childNodes: |
paul@13 | 21 | if node.nodeType == node.TEXT_NODE: |
paul@13 | 22 | nodes.append(node.nodeValue) |
paul@13 | 23 | return "".join(nodes) |
paul@13 | 24 | |
paul@13 | 25 | def linktext(element, feed_type): |
paul@13 | 26 | if feed_type == "rss": |
paul@13 | 27 | return text(element) |
paul@13 | 28 | else: |
paul@13 | 29 | return element.getAttribute("href") |
paul@13 | 30 | |
paul@13 | 31 | def execute(macro, args): |
paul@13 | 32 | request = macro.request |
paul@13 | 33 | fmt = macro.formatter |
paul@13 | 34 | |
paul@13 | 35 | max_entries = MAX_ENTRIES |
paul@13 | 36 | args = args.split(",") |
paul@13 | 37 | if args: |
paul@13 | 38 | try: |
paul@13 | 39 | feed_url = args[0] |
paul@13 | 40 | max_entries = int(args[1]) |
paul@13 | 41 | except IndexError: |
paul@13 | 42 | pass |
paul@13 | 43 | |
paul@13 | 44 | # Obtain the feed. |
paul@13 | 45 | |
paul@13 | 46 | feed = urllib.urlopen(feed_url) |
paul@13 | 47 | |
paul@13 | 48 | try: |
paul@13 | 49 | # Parse each node from the feed. |
paul@13 | 50 | |
paul@13 | 51 | title = link = None |
paul@13 | 52 | channel_title = channel_link = None |
paul@13 | 53 | |
paul@13 | 54 | output = [] |
paul@13 | 55 | append = output.append |
paul@13 | 56 | append(fmt.bullet_list(on=1)) |
paul@13 | 57 | |
paul@13 | 58 | feed_type = None |
paul@13 | 59 | in_item = False |
paul@13 | 60 | nentries = 0 |
paul@13 | 61 | |
paul@13 | 62 | events = xml.dom.pulldom.parse(feed) |
paul@13 | 63 | |
paul@13 | 64 | for event, value in events: |
paul@13 | 65 | |
paul@13 | 66 | if event == xml.dom.pulldom.START_ELEMENT: |
paul@13 | 67 | tagname = value.localName |
paul@13 | 68 | |
paul@13 | 69 | # Detect the feed type and items. |
paul@13 | 70 | |
paul@13 | 71 | if tagname == "feed" and value.namespaceURI == ATOM_NS: |
paul@13 | 72 | feed_type = "atom" |
paul@13 | 73 | |
paul@13 | 74 | elif tagname == "rss": |
paul@13 | 75 | feed_type = "rss" |
paul@13 | 76 | |
paul@13 | 77 | # Detect items. |
paul@13 | 78 | |
paul@13 | 79 | elif feed_type == "rss" and tagname == "item" or \ |
paul@13 | 80 | feed_type == "atom" and tagname == "entry": |
paul@13 | 81 | |
paul@13 | 82 | in_item = True |
paul@13 | 83 | |
paul@13 | 84 | elif tagname == "title": |
paul@13 | 85 | events.expandNode(value) |
paul@13 | 86 | if in_item: |
paul@13 | 87 | title = value |
paul@13 | 88 | else: |
paul@13 | 89 | channel_title = value |
paul@13 | 90 | |
paul@13 | 91 | elif tagname == "link": |
paul@13 | 92 | events.expandNode(value) |
paul@13 | 93 | if in_item: |
paul@13 | 94 | link = value |
paul@13 | 95 | else: |
paul@13 | 96 | channel_link = value |
paul@13 | 97 | |
paul@13 | 98 | elif event == xml.dom.pulldom.END_ELEMENT: |
paul@13 | 99 | tagname = value.localName |
paul@13 | 100 | |
paul@13 | 101 | if feed_type == "rss" and tagname == "item" or \ |
paul@13 | 102 | feed_type == "atom" and tagname == "entry": |
paul@13 | 103 | |
paul@13 | 104 | in_item = False |
paul@13 | 105 | |
paul@13 | 106 | # Emit title and link information for items. |
paul@13 | 107 | |
paul@13 | 108 | if title and link and nentries < max_entries: |
paul@13 | 109 | link_text = linktext(link, feed_type) |
paul@13 | 110 | |
paul@13 | 111 | append(fmt.listitem(on=1)) |
paul@13 | 112 | append(fmt.url(on=1, href=link_text)) |
paul@13 | 113 | append(fmt.icon('www')) |
paul@13 | 114 | append(fmt.text(" " + text(title))) |
paul@13 | 115 | append(fmt.url(on=0)) |
paul@13 | 116 | append(fmt.listitem(on=0)) |
paul@13 | 117 | |
paul@13 | 118 | title = link = None |
paul@13 | 119 | nentries += 1 |
paul@13 | 120 | |
paul@13 | 121 | append(fmt.bullet_list(on=0)) |
paul@13 | 122 | |
paul@13 | 123 | if channel_title and channel_link: |
paul@13 | 124 | channel_link_text = linktext(channel_link, feed_type) |
paul@13 | 125 | |
paul@13 | 126 | append(fmt.paragraph(on=1)) |
paul@13 | 127 | append(fmt.url(on=1, href=channel_link_text)) |
paul@13 | 128 | append(fmt.text(text(channel_title))) |
paul@13 | 129 | append(fmt.url(on=0)) |
paul@13 | 130 | append(fmt.text(" ")) |
paul@13 | 131 | append(fmt.url(on=1, href=feed_url)) |
paul@13 | 132 | append(fmt.icon('rss')) |
paul@13 | 133 | append(fmt.url(on=0)) |
paul@13 | 134 | append(fmt.paragraph(on=0)) |
paul@13 | 135 | |
paul@13 | 136 | finally: |
paul@13 | 137 | feed.close() |
paul@13 | 138 | |
paul@13 | 139 | return ''.join(output) |
paul@13 | 140 | |
paul@13 | 141 | # vim: tabstop=4 expandtab shiftwidth=4 |