Goal
- コマンドラインから特定アカウントにツイートさせる
- 標準出力に特定アカウントのTLをリアルタイム(仮)表示させる
Log
まずはパッケージのインストール
% python --version Python 2.6.6 % pip --version pip 1.3.1 from /usr/local/lib/python2.6/dist-packages/pip-1.3.1-py2.6.egg (python 2.6) % pip list distribute (0.6.14) Django (1.2.3) gevent (0.13.8) gevent-websocket (0.3.6) greenlet (0.4.1) Jinja2 (2.7) Juno (0.1.2) MarkupSafe (0.18) MySQL-python (1.2.2) python-apt (0.7.100.1-squeeze1) reportbug (4.12.6) requests (1.2.3) SQLAlchemy (0.8.2) wsgiref (0.1.2) % pip search twitter | grep python tweepy - Twitter library for python pytwitter - python library for the twitter api python-twitter - A Python wrapper around the Twitter API twitter-text-python - Twitter Tweet parser and formatter tropo-webapi-python - Python library for building voice/SMS/IM/Twitter apps at Tropo.com pwytter - A python client for Twitter pyfatcache - a simple python client for twitters fatcache tweetbot - A python powered twitter library. tweethon - A python wrapper around the Twitter API. Updated often and drop in replacement for twitter-python (aka its a fork) oauth-python-twitter - OAuth implementation for python-twitter % # ここの、python-twitterかな # oauth2入れてないけど、ビルトインなのかな # とりあえずpython-twitterをインストールしよう % sudo pip install python-twitter [sudo] password for otiai10: Downloading/unpacking python-twitter Downloading python-twitter-1.0.tar.gz (101kB): 101kB downloaded Running setup.py egg_info for package python-twitter no previously-included directories found matching '.DS_Store' Requirement already satisfied (use --upgrade to upgrade): setuptools in /usr/local/lib/python2.6/dist-packages/setuptools-0.6c11-py2.6.egg (from python-twitter) Downloading/unpacking simplejson (from python-twitter) Downloading simplejson-3.3.0.tar.gz (67kB): 67kB downloaded Running setup.py egg_info for package simplejson Downloading/unpacking oauth2 (from python-twitter) Downloading oauth2-1.5.211.tar.gz Running setup.py egg_info for package oauth2 Downloading/unpacking httplib2 (from oauth2->python-twitter) Downloading httplib2-0.8.tar.gz (110kB): 110kB downloaded Running setup.py egg_info for package httplib2 Installing collected packages: python-twitter, simplejson, oauth2, httplib2 Running setup.py install for python-twitter no previously-included directories found matching '.DS_Store' Running setup.py install for simplejson building 'simplejson._speedups' extension gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.6 -c simplejson/_speedups.c -o build/temp.linux-i686-2.6/simplejson/_speedups.o gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions build/temp.linux-i686-2.6/simplejson/_speedups.o -o build/lib.linux-i686-2.6/simplejson/_speedups.so Running setup.py install for oauth2 Running setup.py install for httplib2 Successfully installed python-twitter simplejson oauth2 httplib2 Cleaning up... % # oauth2も同時に入ったっぽいな # 確認 % pip list | grep twitter python-twitter (1.0) % # これTwitterAPI 1.1 対応してんのかな
とりあえず対話で確認
% python Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40) [GCC 4.4.5] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import python-twitter File "<stdin>", line 1 import python-twitter ^ SyntaxError: invalid syntax # (;^ω^)っ https://github.com/bear/python-twitter/blob/master/README.md 読め俺 >>> import twitter >>> api = twitter.Api() >>> >>> exit()
お、何か簡単そうだ。ミニマムなアプリケーションを書く
構成
. |-- minimum.py `-- system |-- __init__.py `-- conf.py 1 directory, 3 files
minimum.py
# basic from datetime import * import time # this topic import twitter # my from system import conf api = twitter.Api( consumer_key = conf.consumer_key, consumer_secret = conf.consumer_secret, access_token_key = conf.access_token_key, access_token_secret = conf.access_token_secret, ) # This is confirmation of App Verification # print api.VerifyCredentials() def tweet_test(): timestamp = time.mktime(datetime.now().timetuple()) status = api.PostUpdate("Hi, This is test tweet using python-twitter!! and TS = %s" % str(timestamp)) print "Tweet Post result :\t%s" % status.text def timeline_test(): statuses = api.GetUserTimeline( screen_name='hisyotan' ) print '>>> TimeLine >>>' for s in statuses: print s.text.encode('utf8') if __name__ == '__main__': tweet_test() timeline_test()
system/conf.py
# https://dev.twitter.com/ ここでアプリケーション登録したときにもらえるキーとシークレット # consumer_なんちゃら : 登録したアプリであることを示すキーとシークレット # access_token_なんちゃら : 紐づいたユーザであることを示すキーとシークレット consumer_key = 'XXXXXXXXXXXXXXXXXXX' consumer_secret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' access_token_key = 'zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz' access_token_secret = 'zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz'
で、実行する
% python -B minimum.py # ちなみに-Bするとモジュールimportするときにpycファイルを作らない
結果
ツイート、及びタイムライン取得できてるっぽい