-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitterQuery.py
191 lines (140 loc) · 5.81 KB
/
twitterQuery.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import requests
import os
import json
from yourCreds import bearer_token
import pandas as pd
# To set your enviornment variables in your terminal run the following line:
# export 'BEARER_TOKEN'='<your_bearer_token>'
# To set your enviornment variables in your terminal run the following line:
# export 'BEARER_TOKEN'='<your_bearer_token>'
def auth():
return bearer_token
def create_headers(bearer_token):
headers = {"Authorization": "Bearer {}".format(bearer_token)}
return headers
def connect_to_endpoint(url, headers):
response = requests.request("GET", url, headers=headers)
print(response.status_code)
if response.status_code != 200:
raise Exception(
"Request returned an error: {} {}".format(
response.status_code, response.text
)
)
return response.json()
def create_usergrab_url(queryItems):
# Specify the usernames that you want to lookup below
# You can enter up to 100 comma-separated values.
queryItems = queryItems.replace(' ', '')
usernames = f"usernames={queryItems}"
user_fields = "user.fields=description,created_at,public_metrics"
# User fields are adjustable, options include:
# created_at, description, entities, id, location, name,
# pinned_tweet_id, profile_image_url, protected,
# public_metrics, url, username, verified, and withheld
url = "https://api.twitter.com/2/users/by?{}&{}".format(usernames, user_fields)
return url
# Host the portfolio
# https://www.pythonanywhere.com/details/flask_hosting
def connect_to_endpoint(url, headers):
response = requests.request("GET", url, headers=headers)
print(response.status_code)
if response.status_code != 200:
raise Exception(
"Query Users request returned an error: {} {}".format(
response.status_code, response.text
)
)
return response.json()
def parser(new_json):
# with open('usersTweet.json', 'w') as outf:
# outf.write(new_json)
tweet_json = json.loads(new_json)
x = tweet_json['data']
id = []
for i in x:
id.append(str(i['id'].replace(' ','')))
print(id)
# d = {'id': id}
# new = pd.DataFrame(d)
# return ','.join(id)
return id
# def create_users_followers_url():
# # Replace with user ID below
# user_id = ## add the ids from users here as integers
# return "https://api.twitter.com/2/users/{}/followers".format(user_id)
def get_params():
return {"user.fields": "created_at"}
# def connect_to_users_followers_endpoint(users_followers_url, headers, params):
# response = requests.request("GET", users_followers_url, headers=headers, params=params)
# print(response.status_code)
# if response.status_code != 200:
# raise Exception(
# "Users Followers request returned an error: {} {}".format(
# response.status_code, response.text
# )
# )
# return response.json()
# def create_grabtweets_url():
# tweet_fields = "tweet.fields=lang,author_id,geo,text,public_metrics"
# # Tweet fields are adjustable.
# # Options include:
# # attachments, author_id, context_annotations,
# # conversation_id, created_at, entities, geo, id,
# # in_reply_to_user_id, lang, non_public_metrics, organic_metrics,
# # possibly_sensitive, promoted_metrics, public_metrics, referenced_tweets,
# # source, text, and withheld
# ids = "ids=1278747501642657792,1255542774432063488"
# # You can adjust ids to include a single Tweets.
# # Or you can add to up to 100 comma-separated IDs
# url = "https://api.twitter.com/2/tweets?{}&{}".format(ids, tweet_fields)
# return url
# def connect_to_grabtweets_endpoint(grabtweets_url, headers):
# response = requests.request("GET", grabtweets_url, headers=headers)
# print(response.status_code)
# if response.status_code != 200:
# raise Exception(
# "Grab Tweets request returned an error: {} {}".format(
# response.status_code, response.text
# )
# )
# return response.json()
def get_timeline_params():
return "tweet.fields=lang,author_id,geo,text,public_metrics&max_results=100"
def create_user_timeline_url(cln_ids):
# Replace with user ID below
user_id = cln_ids
return "https://api.twitter.com/2/users/{}/tweets".format(user_id)
def connect_to_user_timeline_endpoint(user_timeline_url, headers, timeline_params):
response = requests.request("GET", user_timeline_url, headers=headers, params=timeline_params)
print(response.status_code)
if response.status_code != 200:
raise Exception(
"Request returned an error: {} {}".format(
response.status_code, response.text
)
)
return response.json()
def scale_bird(queryItems):
#### GET USER CODE *************
bearer_token = auth()
url = create_usergrab_url(queryItems)
headers = create_headers(bearer_token)
json_response = connect_to_endpoint(url, headers)
new = json.dumps(json_response, indent=4, sort_keys=True)
# print(new)
# with open('usersTweet.json', 'w') as outf:
# outf.write(new)
cln_ids = parser(new)
for id in cln_ids:
user_timeline_url = create_user_timeline_url(id)
timeline_params = get_timeline_params()
json_response4 = connect_to_user_timeline_endpoint(user_timeline_url, headers, timeline_params)
naw = json.dumps(json_response4, indent=4, sort_keys=True)
# with open('usersTimeline.json', 'w') as outf:
# outf.write(naw)
tweets_json = json.loads(naw)
burd_df = pd.json_normalize(tweets_json, record_path='data',record_prefix='')
print(burd_df.head(4))
return burd_df
# final return as a dataframe