Last active
December 6, 2021 14:28
-
-
Save netalkGB/77c4e2fc90fb6b591e625eb641a8b6ad to your computer and use it in GitHub Desktop.
[WIP] Twitter API v2でFiltered Stream
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| import os | |
| import base64 | |
| import json | |
| import urllib.request | |
| import urllib.parse | |
| api_key = os.environ['TWITTER_API_KEY'] | |
| api_key_secret = os.environ['TWITTER_API_KEY_SECRET'] | |
| bearer_token = os.environ['TWITTER_BEARER_TOKEN'] | |
| def get_bearer_token(api_key, api_key_secret): | |
| credential = '{}:{}'.format(api_key, api_key_secret) | |
| credential_base64 = base64.b64encode(credential.encode()).decode() | |
| url = 'https://api.twitter.com/oauth2/token' | |
| method = 'POST' | |
| headers = {'Authorization': 'Basic {}'.format(credential_base64), | |
| 'User-Agent': 'autopilot', | |
| 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'} | |
| params = {'grant_type': 'client_credentials'} | |
| data = urllib.parse.urlencode(params).encode('utf-8') | |
| request = urllib.request.Request( | |
| url, data=data, headers=headers, method=method) | |
| res = urllib.request.urlopen(request) | |
| response_data = res.read() | |
| response_json = json.loads(response_data) | |
| return response_json['access_token'] | |
| def add_rule(token): | |
| method = 'POST' | |
| headers = {'Authorization': 'Bearer {}'.format(token), | |
| 'User-Agent': 'autopilot', | |
| 'Content-type': 'application/json'} | |
| data = { | |
| 'add': [{ | |
| 'value': 'from:screen_name OR from:screen_name OR from:screen_name OR from:screen_name OR from:screen_name' | |
| }] | |
| } | |
| url = 'https://api.twitter.com/2/tweets/search/stream/rules' | |
| request = urllib.request.Request( | |
| url, headers=headers, method=method, data=json.dumps(data).encode("utf-8")) | |
| res = urllib.request.urlopen(request) | |
| print(res) | |
| response_data = res.read() | |
| response_json = json.loads(response_data) | |
| print(response_json) | |
| def delete_rule(token, ids): | |
| method = 'POST' | |
| headers = {'Authorization': 'Bearer {}'.format(token), | |
| 'User-Agent': 'autopilot', | |
| 'Content-type': 'application/json'} | |
| data = { | |
| 'delete': { | |
| 'ids': ids | |
| } | |
| } | |
| url = 'https://api.twitter.com/2/tweets/search/stream/rules' | |
| request = urllib.request.Request( | |
| url, headers=headers, method=method, data=json.dumps(data).encode("utf-8")) | |
| res = urllib.request.urlopen(request) | |
| print(res) | |
| response_data = res.read() | |
| response_json = json.loads(response_data) | |
| print(response_json) | |
| def get_rules(token): | |
| method = 'GET' | |
| url = 'https://api.twitter.com/2/tweets/search/stream/rules' | |
| headers = {'Authorization': 'Bearer {}'.format(token), | |
| 'User-Agent': 'autopilot', | |
| 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'} | |
| request = urllib.request.Request( | |
| url, headers=headers, method=method) | |
| res = urllib.request.urlopen(request) | |
| response_data = res.read() | |
| response_json = json.loads(response_data) | |
| print(response_json) | |
| def stream(token): | |
| method = 'GET' | |
| url = 'https://api.twitter.com/2/tweets/search/stream?tweet.fields=created_at&expansions=author_id&user.fields=created_at' | |
| headers = {'Authorization': 'Bearer {}'.format(token), | |
| 'User-Agent': 'autopilot', | |
| 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'} | |
| request = urllib.request.Request( | |
| url, headers=headers, method=method) | |
| res = urllib.request.urlopen(request) | |
| for line in res: | |
| try: | |
| data = json.loads(line.decode('utf-8')) | |
| print(data) | |
| except json.JSONDecodeError as e: | |
| print(e, file=sys.stderr) | |
| pass | |
| if __name__ == '__main__': | |
| # https://developer.twitter.com/ja/docs/authentication/api-reference/token このメソッドで受信したトークンはキャッシュする必要があります。試行の頻度が多すぎると、リクエストはコード99とともにHTTP 403で拒否されます。 | |
| # token = get_bearer_token(api_key, api_key_secret) | |
| # # print(token) | |
| # add_rule(bearer_token) | |
| # get_rules(bearer_token) | |
| # stream(bearer_token) | |
| # delete_rule(bearer_token, ['']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment