1 #!/usr/bin/env python
 2
 3 import glib    # For glib main loop
 4 import gtask   # Our tasking library
 5 import rss     # For rss parsing
 6 import urllib  # To download the url
 7
 8 url = 'http://audidude.com/blog/?feed=rss2'
 9 loop = glib.MainLoop()
10
11 def parse(data):
12     p = rss.Parser()
13     p.load_from_data(data, len(data))
14     l = p.get_document().get_items()
15     l.reverse() # order newest article first
16     return l
17
18 def printit(i):
19     print 'Title:   %s' % i.props.title
20     print 'Author:  %s' % i.props.author
21     print 'Date:    %s' % i.props.pub_date
22     print ''
23
24 # download the url as an async task
25 task = gtask.Task(lambda: urllib.urlopen(url).read())
26
27 # process the content into a list of rss articles.
28 # the result of the task will be the first argument to parse().
29 task.add_callback(parse)
30
31 # print each article to the command line
32 task.add_callback(lambda l: [printit(i) for i in l])
33
34 # quit the main loop when we are done
35 task.add_callback(lambda _: loop.quit())
36
37 # schedule the task for execution
38 gtask.schedule(task)
39
40 loop.run()