GOTO M.

趣味のコーディングとか、勉強とか、読書とか

PythonからTwitterを利用するまで

  • TweetHaskell文として評価し結果を返すBOTを作る
    • 環境を準備する
    • BOTを作る       ←いまここ
    • Haskellと連携させる

PythonTwitter APIのインストール

せっかくPythonの勉強始めたし、現在構想中のツールPythonで実装する予定。
Google Codesにホスティングされている Python-Twitter を利用することにする。

$ sudo apt-get install python-simplejson
$ wget http://python-twitter.googlecode.com/files/python-twitter-0.6.tar.gz
$ tar xvf python-twitter-0.6.tar.gz

python-twitter を使ってみる」(http://techno-st.net/2009/07/04/python-twitter.html)を参考に、フォローユーザの取得、Tweetの発信まで成功。

超単純なBotを記述

自身への@Replyが有った際に何らかの処理をするBot

import twitter
import time

USER="Hoge"
PASS="Fuga"

api=twitter.Api(username=USER, password=PASS)

def reply_tweet(tweet):
  api.PostUpdate("I've got a message from %s !!" % (tweet.GetUser().screen_name))

maxId = 0

while(1):
  for i in reversed(api.GetReplies()):
    if maxId < i.GetId():
      reply_tweet(i) # change if the function changed
      maxId = i.GetId()
  time.sleep(90)