Twitter APIをPythonでいじってみた

#はじめに
Twitter APIをPythonでいじってみました。具体的には、タイムラインと検索結果の取得と表示です。

#コード
```python:twapi_timeline.py
# -*- coding: utf-8 -*-
from requests_oauthlib import OAuth1Session
import json
from datetime import datetime

CK = '************************'# Consumer Key
CS = '************************'# Consumer Secret
AT = '************************'# Access Token
AS = '************************'# Accesss Token Secert

# OAuth で GET
twitter = OAuth1Session(CK, CS, AT, AS)
req = twitter.get(timeline_url, params = params)

# タイムライン取得用のURL
timeline_url = "https://api.twitter.com/1.1/statuses/home_timeline.json"
params = {"count": 20}

if req.status_code != 200:
    print ("Error: %d" % req.status_code)

# レスポンスはJSON形式なので parse する
tweets = json.loads(req.text)
for i, tweet in enumerate(tweets):
    tweet_id = tweet[u'id_str']
    text = tweet[u'text']
    created_at = tweet[u'created_at']
    user_id = tweet[u'user'][u'id_str']
    user_description = tweet[u'user'][u'description']
    screen_name = tweet[u'user'][u'screen_name']
    user_name = tweet[u'user'][u'name']
    print("tweet_id:", tweet_id)
    print("text:", text)
    print("created_at:", created_at)
    print("user_id:", user_id)
    print("user_desc:", user_description)
    print("screen_name:", screen_name)
    print("user_name:", user_name)
    print('\n')

print('取得したtweet数:',len(tweets))

# API残り
limit = req.headers['x-rate-limit-remaining']
# API制限の更新時刻 (UNIX time)
reset = int(req.headers['x-rate-limit-reset'])
print ("API remain: " + limit)
print ("API reset: " + str(datetime.fromtimestamp(reset)))

```

```python:twapi_search.py
# -*- coding: utf-8 -*-
from requests_oauthlib import OAuth1Session
import json
from datetime import datetime

CK = '************************'# Consumer Key
CS = '************************'# Consumer Secret
AT = '************************'# Access Token
AS = '************************'# Accesss Token Secert

# OAuth で GET
twitter = OAuth1Session(CK, CS, AT, AS)
req = twitter.get(search_url, params = params)

# 検索結果取得用のURL
search_url = "https://api.twitter.com/1.1/search/tweets.json"
params = {
        "q": u'bitcoin',
        "lang": "ja",
        "result_type": "recent",
        "count": 20
        }

if req.status_code != 200:
    print ("Error: %d" % req.status_code)

# レスポンスはJSON形式なので parse する
tweets = json.loads(req.text)
for i, tweet in enumerate(tweets["statuses"]):
    tweet_id = tweet[u'id_str']
    text = tweet[u'text']
    created_at = tweet[u'created_at']
    user_id = tweet[u'user'][u'id_str']
    user_description = tweet[u'user'][u'description']
    screen_name = tweet[u'user'][u'screen_name']
    user_name = tweet[u'user'][u'name']
    print("tweet_id:", tweet_id)
    print("text:", text)
    print("created_at:", created_at)
    print("user_id:", user_id)
    print("user_desc:", user_description)
    print("screen_name:", screen_name)
    print("user_name:", user_name)
    print('\n')

print('取得したtweet数:',len(tweets["statuses"]))

# API残り
limit = req.headers['x-rate-limit-remaining']
# API制限の更新時刻 (UNIX time)
reset = int(req.headers['x-rate-limit-reset'])
print ("API remain: " + limit)
print ("API reset: " + str(datetime.fromtimestamp(reset)))
```

#参考文献
[Python で Twitter API にアクセス](https://qiita.com/yubais/items/dd143fe608ccad8e9f85)
[Python で Twitter から情報収集 (Twitter API 編)](https://qiita.com/Salinger/items/020b670466a9835c94bb)
[https://developer.twitter.com/en/docs](https://developer.twitter.com/en/docs)
[https://developer.twitter.com/en/docs/api-reference-index](https://developer.twitter.com/en/docs/api-reference-index)