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"> 1 </font><font color="#87ceeb">#!/usr/bin/env python</font><br>
<font color="#ffff00"> 2 </font><br>
<font color="#ffff00"> 3 </font><font color="#cd5c5c">import</font> glib <font color="#87ceeb"># For glib main loop</font><br>
<font color="#ffff00"> 4 </font><font color="#cd5c5c">import</font> gtask <font color="#87ceeb"># Our tasking library</font><br>
<font color="#ffff00"> 5 </font><font color="#cd5c5c">import</font> rss <font color="#87ceeb"># For rss parsing</font><br>
<font color="#ffff00"> 6 </font><font color="#cd5c5c">import</font> urllib <font color="#87ceeb"># To download the url</font><br>
<font color="#ffff00"> 7 </font><br>
<font color="#ffff00"> 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"> 9 </font>loop = glib.MainLoop()<br>
<font color="#ffff00">10 </font><br>
<font color="#ffff00">11 </font><font color="#f0e68c"><b>def</b></font> <font color="#98fb98">parse</font>(data):<br>
<font color="#ffff00">12 </font> p = rss.Parser()<br>
<font color="#ffff00">13 </font> p.load_from_data(data, <font color="#98fb98">len</font>(data))<br>
<font color="#ffff00">14 </font> l = p.get_document().get_items()<br>
<font color="#ffff00">15 </font> l.reverse() <font color="#87ceeb"># order newest article first</font><br>
<font color="#ffff00">16 </font> <font color="#f0e68c"><b>return</b></font> l<br>
<font color="#ffff00">17 </font><br>
<font color="#ffff00">18 </font><font color="#f0e68c"><b>def</b></font> <font color="#98fb98">printit</font>(i):<br>
<font color="#ffff00">19 </font> <font color="#f0e68c"><b>print</b></font> <span style="background-color: #333333"><font color="#ffffff">'</font></span><font color="#ffa0a0">Title: %s</font><span style="background-color: #333333"><font color="#ffffff">'</font></span> % i.props.title<br>
<font color="#ffff00">20 </font> <font color="#f0e68c"><b>print</b></font> <span style="background-color: #333333"><font color="#ffffff">'</font></span><font color="#ffa0a0">Author: %s</font><span style="background-color: #333333"><font color="#ffffff">'</font></span> % i.props.author<br>
<font color="#ffff00">21 </font> <font color="#f0e68c"><b>print</b></font> <span style="background-color: #333333"><font color="#ffffff">'</font></span><font color="#ffa0a0">Date: %s</font><span style="background-color: #333333"><font color="#ffffff">'</font></span> % i.props.pub_date<br>
<font color="#ffff00">22 </font> <font color="#f0e68c"><b>print</b></font> <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> l: [printit(i) <font color="#f0e68c"><b>for</b></font> i <font color="#f0e68c"><b>in</b></font> 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> _: 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>
|