Skip to content

Instantly share code, notes, and snippets.

@yatt
Created June 25, 2011 17:48
Show Gist options
  • Select an option

  • Save yatt/1046706 to your computer and use it in GitHub Desktop.

Select an option

Save yatt/1046706 to your computer and use it in GitHub Desktop.
ガラケーでfavstar.fmのrecentを見る
#!/usr/bin/python2.6
# coding: utf-8
import cgi
import re
import urllib
from BeautifulSoup import BeautifulSoup as bs
def conv(txt):
lst = []
for n in txt.contents:
if isinstance(n, unicode):
n = re.sub('(https?://[\w./]+)', '<a href="\\1">\\1</a>', n)
lst.append(n)
else:
lst.extend(conv(n))
return ''.join(lst)
def favstar(username):
url = 'http://favstar.fm/users/%s/recent' % username
soup = bs(urllib.urlopen(url).read())
tweets = soup.findAll('div', {'class': 'tweetWithStats'})
ret = []
for tw in tweets:
txt = tw.find('div', 'theTweet')
txt = conv(txt)
lst = tw.findAll('div', {'class': 'avatarList'})
fav = []
rt = []
for child in lst:
if child.attrMap['id'].startswith('fav'):
fav = map(lambda n: n.attrMap['title'], child.findAll('a', {'class': 'avatar'}))
if child.attrMap['id'].startswith('rt'):
rt = map(lambda n: n.attrMap['title'], child.findAll('a', {'class': 'avatar'}))
ret.append({'txt': txt, 'fav': fav, 'rt': rt})
return ret
def main():
fs = cgi.FieldStorage()
name = fs.getvalue('name', None)
if name is None:
return
print 'Content-Type: text/html; charset=utf-8'
print ''
print '<html><head><title>favstar recent</title></head><body>'
for stat in favstar(name):
print '<font color="red">txt:</font>', stat['txt'], '<br/>'
print '<font color="green">fav: (%d)</font>' % len(stat['fav']), ','.join(stat['fav']), '<br/>'
print '<font color="blue">rt : (%d)</font>' % len(stat['rt']), ','.join(stat['rt']), '<br/>'
print '<br/>'
print '</body></html>'
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment