-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsearch_Tweets.py
40 lines (29 loc) · 1.24 KB
/
search_Tweets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import requests
import os
import json
# To set your environment variables in your terminal run the following line:
# export 'BEARER_TOKEN'='<your_bearer_token>'
bearer_token = "AAAAAAAAAAAAAAAAAAAAAJUCUQEAAAAAKbKDbiYVdLKbeg0czFTSDHM02cQ%3DubcnsoHHQ9YWszx5bZslGmhktsHlxa0rbZO26shLQs2jtZRcjV"
search_url = "https://api.twitter.com/2/tweets/search/recent"
# Optional params: start_time,end_time,since_id,until_id,max_results,next_token,
# expansions,tweet.fields,media.fields,poll.fields,place.fields,user.fields
query_params = {'query': '#fb','tweet.fields': 'author_id'}
def bearer_oauth(r):
"""
Method required by bearer token authentication.
"""
r.headers["Authorization"] = f"Bearer {bearer_token}"
r.headers["User-Agent"] = "v2RecentSearchPython"
return r
def connect_to_endpoint(url, params):
response = requests.get(url, auth=bearer_oauth, params=params)
print(response.status_code)
if response.status_code != 200:
raise Exception(response.status_code, response.text)
return response.json()
def main():
json_response = connect_to_endpoint(search_url, query_params)
print(len(json_response["data"]))
print(json.dumps(json_response, indent=4, sort_keys=True))
if __name__ == "__main__":
main()