特定ユーザーのツイートを取得する方法です。
API名は、user_timeline ですが、特定ユーザーのツイートを取得できます。
参考情報
GET statuses/user_timeline - ユーザータイムラインを取得する
get_tweet.py
#! /usr/bin/python3
# -*- coding: utf-8 -*-
#
# get_tweet.py
#
# Dec/21/2017
# --------------------------------------------------------------------
import sys
import json
import oauth2 as oauth
#
from define_client import define_client_proc
# --------------------------------------------------------------------
# [8]:
def get_tweets_proc(client,user_id):
nnx = 10
url_base = "https://api.twitter.com/1.1/statuses/user_timeline.json?user_id="
url = url_base + user_id + "&count=" + str(nnx)
array_aa = []
response, data = client.request(url)
if response.status == 200:
json_str = data.decode('utf-8')
# print(json_str)
array_aa = json.loads(json_str)
sys.stderr.write("len(array_aa) = %d\n" % len(array_aa))
#
else:
sys.stderr.write("*** error *** get_ids_proc ***\n")
sys.stderr.write("Error: %d\n" % response.status)
#
return array_aa
# --------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
user_id = "276219120"
#
client = define_client_proc()
#
array_aa = get_tweets_proc(client,user_id)
#
sys.stderr.write("len(array_aa) = %d\n" % len(array_aa))
#
for unit_aa in array_aa:
print (unit_aa['created_at'])
print (unit_aa['id'])
print (unit_aa['text'])
#
sys.stderr.write("*** 終了 ***\n")
# --------------------------------------------------------------------
define_client.py
# --------------------------------------------------------------------
# define_client.py
#
# Apr/26/2019
# --------------------------------------------------------------------
import os
from dotenv import load_dotenv
from requests_oauthlib import OAuth1Session
import oauth2 as oauth
# --------------------------------------------------------------------
def define_client_proc():
dotenv_path = '.env'
load_dotenv(dotenv_path)
CONSUMER_KEY = os.environ.get("CONSUMER_KEY")
CONSUMER_SECRET = os.environ.get("CONSUMER_SECRET")
ACCESS_TOKEN = os.environ.get("ACCESS_TOKEN")
ACCESS_TOKEN_SECRET = os.environ.get("ACCESS_TOKEN_SECRET")
#
consumer = oauth.Consumer(key=CONSUMER_KEY, secret=CONSUMER_SECRET)
access_token = oauth.Token(key=ACCESS_TOKEN, secret=ACCESS_TOKEN_SECRET)
client = oauth.Client(consumer, access_token)
#
return client
# --------------------------------------------------------------------
.env
CONSUMER_KEY = '******'
CONSUMER_SECRET = '******'
ACCESS_TOKEN = '******'
ACCESS_TOKEN_SECRET = '******'
Arch Linux でのライブラリーのインストール
sudo pip install oauth2