携帯メールからTwitterしてみる
日本の一般的な携帯電話だとSMSが使えないので外出先で気軽にTwitterできない。これじゃあ全然Twitterの良さが発揮できないので、携帯からメールでPOST出来る様にしてみた。
仕組みも内容もシンプルで、書いたコードも20行弱。
Twitter用のメールアドレス(twitter@DOMAIN.COMとか)を、下のPythonスクリプトにパイプしておいて、そのアドレスへ本文にTwitterしたいテキストを書いて送るだけ。
ex.「/etc/alias」
ex.「/home/masashi/pyTwitter/pyTwitter.py」
from email.Header import Header, decode_header
import twitter, sys, email
uid = "ACCOUNT"
pw = "PASSWORD"
data = ""
for line in sys.stdin:
data = data + line
mail = email.message_from_string(data)
for part in mail.walk():
type = part.get_content_maintype()
if type and type.find("text") != -1:
enc = part.get_charsets()[0] or "us-ascii"
message = unicode(part.get_payload(), enc, "ignore")
api = twitter.Api()
status = api.PostUpdate(uid, pw, message)
Twitterは早くからAPIが公開されていて、公式からはFlashのAS2やAS3用のwrapperがダウンロードできるが、今回調べてみたら早くもPython用のwrapperを作っている人をGoogle Codeで発見。おかげですげーらくちんでした。感謝。
当然ですが、python-twitterのインストールが必要です。
Twitter
http://twitter.com/
python-twitter - Google Code
http://code.google.com/p/python-twitter/
Python
http://www.python.org/
Posted by masashi at 01:32
Trackback URL: http://mojo.jp/cgi-bin/mt.3.11/mt-tb.cgi/259