TWEEPY(2)

つづき。

Pythonでtwitterを扱うライブラリは何種類か存在しているが、ここでは tweepy を使用する。最初に試してみたのがたまたまこれだったというだけで、特に理由があって選択したわけでは無い。あえて言うならpipで簡単にインストールできたから、というのが理由。

Raspberry Piにpipがインストール済みであれば

$ pip install tweepy

でインストールできる。
pipがインストールされていない場合は、その前に

$ easy_install pip

でインストールしておく。
easy_installもインストールされていない場合は、さらにその前に

$ sudo apt-get install python-setuptools

でインストールしておく必要がある。

tweepyが正常にインストールできたかどうかは、シェルでpythonを起動して「import tweepy」と入力すれば良い。何の警告も出なければ正常。

tweepyを使用して単純にツイートさせるプログラムは、以下のようになる。

#!/usr/bin/env python

import tweepy                                                               
import datetime
import locale

consumer_key = "xxxxxxxxxxxxx"
consumer_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
access_key = "xxxxxxxxxxxxxxxxxxxx"
access_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)                   
auth.set_access_token(access_key, access_secret)                            
api = tweepy.API(auth_handler=auth)                                         

date = datetime.datetime.today()

api.update_status(date.strftime("%Y-%m-%d %H:%M:%S"))    

consumer_key 、consumer_secret 、access_key、access_secretには取得済みのコードをそのまま記述。実行すると以下のように現在時刻がツイートされるはず。

tweet1

これだけでは面白くないので、 少し手を加えてI2C温度センサーからのデータをツイートさせてみる。プログラムは以下の通り。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
  
import smbus
import time
import tweepy                                                               
import datetime
import locale

class i2c:
    def __init__(self, bus, addr):
        self.b = smbus.SMBus(bus)
        self.addr = addr
    def put(self, cmd, data):
        self.b.write_byte_data(self.addr, cmd, data)
    def get(self, cmd):
        return self.b.read_byte_data(self.addr, cmd)
    def getblock(self, cmd, len):
        return self.b.read_i2c_block_data(self.addr, cmd, len)

class tweet:
    def __init__(self):
        self.consumer_key = "xxxxxxxxxxxxxxxxxxxxxxx"
        self.consumer_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
        self.access_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
        self.access_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    def tweet(self, msg):
        auth = tweepy.OAuthHandler(self.consumer_key, self.consumer_secret)                   
        auth.set_access_token(self.access_key, self.access_secret)                            
        api = tweepy.API(auth_handler = auth)                                         
        api.update_status(msg)
    
if __name__ == '__main__':
    date = datetime.datetime.today()
    tw = tweet()
    dev = i2c(1, 0x48)
    result = dev.getblock(0x00, 12)
    temp = (result[0] << 8 | result[1]) >> 3
    if(temp >= 4096):
        temp -= 8192;
    msg = '{0} current temperature {1:7.2f}'\
        .format(date.strftime("%Y-%m-%d %H:%M:%S"), temp / 16.0)
    tw.tweet(msg)    

実行すると現在の気温付きでツイートされる。室温21.12度。

tweet2

あとは一定時間で自動的にツイートするようにすれば、twitterで何かのデータを監視をするシステムに応用できるはず。ただしtwitterAPIを使用したツイートには回数制限があるため、短時間に大量のツイートを行うようなプログラムは規制に引っかかる可能性がある。

将来的にtwitterAPIの仕様が変更になる可能性もあるので、注意が必要。