-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
413 lines (327 loc) · 17 KB
/
app.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import csv
import json
import uuid
from io import BytesIO, StringIO
import zipfile
from typing import List, Dict, Any
import pandas as pd
import requests
import gspread
from flask import Flask, render_template, request, redirect, jsonify, send_file, flash, Blueprint
from apscheduler.schedulers.background import BackgroundScheduler
from oauth2client.service_account import ServiceAccountCredentials
from dotenv import load_dotenv
# Load environment variables from the .env file
load_dotenv()
# Creator: Quadri Basit Ayomide
# Email: [email protected]
# Initialize Flask app
load = Flask(__name__)
load.secret_key = 'Quadri Basit Ayomide'
# Twitter API Bearer Token
bearer_token = os.getenv("BEARER_TOKEN")
# Scheduler setup
scheduler = BackgroundScheduler()
scheduler.start()
# JSON file path for storing space IDs
json_file_path = 'space_id.json'
#################################### Route For The Homepage ############################################
@load.route('/', methods=['GET', 'POST'])
def home() -> Any:
"""Render the homepage and handle POST requests for space ID submission."""
if request.method == 'POST':
space_id = request.form["spaceID"]
try:
with open(json_file_path, 'r') as json_file:
data = json.load(json_file)
except FileNotFoundError:
data = []
# Append the new space_id
data.append({'space_id': space_id})
# Write the updated data back to the JSON file
with open(json_file_path, 'w') as json_file:
json.dump(data, json_file, indent=2)
params = {
"expansions": "host_ids,topic_ids,invited_user_ids,creator_id,speaker_ids",
"space.fields": "created_at,lang,invited_user_ids,participant_count,scheduled_start,started_at,title,topic_ids,updated_at,speaker_ids,ended_at",
"user.fields": "created_at,description,entities,id,location,name,pinned_tweet_id,profile_image_url,protected,public_metrics,url,username,verified,verified_type,withheld",
}
headers = {
"Authorization": f"Bearer {bearer_token}",
"Content-Type": "application/json",
}
response = requests.get(f"https://api.twitter.com/2/spaces/{space_id}", headers=headers, params=params)
if response.status_code == 200:
space_data = response.json().get("data", {})
topics = response.json().get('includes', {}).get('topics', [])
custom_space_data = {
'Space Title': space_data.get('title', ''),
'Topics Ids': ', '.join(map(str, space_data.get('topic_ids', []))),
'topic_name': [topic.get('name', '') for topic in topics],
'topic_des': [topic.get('description', '') for topic in topics],
'Space State': space_data.get('state', ''),
'Host ID': space_data.get('host_ids', ''),
'Creator ID': space_data.get('creator_id', ''),
'Updated At': space_data.get('updated_at', ''),
'invited_user_ids': space_data.get('invited_user_ids', []),
'speakers': ', '.join(map(str, space_data.get('speaker_ids', []))),
'speakersn': len(space_data.get('speaker_ids', [])),
'Total Moderators': len(space_data.get('speaker_ids', [])),
'Created At': space_data.get('created_at', ''),
'Ended At': space_data.get('ended_at', ''),
'Language': space_data.get('lang', ''),
'Subscriber Count': space_data.get('participant_count', '')
}
for key, value in custom_space_data.items():
space_data[key] = value
with open("space_data.json", "w") as space_file:
json.dump(space_data, space_file, indent=3)
space_users_data = response.json().get("includes", {}).get("users", [])
space_user_fields = {
'id': 'id',
'name': 'name',
'username': 'username',
'created_at': 'created_at',
'location': 'location',
'protected': 'protected',
'public_metrics': 'public_metrics',
'description': 'description',
'entities': 'entities',
'pinned_tweet_id': 'pinned_tweet_id',
'profile_image_url': 'profile_image_url',
'url': 'url',
}
user_data = []
for user in space_users_data:
formatted_user = {user_key: user.get(user_value, '') for user_key, user_value in space_user_fields.items() if user_value not in ['url']}
user_data.append(formatted_user)
with open("space_user_data.json", "w") as space_user_file:
json.dump(user_data, space_user_file, indent=3)
df = pd.json_normalize(space_users_data)
eq = pd.json_normalize(space_data)
keys_to_exclude = [
'entities.url.urls',
'entities.description.mentions',
'entities.description.hashtags',
'profile_image_url',
'pinned_tweet_id',
]
rename_mapping = {
'public_metrics.followers_count': 'Followers',
'public_metrics.following_count': 'Following',
'public_metrics.tweet_count': 'Tweet',
'public_metrics.listed_count': 'Listed',
'public_metrics.like_count': 'Likes'
}
keys_to_expand = ['invited_user_ids', 'topic_ids', 'host_ids']
desired_order_space_data = [
'Space Title', 'Topics Ids', 'topic_des', 'topic_name', 'Space State', 'Host ID', 'Creator ID',
'Updated At', 'invited_user_ids', 'speakers', 'speakersn',
'Total Moderators', 'Created At', 'Ended At', 'Language', 'Subscriber Count'
]
eq = eq[desired_order_space_data]
desired_order_user_data = [
'id', 'name', 'username', 'created_at', 'location', 'protected',
'description', 'pinned_tweet_id',
'public_metrics.followers_count',
'public_metrics.following_count',
'public_metrics.tweet_count',
'public_metrics.listed_count',
'public_metrics.like_count',
]
df = df[desired_order_user_data]
for key in keys_to_expand:
if key in eq and not eq[key].empty and all(isinstance(item, list) and item for item in eq[key]):
new_columns = [f"{key}_{i + 1}" for i in range(len(eq[key].iloc[0]))]
eq[new_columns] = eq[key].apply(lambda x: pd.Series(x) if x else pd.Series([None] * len(new_columns)))
eq = eq.drop([key], axis=1, errors='ignore')
df.rename(columns=rename_mapping, inplace=True)
df.replace('', None, inplace=True)
df = df.drop(keys_to_exclude, axis=1, errors='ignore')
eq = eq.drop([], axis=1, errors='ignore')
eq.to_csv(f'{space_id}_data.csv', index=False)
df.to_csv(f'{space_id}_spaceData.csv', index=False, quoting=csv.QUOTE_NONNUMERIC)
return redirect('/report2.html')
else:
print(f"Error: {response.status_code}, {response.text}")
return render_template('index.html')
######################### End of HomePage Route ###################################################
############### Route For Home Page Using Space Title ##################################################
@load.route('/hi', methods=['GET', 'POST'])
def hi() -> Any:
"""Render the homepage with a different route and handle POST requests for space title submission."""
if request.method == 'POST':
space_id = request.form["spaceID"]
try:
with open(json_file_path, 'r') as json_file:
data = json.load(json_file)
except FileNotFoundError:
data = []
# Append the new space_id
data.append({'space_id': space_id})
# Write the updated data back to the JSON file
with open(json_file_path, 'w') as json_file:
json.dump(data, json_file, indent=2)
params = {
'query': space_id,
"space.fields": "created_at,lang,invited_user_ids,participant_count,scheduled_start,started_at,title,topic_ids,updated_at,speaker_ids,ended_at",
"user.fields": "created_at,description,entities,id,location,name,pinned_tweet_id,profile_image_url,protected,public_metrics,url,username,verified,verified_type,withheld",
"expansions": "host_ids,topic_ids,invited_user_ids,creator_id,speaker_ids"
}
headers = {
"Authorization": f"Bearer {bearer_token}",
"User-Agent": "v2SpacesSearchPython"
}
search_url = "https://api.twitter.com/2/spaces/search"
response = requests.request("GET", search_url, headers=headers, params=params)
if response.status_code == 200:
data = response.json()
space_data_list = data.get("data", [])
topics = data.get('includes', {}).get('topics', [])
formatted_spaces = []
for space_data in space_data_list:
custom_space_data = {
'Space Title': space_data.get('title', ''),
'Topics Ids': ', '.join(map(str, space_data.get('topic_ids', []))),
'topic_name': [topic.get('name', '') for topic in topics],
'topic_des': [topic.get('description', '') for topic in topics],
'Space State': space_data.get('state', ''),
'Host ID': space_data.get('host_ids', ''),
'Creator ID': space_data.get('creator_id', ''),
'Updated At': space_data.get('updated_at', ''),
'invited_user_ids': space_data.get('invited_user_ids', []),
'speakers': ', '.join(map(str, space_data.get('speaker_ids', []))),
'speakersn': len(space_data.get('speaker_ids', [])),
'Total Moderators': len(space_data.get('speaker_ids', [])),
'Created At': space_data.get('created_at', ''),
'Ended At': space_data.get('ended_at', ''),
'Language': space_data.get('lang', ''),
'Subscriber Count': space_data.get('participant_count', '')
}
formatted_spaces.append(custom_space_data)
for formatted_space in formatted_spaces:
for key, value in formatted_space.items():
space_data[key] = value
with open("space_data.json", "w") as space_file:
json.dump(space_data, space_file, indent=3)
space_users_data = data.get("includes", {}).get("users", [])
space_user_fields = {
'id': 'id',
'name': 'name',
'username': 'username',
'created_at': 'created_at',
'location': 'location',
'protected': 'protected',
'public_metrics': 'public_metrics',
'description': 'description',
'entities': 'entities',
'pinned_tweet_id': 'pinned_tweet_id',
'profile_image_url': 'profile_image_url',
'url': 'url',
}
user_data = []
for user in space_users_data:
formatted_user = {user_key: user.get(user_value, '') for user_key, user_value in space_user_fields.items() if user_value not in ['url']}
user_data.append(formatted_user)
with open("space_user_data.json", "w") as space_user_file:
json.dump(user_data, space_user_file, indent=3)
df = pd.json_normalize(space_users_data)
eq = pd.json_normalize(space_data)
keys_to_exclude = [
'entities.url.urls',
'entities.description.mentions',
'entities.description.hashtags',
'profile_image_url',
'pinned_tweet_id',
]
rename_mapping = {
'public_metrics.followers_count': 'Followers',
'public_metrics.following_count': 'Following',
'public_metrics.tweet_count': 'Tweet',
'public_metrics.listed_count': 'Listed',
'public_metrics.like_count': 'Likes'
}
keys_to_expand = ['invited_user_ids', 'topic_ids', 'host_ids']
desired_order_space_data = [
'Space Title', 'Topics Ids', 'topic_des', 'topic_name', 'Space State', 'Host ID', 'Creator ID',
'Updated At', 'invited_user_ids', 'speakers', 'speakersn',
'Total Moderators', 'Created At', 'Ended At', 'Language', 'Subscriber Count'
]
eq = eq[desired_order_space_data]
desired_order_user_data = [
'id', 'name', 'username', 'created_at', 'location', 'protected',
'description', 'pinned_tweet_id',
'public_metrics.followers_count',
'public_metrics.following_count',
'public_metrics.tweet_count',
'public_metrics.listed_count',
'public_metrics.like_count',
]
df = df[desired_order_user_data]
for key in keys_to_expand:
if key in eq and not eq[key].empty and all(isinstance(item, list) and item for item in eq[key]):
new_columns = [f"{key}_{i + 1}" for i in range(len(eq[key].iloc[0]))]
eq[new_columns] = eq[key].apply(lambda x: pd.Series(x) if x else pd.Series([None] * len(new_columns)))
eq = eq.drop([key], axis=1, errors='ignore')
df.rename(columns=rename_mapping, inplace=True)
df.replace('', None, inplace=True)
df = df.drop(keys_to_exclude, axis=1, errors='ignore')
eq = eq.drop([], axis=1, errors='ignore')
eq.to_csv(f'{space_id}_data.csv', index=False)
df.to_csv(f'{space_id}_spaceData.csv', index=False, quoting=csv.QUOTE_NONNUMERIC)
return redirect('/report2.html')
else:
print(f"Error: {response.status_code}, {response.text}")
return render_template('index.html')
######################### End of HomePage Route ###################################################
#######################################Route For The Report Page #######################################
@load.route('/report.html', methods=['GET'])
def report() -> str:
"""Render the report page."""
return render_template('report.html')
################################ End of Report Page ####################################################
@load.route('/report2.html', methods=['GET'])
def report2() -> str:
"""Render an alternative report page."""
return render_template('report2.html')
################################ End of Report Page ####################################################
######################################### Route For Testing #####################################################
@load.route('/test', methods=['GET'])
def test() -> str:
"""Render the test page."""
return render_template('test.html')
######################### End of Test Page Route ###################################################
######################################### Route For Downloading User Report #####################################
@load.route('/download', methods=['GET'])
def download() -> Any:
"""Download a zip file containing the user report."""
df = pd.read_json('space_user_data.json')
if df.empty:
return "Error: No data found to create a CSV file."
csv_buffer = StringIO()
df.to_csv(csv_buffer, index=False)
csv_buffer.seek(0)
zip_buffer = BytesIO()
with zipfile.ZipFile(zip_buffer, 'w') as zip_file:
zip_file.writestr('space_user_data.csv', csv_buffer.getvalue())
zip_buffer.seek(0)
return send_file(zip_buffer, mimetype='application/zip', as_attachment=True, download_name='user_report.zip')
######################### End of Download Page Route ###################################################
######################################### Route For Downloading Topic Report #####################################
@load.route('/download2', methods=['GET'])
def download2() -> Any:
"""Download a zip file containing the topic report."""
df = pd.read_json('space_data.json')
if df.empty:
return "Error: No data found to create a CSV file."
csv_buffer = StringIO()
df.to_csv(csv_buffer, index=False)
csv_buffer.seek(0)
zip_buffer = BytesIO()
with zipfile.ZipFile(zip_buffer, 'w') as zip_file:
zip_file.writestr('space_data.csv', csv_buffer.getvalue())
zip_buffer.seek(0)
return send_file(zip_buffer, mimetype='application/zip', as_attachment=True, download_name='topic_report.zip')
######################### End of Download Page Route ###################################################
if __name__ == "__main__":
load.run(debug=True)