まずpython-twitter
http://code.google.com/p/python-twitter/
python-twitterを使うには以下のモジュールが必要。
http://cheeseshop.python.org/pypi/simplejson http://code.google.com/p/httplib2/ http://github.com/simplegeo/python-oauth2
さらに、python-oauth2をインストールするには setuptoolsというモジュールが必要。
http://pypi.python.org/pypi/setuptools#files
必要なモジュールがそろったら、以下の順にインストールする。
setuptools→ simplejson, httplib2, oauth2 → python-twitter
setuptoolsはwindows用のインストーラがある。
次の3つのモジュールは皆、解凍したフォルダの中で以下のコマンドでインストールできる。
python setup.py install
python-twitterは、以下のようにbuild と installが必要。
C:\python-twitter-0.8.1>python setup.py build C:\python-twitter-0.8.1>python setup.py install
・・・やっぱりperlのCPANみたいに楽ではない。
<サンプル>
import twitter
api = twitter.Api()
statuses = api.GetPublicTimeline()
print '<html><head><title>tl</title><meta charset="utf-8"></head>'
print "<body>"
for s in statuses:
print "<p>"
print "<b>" + s.GetUser().GetScreenName()+ "</b><br>",
s.GetText().encode("utf-8","replace") + "<br>"
print "</p>"
print "</body></html>"
python twi.py > tl.html
とやってブラウザで見てください。
tweetするやつ。
# -*- coding: utf-8 -*-
import twitter
from urllib import urlencode
from oauth2 import Client, Consumer, Token
account_name = 'your_account_name'
account_pass = 'your_password'
consumer_key = 'your_consumer_key'
consumer_secret = 'your_consumer_secret'
access_token_key = 'your_access_token_key'
access_token_secret = 'your_access_token_secret'
client = Client(Consumer(consumer_key, consumer_secret),
Token(access_token_key, access_token_secret))
client.request('http://api.twitter.com/1/statuses/update.xml',
'POST',
urlencode({'status':'GOOD MORNING'}))