python-twitterのまとめ

最近、研究でpython-twitterからTwitterAPIをたたいているので、まとめておく。

英語版のドキュメントはこちら→ Python: module twitter

インポート・認証

以下でおk.

import twitter
api = twitter.Api()

ただし、認証が必要なものをたたく場合は、以下のようにする。

api = twitter.Api(consumer_key=CONSUMER_KEY,
                  consumer_secret=CONSUMER_SECRET,
                  access_token_key=ACCESS_TOKEN,
                  access_token_secret=ACCESS_TOKEN_SECRET,
                  cache=None)

認証方法は以下を参照。
TwitterBotの作成① - Hello World !

GetUser(user)

user情報を取得する。

以下、pika_shiの情報を取得してみる。

>>> user = api.GetUser('pika_shi')
>>> user.id # ユーザid
141077154
>>> print user.screen_name # ユーザ名
pika_shi
>>> print user.name # 名前
ぴかし
>>> print user.location # 位置情報
京都
>>> print user.description # 自己紹介
京都大学/工学部/情報学科/B4/KUIS4/社会情報学専攻/京炎そでふれ!/彩京前線/京都学生祭典/KIF8th,9th/総務部/MENSA会員/塾講師/プログラミング/Python/Java/Mac/TLから学習する人工無能bot @pika_shi_bot もよろしく!
>>> user.protected # ツイートを非公開にしてるか
False
>>> user.utc_offset # 協定世界時とローカルタイムとの時差(秒)
32400
>>> print user.time_zone # タイムゾーン
Osaka
>>> print user.url # URL
http://d.hatena.ne.jp/pika_shi/
>>> user.status # 最新ツイート
<twitter.Status object at 0x101052090>
>>> user.statuses_count # ツイート数
3547
>>> user.followers_count # フォロワー数
376
>>> user.friends_count # フォロイー数
297
>>> user.favourites_count # お気に入り数
212

GetUserTimeline(user)

userのツイート一覧を取得する。

以下、pika_shiのツイートを取得する。

>>> timeline = api.GetUserTimeline('pika_shi')
>>> for tweet in timeline:
...     print tweet.text
... 
@petitviolet 了解^^
@petitviolet いつくらいに確定しそうか聞いてみるわ! 増えること自体はお店的に大丈夫なんかな?
・・・
@yoshito8 フライングバルスw 何やねんw
卒論ェ… RT @o_tomox: 今年もあと3週間……

また、以下のようにしてツイートの内容(text)以外の様々な情報も取得できる。

>>> tweet = timeline[0]
>>> print tweet.created_at # ツイートされた時間(協定世界時)
Sun Dec 11 05:07:32 +0000 2011
>>> tweet.created_at_in_seconds # ツイートされた時間(UNIX秒)
1323580052
>>> tweet.favorited # お気に入りに入れているか(要認証)
False
>>> print tweet.in_reply_to_screen_name # 返信先のユーザ名
petitviolet
>>> print tweet.in_reply_to_user_id # 返信先のユーザid
116969050
>>> tweet.in_reply_to_status_id # 返信先のツイートid
145731319173877760
>>> tweet.truncated # 140字を超えているか
False
>>> print tweet.source # ツイートされたクライアント
<a href="http://www.tweetdeck.com" rel="nofollow">TweetDeck</a>
>>> tweet.id # ツイートid
145731418545336320
>>> print tweet.text # ツイートの内容(テキスト)
@petitviolet 了解^^
>>> print tweet.location # ツイートの位置情報
None
>>> print tweet.relative_created_at # ツイートされた時間
about an hour ago
>>> tweet.user # ツイートしたユーザの情報
<twitter.User object at 0x101052b50>

GetFriendsTimeline(user)

userのTLを取得。(要認証)

>>> timeline = api.GetFriendsTimeline('pika_shi')
>>> for tweet in timeline:
...     print tweet.text
... 
「成果をあげるのは才能ではなく、習慣だ。」
実験のレポートは一旦寝てから始めよう※再徹夜ふらぐ乱立中
・・・
いやー、晴天って良いなぁ
マスオデックス

GetPublicTimeline()

パブリックTLを取得。
当然日本語以外も混ざってる。

>>> timeline = api.GetPublicTimeline()
>>> for tweet in timeline:
...     print tweet.text
... 
*yawns*
Just gave up in Monopoly cause he is so brutal. Gave all my money & 
・・・
LMFAOOOOO !
I have tons of followers and got them by using this easy program! http://t.co/9l1TaXHo

GetFriends(user)

userのフォロイーを取得する。

最大100件までを取得するが、ライブラリに変更を加えることでそれ以上取得可能。
参考→ python-twitterで100人以上のフォロイーを取得 - Hello World !

>>> friends = api.GetFriends()
>>> for friend in friends:
...     print friend.screen_name
... 
teracy
happy_nina
・・・
toumeinomochi
sakikuri1023

GetFollowers()

フォロワーを取得する。(要認証)
なぜ要認証なのかが謎。

>>> followers = api.GetFollowers()
>>> for follower in followers:
...     print follower.screen_name
... 
FurusatoOuen
happy_nina
・・・
Foomin_110
reigaaaaan

PostUpdate(tweet)

ツイートする。(要認証)

>>> postmsg = '疲れた'
>>> api.PostUpdate(postmsg.decode("utf-8"))

よく使うのはこれくらいかな。