# Create links based on Google blog feed results
# Jason McArthur <jason@personaltelco.net>
# Set the url variable below to match your search criteria
# Usage: <<GoogleNews>>

Dependencies = ["time"]

from MoinMoin import util, wikiutil, config
from MoinMoin.Page import Page

class RSStoWiki:
    def __init__(self, macro):
        self.macro = macro
        self.fmt = macro.formatter
        # google blog search url. 10 items, sorted by date 
	url = "http://blogsearch.google.com/blogsearch_feeds?hl=en&client=news&scoring=d&q=%22personal+telco%22&ie=utf-8&num=10&output=rss"
        import feedparser
        self.f = feedparser.parse(url)
        self.result = []

    def get_link(self, link):
        self.result.append(self.fmt.url(on=1, href=link) + \
                           self.fmt.icon('www'))

    def get_entry_header(self, title):
        self.result.append(self.fmt.rawHTML(" "+title) + \
			   self.fmt.url(on=0) + \
			   self.fmt.rawHTML('<br>'))

    def get_entries(self):
        for entry in self.f.entries:
            if entry.has_key('link'):
                self.get_link(entry.link)
            if entry.has_key('title'):
                self.get_entry_header(entry.title)

    def get_output(self):
	self.get_entries()
        return ''.join(self.result)

def execute(macro, args):
      rss = RSStoWiki(macro)
      return rss.get_output()
