MyEnigma

とある自律移動システムエンジニアのブログです。#Robotics #Programing #C++ #Python #MATLAB #Vim #Mathematics #Book #Movie #Traveling #Mac #iPhone

Python-twitterを使う時の注意点

目次

はじめに

python-twitterというtwitterのAPIを利用するPythonライブラリを使って、

色々遊んでいるのですが、個人的にはまった所をメモとして残しておきます。

python-twitterについて

python-twitterの説明やコードはこちら

python-twitter - A python wrapper around the Twitter API - Google Project Hosting

github.com

 

インストールはpipを使うのが簡単でした。

$ pip install python-twitter

認証してオブジェクト生成

事前に下記のサイトから、認証用のキーを取得して、

下記のようにオブジェクトを設定しましょう。

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

下記の例では、このapiオブジェクトを使う前提です。

ツイートしたい時

api.PostUpdate(u"Tweet!!")

あるユーザのツイートを取得したい時

statuses = api.GetUserTimeline(userName="Atsushi_twi")

print [s.text for s in statuses]

リツイートしたい時

tweetのidが分かればretweet出来ます。

下記の例は@Atsushi_twiのtweetを取得して、

その一番新しいtweetをリツイートする場合です。

tweets=api.GetUserTimeline(screen_name="Atsushi_twi")

api.PostRetweet(tweets[0].id)

GetUserTimelineで指定したユーザのではなく、自分のtweetが取得されてしまう時は、

別のアカウント@hogehogeでauthorizationして、

@Atsushi_twiのツイートを取得しようと思った時に、

下記のように書いたら、

statuses = api.GetUserTimeline("Atsushi_twi")

print [s.text for s in statuses]

なぜか@hogehogeのツイートが取得されてしまいました。

 

色々調べた所、GetUserTimeline関数にユーザ名を渡す時は、

userName変数に入れなければいけないみたいです。

statuses = api.GetUserTimeline(userName="Atsushi_twi")

print [s.text for s in statuses]

こうしたら、ちゃんと@Atsushi_twiのtweetが取得できました (^^)

ツイートを検索したい時

GetSearch関数を使います。

search="Python OR Twitter "
tweets = api.GetSearch(term=search, count=100,result_type='recent')

searchの検索文字列には、ANDとORが使えるので

複雑な検索条件も可能です。

countは検索するツイートの数で、現在の所、100件が最高数のようです。

返り値は検索されたツイートのリストです。

 

ユーザをフォローする

APIからフォローする場合は下記のようにできます。

username="Atsushi_twi"
api.CreateFriendship(screen_name=username)

screen_nameにユーザ名を入れてAPIを呼べばフォローできます。

ちなみにuser_id引数にユーザIDを入れてもフォロー可能です。

 

Status構造体のメンバ一覧

ヘルプコマンドより抜粋しました。

(少しずつ翻訳する予定です)

 

メンバ 説明
contributors
coordinates
created_at The time this status message was posted.
created_at_in_seconds The time this status message was posted, in seconds since the epoch
current_user_retweet
favorite_count The number of favorites for this status message.
favorited The favorited state of this status message.
geo
id The unique id of this status message.
id_str The unique id_str of this status message.
in_reply_to_screen_name
in_reply_to_status_id
in_reply_to_user_id
location The geolocation string of this status message
now The wallclock time for this status instance.
place
possibly_sensitive
relative_created_at Get a human readable string representing the posting time
retweet_count
retweeted
retweeted_status
scopes
source
text The text of this status message
truncated
user A twitter.User representing the entity posting this status message
withheld_copyright
withheld_in_countries
withheld_scope

参考資料

python-twitterのまとめ - Hello World!!

Python で TwitterAPIを使って Tweet を取得 - my-notebook

Pythonを用いてTwitterの検索を行う - Qiita

 

MyEnigma Supporters

もしこの記事が参考になり、

ブログをサポートしたいと思われた方は、

こちらからよろしくお願いします。

myenigma.hatenablog.com