"Diary" インターネットさんへの恩返し

いつもソースコードコピペばかりなので,みなさまへ少しばかりの恩返しを

Raspberrypiで作るTweeトイ!(pythonでTweetを取得する方法)



スポンサーリンク

Raspberry Pi Type B 512MBTwitterのつぶやきを音声に変えるTOYを作ったぜ。デーブ・スペクターのtweetが面白いのでそれを取得してつぶやく。

完成(一応。。本ページに書いていないけど赤外線センサーをトリガーにして人が近づいたらつぶやきます。)

必要な道具

  1. Raspberry Pi Type B 512MB
  2. 無線LAN USBアダプタ GW-USNano2
  3. Raspberry Pi Type B ケース (Clear 透明) •••• お好みで
  4. 100均のスピーカー(音が小さいのが難点)
  5. 開発環境(パソコン )

1.事前準備

python 3.3.3で当初チャレンジしたが SimpleGeo's OAuth2 や OAuth2 をインストールすると以下のエラーがでて実装できなかったので2.xでやることにした。

python-oauth2-master>python setup.py install
  File "setup.py", line 18
    print "unable to find version in %s" % (VERSIONFILE,)
                                       ^
SyntaxError: invalid syntax

2.python2.7のインストール

インストーラのexeをダブルクリック。詳細なし。



3.python setuptoolのインストール(Raspbery piの場合不要)

python ez_setup.py install
・
・
・
Installed c:\python27\lib\site-packages\setuptools-2.0.1-py2.7.egg
Processing dependencies for setuptools==2.0.1
Finished processing dependencies for setuptools==2.0.1

4.関連モジュールのインストール

httplibのインスト-ル

$ wget https://httplib2.googlecode.com/files/httplib2-0.8.zip
$ unzip httplib2-0.8.zip
$ cd httplib2-0.8
$ sudo python setup.py install
・
・
・
Installed /usr/local/lib/python2.7/dist-packages/httplib2-0.8-py2.7.egg
Processing dependencies for httplib2==0.8
Finished processing dependencies for httplib2==0.8

simplejson-3.3.1のインスト-ル

以下のWaringが出たがよしとしよう。

$ git clone https://github.com/simplejson/simplejson
$ cd simplejson
$ sudo python setup.py install
・
・
・
running install_egg_info
Writing /usr/local/lib/python2.7/dist-packages/simplejson-3.3.1.egg-info
***************************************************************************
WARNING: The C extension could not be compiled, speedups are not enabled.
Plain-Python installation succeeded.
***************************************************************************

oauth2モジュールのインスト-ル

$ git clone https://github.com/simplegeo/python-oauth2
$ cd python-oauth2
$ sudo python setup.py install
・
・
・
Installed /usr/local/lib/python2.7/dist-packages/httplib2-0.8-py2.7.egg
Finished processing dependencies for oauth2==1.5.211

Twitterモジュールのインスト-ル

$ git clone https://github.com/bear/python-twitter
$ cd python-twitter-master 
$ sudo python setup.py build
running build
running build_py
creating build
creating build/lib.linux-armv6l-2.7
copying twitter.py -> build/lib.linux-armv6l-2.7
running egg_info
creating python_twitter.egg-info
writing requirements to python_twitter.egg-info/requires.txt
writing python_twitter.egg-info/PKG-INFO
writing top-level names to python_twitter.egg-info/top_level.txt
writing dependency_links to python_twitter.egg-info/dependency_links.txt
writing manifest file 'python_twitter.egg-info/SOURCES.txt'
reading manifest file 'python_twitter.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
no previously-included directories found matching '.DS_Store'
writing manifest file 'python_twitter.egg-info/SOURCES.txt'


インストール。

$ python setup.py install
・
・
・
Using /usr/local/lib/python2.7/dist-packages
Searching for setuptools==1.1.7
Best match: setuptools 1.1.7
Processing setuptools-1.1.7-py2.7.egg
setuptools 1.1.7 is already the active version in easy-install.pth
Installing easy_install script to /usr/local/bin
Installing easy_install-2.7 script to /usr/local/bin

Using /usr/local/lib/python2.7/dist-packages/setuptools-1.1.7-py2.7.egg
Finished processing dependencies for python-twitter==1.1

5.プログラム作成

run.py(ファイル名はなんでも可)で以下のプログラムを記載し保存。ツートを取得して、ファイルに出力。これをAquesTalkpiに渡して喋らせる。

# coding:utf-8

import twitter
import os

api = twitter.Api(consumer_key='カスタマーキー',
                  consumer_secret='カスタマシークレットキー',
                  access_token_key='トークンキー',
                  access_token_secret='トークンシークレットキー')

f = open('text.txt', 'w') # open file write mode

statuses = api.GetUserTimeline(screen_name='@dave_spector')

#print get latest tweet.
print statuses[0].text + '\n'
text = statuses[0].text.encode('utf-8')  #utf-8 encodeしないと"【" や "】"が入っているときに

message = "/home/pi/aquestalkpi/AquesTalkPi " + '"' + text  + '"' +" | aplay"
print message
os.system(message)   #aquesTalkPi実行

その後、プロンプトで以下を実行(こんなかんじで出る)

python run.py

【速報】東京都が政治資金がきちんと入る鞄を携帯することを条例化へ。

/home/pi/aquestalkpi/AquesTalkPi "【速報】東京都が政治資金がきちんと入る鞄を携帯することを条例化へ。" | aplay
再生中 WAVE 'stdin' : Signed 16 bit Little Endian, レート 8000 Hz, モノラル