2011年11月28日月曜日

[Python][Mining the Social Web]Twitter APIを使用してデータを集める

Twitter APIを使用するためのPythonパッケージを追加し、実際にTwitterデータにアクセスしてみる。

$ sudo easy_install twitter
Searching for twitter
Reading http://pypi.python.org/simple/twitter/
Reading http://mike.verdone.ca/twitter/
Best match: twitter 1.7.2
Downloading http://pypi.python.org/packages/source/t/twitter/twitter-1.7.2.tar.gz#md5=a81b512cc8be5ba278bdaceb6d988fe6
Processing twitter-1.7.2.tar.gz
Running twitter-1.7.2/setup.py -q bdist_egg --dist-dir /tmp/easy_install-1iZuPg/twitter-1.7.2/egg-dist-tmp-7cd07T
Adding twitter 1.7.2 to easy-install.pth file
Installing twitter-stream-example script to /usr/local/bin
Installing twitter script to /usr/local/bin
Installing twitterbot script to /usr/local/bin
Installing twitter-log script to /usr/local/bin


Installed /Library/Python/2.7/site-packages/twitter-1.7.2-py2.7.egg
Processing dependencies for twitter
Finished processing dependencies for twitter


パッケージドキュメントを読むためにはpydocを実行する。
*nix : pydoc twitter.Twitter
(Windows : python -mpydoc twitter.Twitter)

TwitterのトレンドAPIを実行してみるが、エラー発生。
$ python
Python 2.7.1 (r271:86832, Jun 25 2011, 05:09:01)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import twitter
>>> twitter_api = twitter.Twitter(domain="api.twitter.com", api_version='1')
>>> trends = twitter_api.trends()
Traceback (most recent call last):
File "", line 1, in
File "build/bdist.macosx-10.7-intel/egg/twitter/api.py", line 165, in __call__
File "build/bdist.macosx-10.7-intel/egg/twitter/api.py", line 180, in _handle_response
twitter.api.TwitterHTTPError: Twitter sent status 404 for URL: 1/trends.json using parameters: ()
details:

はて、404エラー出てるな。
とりあえず、本文に記載のURL:http://search.twitter.com/1/trends.jsonを入力してみるがこちらもエラー。
よく見ると、このURLってhttp指定してるけど、Twitterってhttps使うように変わったような...

と、いうことでTwitter API のドキュメントを確認してみる。
https://dev.twitter.com/docs/api/1/get/trends/%3Awoeid

やっぱり、API仕様変わってますね。ということで、モジュール修正が必要だな。

[Music] 2011年11月購入リスト


11月の購入リストを残す。

・Derrick May / Heartbeat Presents Mixed By Derrick May×AIR Vol.2

Derrick May/代官山AIRコラボMixの第2弾。
今作も一切手抜きなし。これほどクラブにいる感覚を味合わせてくれるMix作れるのは「Derrick May」だから。


・Surkin / USA

「Action Replay」から4年、待ちに待った作品。
フレンチエレクトロと言えば、「Justice」じゃなくて「Surkin」ですね。個人的には。 カッコいいです。



・DJ Shadow / The Less You Know The Better

ジャケがお茶目です。原点回帰とか言われてるのは、正直わからんですが、いいものはいいと。

Little Dragonとのコラボ曲(Scale It Back)とか嬉しいし、すばらしい作品です。




コメント: 12/2は遂にNujabes新譜発売ですね。

[Python][Mining the Social Web] easy_installでモジュール追加

Mining the Social Web(オライリー刊)を参考に分析を進めていくための準備

Pythonでモジュール管理はeasy_installを使用するのがベター。
Macでも変わらない。

NetworkXモジュールはグラフを作り、分析し、類似する出力を観察するために使用するようだ。
$ sudo easy_install networkx
Searching for networkx
Reading http://pypi.python.org/simple/networkx/
Reading http://networkx.lanl.gov/
Reading http://networkx.lanl.gov/download/networkx
Reading http://sourceforge.net/project/showfiles.php?group_id=122233
Reading https://networkx.lanl.gov
Reading http://networkx.lanl.gov/download
Reading http://networkx.lanl.gov
Best match: networkx 1.6
Downloading http://networkx.lanl.gov/download/networkx/networkx-1.6-py2.7.egg
Processing networkx-1.6-py2.7.egg
creating /Library/Python/2.7/site-packages/networkx-1.6-py2.7.egg
Extracting networkx-1.6-py2.7.egg to /Library/Python/2.7/site-packages
Adding networkx 1.6 to easy-install.pth file

Installed /Library/Python/2.7/site-packages/networkx-1.6-py2.7.egg
Processing dependencies for networkx
Finished processing dependencies for networkx

Numpyは科学計算用ツール。NetworkXが依存関係にある。
$ sudo easy_install numpy
Searching for numpy
Best match: numpy 1.5.1
Adding numpy 1.5.1 to easy-install.pth file

Using /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
Processing dependencies for numpy
Finished processing dependencies for numpy

NoseはPythonのテストツール
$ sudo easy_install nose
Searching for nose
Reading http://pypi.python.org/simple/nose/
Reading http://somethingaboutorange.com/mrl/projects/nose/
Reading http://readthedocs.org/docs/nose/
Best match: nose 1.1.2
Downloading http://pypi.python.org/packages/source/n/nose/nose-1.1.2.tar.gz#md5=144f237b615e23f21f6a50b2183aa817
Processing nose-1.1.2.tar.gz
Running nose-1.1.2/setup.py -q bdist_egg --dist-dir /tmp/easy_install-UusYYV/nose-1.1.2/egg-dist-tmp-UafvfE
Adding nose 1.1.2 to easy-install.pth file
Installing nosetests script to /usr/local/bin
Installing nosetests-2.7 script to /usr/local/bin

Installed /Library/Python/2.7/site-packages/nose-1.1.2-py2.7.egg
Processing dependencies for nose
Finished processing dependencies for nose


以上です。