tweepyでOAuthを試してみた

PythonでOAuthを試したかったのでtweepyを使ってみた。

easy_installじゃなくてgitから

git clone git://github.com/joshthecoder/tweepy.git

まずはBasic認証

>>> import tweepy
>>> auth = tweepy.BasicAuthHandler("user", "password")
>>> api = tweepy.API(auth)
>>> api.update_status('hello from tweepy(Basic)')
<tweepy.models.Status object at 0x13c8db0>

http://twitter.com/kzfm/status/9330123865

続いてOAuth。consumer_token,consumer_secretはあらかじめtwitterから取得しておく。

>>> import tweepy
>>> consumer_token = "xxxxxxxxxxxxxx"
>>> consumer_secret = "xxxxxxxxxxxxxxxx"
>>> auth = tweepy.OAuthHandler(consumer_token, consumer_secret)
>>> redirect_url = auth.get_authorization_url()
>>> redirect_url

redirect_urlのURLにブラウザでアクセスして許可すると7桁の数字が表示されるのでそれを入力

>>> verifier = '1234567'
>>> auth.get_access_token(verifier)
<tweepy.oauth.OAuthToken object at 0x1393510>
>>> key = auth.access_token.key
>>> secret = auth.access_token.secret
>>> auth = tweepy.OAuthHandler(consumer_token, consumer_secret)
>>> auth.set_access_token(key, secret)
>>> oauthapi = tweepy.API(auth)
>>> oauthapi.update_status('hello from tweepy(OAuth)')
<tweepy.models.Status object at 0x1397b50>

http://twitter.com/kzfm/status/9330367673

wsgioauthもいいかもとか思い始めたでござる。

twitterでフォローされているか調べるスクリプト

ダイレクトメッセージを送れるかどうかで判断するみたいなつぶやきが流れてたので。

setでごにょれば良かろうと。

import twitter
api = twitter.Api(username='user',password='pass')
friends = set([u.name for u in api.GetFriends()])
followers = set([u.name for u in api.GetFollowers()])

print "follow and followed: "     + ",".join(list(friends.intersection(followers)))
print "not follow but followed: " + ",".join(list(followers.difference(friends)))
print "follow but not followed: " + ",".join(list(friends.difference(followers)))

ProductName みんなのPython 改訂版
柴田 淳
ソフトバンククリエイティブ / ¥ 2,940 ()
在庫あり。

横着おはありスクリプト

おは(ようを)あり(がとう)ではなくて おは(が)あり(ました)

つまり

isOha? :: Str -> Bool

なスクリプト。

#!/usr/bin/env python
# -*- encoding:utf-8 -*-

import twitter
api = twitter.Api(username='user',password='pass')
oha = [r.user.screen_name for r in api.GetReplies() \
if r.text.encode('utf-8').find('おは') > -1]
thx = "@" + " @".join(oha) + "おはありです"
status = api.PostUpdate(thx.decode('utf-8'))

pypiにあるtwitterじゃなくてpython-twitterを使いたかったので、既にインストールされてたtwitterモジュールpythonモジュールのアンインストール - kokiyaの日記を参考に消した。

Rでつぶやく(twitteR)

Tweeting from R

> library("RCurl")
> opts <- curlOptions(header = FALSE, 
+   userpwd = "[user]:[pass]", netrc = FALSE)
> tweet <- function(status){
+   method <- "http://twitter.com/statuses/update.xml?status="
+   encoded_status <- URLencode(status)
+   request <- paste(method,encoded_status,sep = "")
+   postForm(request,.opts = opts)
+ }
> xx<-rnorm(2)
> tweet(paste(xx,collapse="|"))

つぶやいた

SVMかなんかを利用して分類結果をつぶやくボットをつくっても面白いかも。

Programming Twitter

気になるけど様子見だなぁ。

ProductName Programming Twitter: Learn How to Build Applications With the Twitter Api
Kevin Makice
Oreilly & Associates Inc / ¥ 3,134 ()
近日発売 予約可

珈琲メーカーが一緒だ。でもこの機種結構漏れるんだよね。知り合いも一緒の使っていてあれは欠陥品だといつも漏らしてる。

WassrとTwitterのIRCゲートウェイを入れていい感じ

LimeChat使ってます。

TwitterIrcGateway快適。

Wassr 用の IRC ゲートウェイ入れるともっと快適

素晴らしい