File: rss_reader.py.html

package info (click to toggle)
gtask 0.1.2-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 2,360 kB
  • ctags: 570
  • sloc: sh: 9,649; ansic: 2,109; xml: 913; makefile: 218; python: 86
file content (42 lines) | stat: -rw-r--r-- 4,808 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<div style="background:#333333;color:#ffffff;"><font face="monospace">
<font color="#ffff00">&nbsp;1 </font><font color="#87ceeb">#!/usr/bin/env python</font><br>
<font color="#ffff00">&nbsp;2 </font><br>
<font color="#ffff00">&nbsp;3 </font><font color="#cd5c5c">import</font>&nbsp;glib&nbsp;&nbsp;&nbsp;&nbsp;<font color="#87ceeb"># For glib main loop</font><br>
<font color="#ffff00">&nbsp;4 </font><font color="#cd5c5c">import</font>&nbsp;gtask&nbsp;&nbsp; <font color="#87ceeb"># Our tasking library</font><br>
<font color="#ffff00">&nbsp;5 </font><font color="#cd5c5c">import</font>&nbsp;rss&nbsp;&nbsp;&nbsp;&nbsp; <font color="#87ceeb"># For rss parsing</font><br>
<font color="#ffff00">&nbsp;6 </font><font color="#cd5c5c">import</font>&nbsp;urllib&nbsp;&nbsp;<font color="#87ceeb"># To download the url</font><br>
<font color="#ffff00">&nbsp;7 </font><br>
<font color="#ffff00">&nbsp;8 </font>url = <span style="background-color: #333333"><font color="#ffffff">'</font></span><font color="#ffa0a0"><a href="http://audidude.com/blog/?feed=rss2">http://audidude.com/blog/?feed=rss2</a></font><span style="background-color: #333333"><font color="#ffffff">'</font></span><br>
<font color="#ffff00">&nbsp;9 </font>loop = glib.MainLoop()<br>
<font color="#ffff00">10 </font><br>
<font color="#ffff00">11 </font><font color="#f0e68c"><b>def</b></font>&nbsp;<font color="#98fb98">parse</font>(data):<br>
<font color="#ffff00">12 </font>&nbsp;&nbsp;&nbsp;&nbsp;p = rss.Parser()<br>
<font color="#ffff00">13 </font>&nbsp;&nbsp;&nbsp;&nbsp;p.load_from_data(data, <font color="#98fb98">len</font>(data))<br>
<font color="#ffff00">14 </font>&nbsp;&nbsp;&nbsp;&nbsp;l = p.get_document().get_items()<br>
<font color="#ffff00">15 </font>&nbsp;&nbsp;&nbsp;&nbsp;l.reverse() <font color="#87ceeb"># order newest article first</font><br>
<font color="#ffff00">16 </font>&nbsp;&nbsp;&nbsp;&nbsp;<font color="#f0e68c"><b>return</b></font>&nbsp;l<br>
<font color="#ffff00">17 </font><br>
<font color="#ffff00">18 </font><font color="#f0e68c"><b>def</b></font>&nbsp;<font color="#98fb98">printit</font>(i):<br>
<font color="#ffff00">19 </font>&nbsp;&nbsp;&nbsp;&nbsp;<font color="#f0e68c"><b>print</b></font>&nbsp;<span style="background-color: #333333"><font color="#ffffff">'</font></span><font color="#ffa0a0">Title:&nbsp;&nbsp; %s</font><span style="background-color: #333333"><font color="#ffffff">'</font></span>&nbsp;% i.props.title<br>
<font color="#ffff00">20 </font>&nbsp;&nbsp;&nbsp;&nbsp;<font color="#f0e68c"><b>print</b></font>&nbsp;<span style="background-color: #333333"><font color="#ffffff">'</font></span><font color="#ffa0a0">Author:&nbsp;&nbsp;%s</font><span style="background-color: #333333"><font color="#ffffff">'</font></span>&nbsp;% i.props.author<br>
<font color="#ffff00">21 </font>&nbsp;&nbsp;&nbsp;&nbsp;<font color="#f0e68c"><b>print</b></font>&nbsp;<span style="background-color: #333333"><font color="#ffffff">'</font></span><font color="#ffa0a0">Date:&nbsp;&nbsp;&nbsp;&nbsp;%s</font><span style="background-color: #333333"><font color="#ffffff">'</font></span>&nbsp;% i.props.pub_date<br>
<font color="#ffff00">22 </font>&nbsp;&nbsp;&nbsp;&nbsp;<font color="#f0e68c"><b>print</b></font>&nbsp;<span style="background-color: #333333"><font color="#ffffff">''</font></span><br>
<font color="#ffff00">23 </font><br>
<font color="#ffff00">24 </font><font color="#87ceeb"># download the url as an async task</font><br>
<font color="#ffff00">25 </font>task = gtask.Task(<font color="#f0e68c"><b>lambda</b></font>: urllib.urlopen(url).read())<br>
<font color="#ffff00">26 </font><br>
<font color="#ffff00">27 </font><font color="#87ceeb"># process the content into a list of rss articles.</font><br>
<font color="#ffff00">28 </font><font color="#87ceeb"># the result of the task will be the first argument to parse().</font><br>
<font color="#ffff00">29 </font>task.add_callback(parse)<br>
<font color="#ffff00">30 </font><br>
<font color="#ffff00">31 </font><font color="#87ceeb"># print each article to the command line</font><br>
<font color="#ffff00">32 </font>task.add_callback(<font color="#f0e68c"><b>lambda</b></font>&nbsp;l: [printit(i) <font color="#f0e68c"><b>for</b></font>&nbsp;i <font color="#f0e68c"><b>in</b></font>&nbsp;l])<br>
<font color="#ffff00">33 </font><br>
<font color="#ffff00">34 </font><font color="#87ceeb"># quit the main loop when we are done</font><br>
<font color="#ffff00">35 </font>task.add_callback(<font color="#f0e68c"><b>lambda</b></font>&nbsp;_: loop.quit())<br>
<font color="#ffff00">36 </font><br>
<font color="#ffff00">37 </font><font color="#87ceeb"># schedule the task for execution</font><br>
<font color="#ffff00">38 </font>gtask.schedule(task)<br>
<font color="#ffff00">39 </font><br>
<font color="#ffff00">40 </font>loop.run()<br>
</font></div>