<div>Hi,</div>
<div>&nbsp;&nbsp;I have been follow twisted for like half a year now, I have read the doc and the oreilly book, but i still cann't do some simple work in the twisted way. Could someone give me a hint?</div>
<div>&nbsp; Like today, i write this little code to follow my friends list on my msn space, and get their friends list, so on and so on, and maybe at last I can build a social relationship graph with it. The code is actually&nbsp;really navie, here it goes. Sorry for the ugliness of my code, please help me make it prettier:)
</div>
<div>&nbsp; Thanks. Appologize for my poor English.</div>
<div>Jia Liu</div>
<div>
<p>import re, sys, socket<br>import urllib<br>from sets import Set</p>
<p>socket.setdefaulttimeout = 10<br>re_buddy_pre = re.compile(&quot;href=\&quot;(?:http://)?([^\.\&quot;]+)\.spaces.msn.com&quot;, re.IGNORECASE)&nbsp; # <a href="http://nick.spaces.msn.com/">http://nick.spaces.msn.com</a><br>
re_buddy_post = re.compile(&quot;<a href="http://spaces.msn.com/members/([^\">spaces.msn.com/members/([^\</a>&quot;\/]+)&quot;, re.IGNORECASE) # <a href="http://spaces.msn.com/members/nick/">http://spaces.msn.com/members/nick/
</a> </p>
<p>all_buddies = Set()<br>relation_dict = {}</p>
<p>def get_buddy(page, buddy=None):<br>&nbsp;&nbsp;&nbsp; #print page<br>&nbsp;&nbsp;&nbsp; print buddy<br>&nbsp;&nbsp;&nbsp; global all_buddies<br>&nbsp;&nbsp;&nbsp; buddies_of_this_page = Set()<br>&nbsp;&nbsp;&nbsp; for re_buddy in [re_buddy_pre, re_buddy_post]:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for m in re_buddy.finditer(page):
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; buddies_of_this_page.add(m.group(1))<br>&nbsp;&nbsp;&nbsp; relation_dict[buddy] = buddies_of_this_page<br>&nbsp;&nbsp;&nbsp; return buddies_of_this_page</p>
<p>def got(url, buddy=None):<br>&nbsp;&nbsp;&nbsp; f = urllib.urlopen(url)<br>&nbsp;&nbsp;&nbsp; return f.read(), buddy<br>&nbsp;&nbsp;&nbsp; <br>URL = &quot;<a href="http://ayueer.spaces.msn.com/">http://ayueer.spaces.msn.com/</a>&quot;<br>(page, buddy) = got(URL, &quot;ayueer&quot;)
</p>
<p>buddies_of_this_page = get_buddy(page, buddy)<br>all_buddies.add(&quot;ayueer&quot;)<br>next_to_visit = buddies_of_this_page.difference(all_buddies)</p>
<p>try:<br>&nbsp;&nbsp;&nbsp; while 1:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #reactor.run()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #multi_buddy_factory(buddies_of_this_page)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for buddy in next_to_visit:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; url = &quot;<a href="http://&quot;+buddy+&quot;.spaces.msn.com/">
http://&quot;+buddy+&quot;.spaces.msn.com/</a>&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (page, buddy) = got(url, buddy)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; buddies_of_this_page.update(get_buddy(page, buddy))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; except:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pass<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; all_buddies.update(next_to_visit)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; next_to_visit = buddies_of_this_page.difference(all_buddies)<br>except KeyboardInterrupt:<br>&nbsp;&nbsp;&nbsp; print all_buddies</p><br clear="all"><br>-- <br>
ÒøóÝÒ¹¾ÃÒóÇÚŪ£¬ÐÄÇÓ¿Õ·¿²»È̹é </div>