今日のTwitter

30分プログラム、その57。TwitterPythonで遊ぼう。

今日のTwitterのログを出力してみる。

$ python twitter.py
11:32 mzp: Twitter with Pythonでなにかつくろう L:家 *p
12:35 mzp: localtimeの逆はmktime。gmtimeの逆をよこせ L:家 *p
12:35 mzp: タイムゾーンなんてなければいいのに L:家 *p
12:36 mzp: TwitterPodっぽいところを消してみた
import rssparser
from time import *
import email.Utils
import re
 
URL='http://twitter.com/statuses/user_timeline/3991031.rss'
 
def entity(s):
    p = re.compile(r'&#(\d+);')
    
    match = p.search(s)
    while match:
	s =  s.replace(match.group(0),
		       unicode('\\u%04x' % eval(match.group(1)), 'unicode-escape'))
	match = p.search(s)
    return s
 
items = rssparser.parse(URL)['items']
items.reverse()
 
today = localtime(time())[0:3]
for item in items:
    time = localtime(
	mktime(email.Utils.parsedate(item['date'])) + 9 * 60 * 60)
    if time[0:3] == today:
	print "%s %s" % (strftime('%H:%M',time),entity(item['description']))
  • 時間の扱いにはまった
    • RFC 2822(e.g. Sat Jun 16 12:25:54 +0000 2007)はemail.Utils.parsedateでパースできる。返すのはUTC
    • mktimeはパースされた時間をUnix時間に変換する。ただし、時間をローカル時間として扱う
  • だれか各言語の時間の扱いについてまとめて