The idea is a Macro for displaying RSS news feed on a wikipage. For MoinMoin only exists a macro for the reverse way, i. e. making a feed from a wikipage (see IRSS.py). (Similar is this: http://openwiki.com/ow.asp?RecentChangesSites)

example:

[[FeedParser(URL)]]

should deliver something like this (in HTML):

Logo, Title of the channel
 * Title of Item1 (with link), description
 * Title of Item2 ...
 * ...

Aditional parameters could be given, i. g.

A timeout would be nice, something like "no feed available at the moment" could be shown then.

This should be usefull: http://www.python.org/moin/RssLibraries

Code

First try, for a demo see http://zosel.dyndns.org/testwiki/FeedParser

Tips for better python coding are welcome. :)

import feedparser
Dependencies = []
def execute(macro, args):
    url = args
    feed = feedparser.parse(url )

    output = "<h1><a href=\"" + feed[ "channel" ][ "link" ] + "\">" + feed[ "channel" ][ "title" ] + "</a></h1>"

    items = feed[ "items" ]

    for item in items:
        output = output + "\n<p><a href=\""+ item[ "link" ] + "\">"+ item[ "title" ] + "</a><br />"
        output = output + "\n"+ item[ "summary" ] + "</p>"

    return output

Comments

When I ran the above FeedParser macro on some feeds it crashed. Here is my suggestion for making it a little more resilient:

import feedparser
Dependencies = []
def execute(macro, args):
    url = args
    feed = feedparser.parse(url )
    if feed.has_key("channel"):
       mylink = feed["channel"].get("link", "No link")
       mytitle = feed["channel"].get("title", "No title")
       output = "<h1><a href=\"" + mylink + "\">" + feed.version + " Feed: " + mytitle + "</a></h1>"
    else:
       output = "<h1>" + feed.version + " Feed: No channel</h1>"
    if feed.has_key("items"):
       items = feed["items"]
       for item in items:
          mytitle = item.get("title", "")
          mysummary = item.get("summary", "")
          if item.has_key("link"):
             output = output + "\n<p><a href=\""+ item["link"] + "\">"+ mytitle + "</a>"
          else:
             output = output + "\n<p>" + mytitle
          output = output + " "+ mysummary + "</p>"
    else:
       output = output + "\n<p>No items</p>"
    return output

-- BarryCornelius 2005-04-27 15:18:59

One problem with the above code is that the page is cached after the contents of the newsfeed has been obtained. So any updates to the newsfeed will not easily be seen. The code of the rival RSSReader macro contains:

# this tells MoinMoin not to cache the page, as we don't know when it
# changes.
Dependencies = ["time"]

So I've changed line 3 of our FeedParser.py file from:

Dependencies = []

to:

Dependencies = ["time"]

Now this page is no longer cached.

-- BarryCornelius 2006-08-04 13:32:34

MoinMoin: macro/FeedParser (last edited 2007-10-29 19:11:00 by localhost)