辞書引く日々

辞書が好きなのだ。辞書を引くのだ。

python + tweepy + OAuth 最低限度の twitter メモ(3)タイムライン

2010年07月11日 | Weblog
http://joshthecoder.github.com/tweepy/docs/api.html# を見ながら、
tweepy でホームタイムラインを得てみようと思う。

まず、以下は念仏のように最初にやって api オブジェクトを得ておく。

--------------------------------------------------
#!/usr/bin/env python

import tweepy
import type

CONSUMER_KEY = ''
CONSUMER_SECRET = ''
ACCESS_KEY = ''
ACCESS_SECRET = ''

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
------------------------------------------------------

このあとに
api.home_timeline()
とやると、最新20個の投稿のリストが得られる。

このリストの要素はそれぞれ
<class 'tweepy.models.Status'>
のインスタンスである。
これには、以下のような変数が入っている。

api <class 'tweepy.api.API'>
contributors <type 'NoneType'>
truncated <type 'bool'>
text <type 'unicode'>
created_at <type 'datetime.datetime'>
author <class 'tweepy.models.User'>
in_reply_to_status_id <type 'NoneType'>
coordinates <type 'NoneType'>
source_url <type 'str'>
source <type 'str'>
favorited <type 'bool'>
in_reply_to_screen_name <type 'NoneType'>
in_reply_to_user_id <type 'NoneType'>
place <type 'NoneType'>
geo <type 'NoneType'>
id <type 'long'>
user <class 'tweepy.models.User'>

(ドキュメントが見つからないから__dict__ を調べた)

テキストの内容は、text <type 'unicode'> だから、
最新(0番目)の投稿本文は、
api.home_timeline()[0].text
で得られる。

また、この投稿者名は、author で、<class 'tweepy.models.User'>
のインスタンスである。
そこで、これが持つ変数を調べてみると、

profile_use_background_image <type 'bool'>
id <type 'int'>
api <class 'tweepy.api.API'>
verified <type 'bool'>
profile_sidebar_fill_color <type 'str'>
profile_text_color <type 'str'>
followers_count <type 'int'>
profile_sidebar_border_color <type 'str'>
location <type 'str'>
profile_background_color <type 'str'>
utc_offset <type 'int'>
statuses_count <type 'int'>
description <type 'unicode'>
friends_count <type 'int'>
profile_link_color <type 'str'>
profile_image_url <type 'str'>
notifications <type 'bool'>
geo_enabled <type 'bool'>
profile_background_image_url <type 'str'>
screen_name <type 'str'>
lang <type 'str'>
profile_background_tile <type 'bool'>
favourites_count <type 'int'>
name <type 'unicode'>
url <type 'str'>
created_at <type 'datetime.datetime'>
contributors_enabled <type 'bool'>
time_zone <type 'str'>
protected <type 'bool'>
following <type 'bool'>

したがって、最新(0番目)の投稿について、
api.home_timeline()[0].author.name で名前
api.home_timeline()[0].author.screen_name でスクリーンネーム(@の後につけるやつ)を知ることができる。

まあ、タイムラインを一通り読めるようになった。

つぎは、最新20より前の投稿を得ることを考えてみよっと。(つづく)
コメント    この記事についてブログを書く
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする
« python + tweepy + OAuth 最... | トップ | python + tweepy + OAuth 最... »
最新の画像もっと見る

コメントを投稿

ブログ作成者から承認されるまでコメントは反映されません。

Weblog」カテゴリの最新記事