-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
396 lines (336 loc) · 13.1 KB
/
main.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
import asyncio
from flask import Flask
from flask_cors import CORS
from flask_sslify import SSLify
from threading import Thread
import os
from dotenv import load_dotenv
from flask import jsonify, request
import fastf1
import fastf1.plotting
import pandas as pd
import matplotlib as mpl
from datetime import datetime as dt
import datetime
from pymongo import MongoClient
from update import *
import warnings
import platform
import traceback
from utils import *
load_dotenv()
FUNCS = os.getenv("FUNCS")
if FUNCS == "aws":
from funcs_aws import *
elif FUNCS == "cache":
from funcs import *
HTTPS = os.getenv("HTTPS")
aws_url = os.getenv("aws_url")
IDS = os.getenv("IDS")
PY = os.getenv("PY")
connection_string = os.getenv("connection_string")
db_name = os.getenv("db_name")
warnings.filterwarnings("ignore", category=FutureWarning)
platform.system()
mpl.use('Agg')
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
# set mpl font
set_font()
# enable cache
if os.path.exists(dir_path + get_path() + "doc_cache"):
fastf1.Cache.enable_cache(dir_path + get_path() + "doc_cache")
# delete all files
def delete_all():
folder_path = dir_path + get_path() + "res" + get_path() + "output"
# Get a list of all files in the folder
file_list = os.listdir(folder_path)
# Loop through each file and delete it
for file_name in file_list:
file_path = os.path.join(folder_path, file_name)
os.remove(file_path)
# method that logs data from slash commands
def log(user_id, func, message, exc, flag, datetime):
datetime = datetime.replace("-", " ").replace(".", ":")
id_flg = False
for i in IDS:
if i in user_id:
id_flg = True
break
if (not id_flg and not flag):
collection_name = "logs"
elif (not id_flg and flag):
collection_name = "exc"
elif (user_id in IDS and not flag):
collection_name = "devlogs"
elif (user_id in IDS and flag):
collection_name = "devexc"
client = MongoClient(connection_string)
db = client[db_name]
collection = db[collection_name]
print(message)
comm = func
inputs = message
if not exc:
collection.insert_one({
"name": user_id,
"command": comm,
"inputs": inputs,
"datetime": datetime})
else:
collection.insert_one({
"name": user_id,
"command": comm,
"inputs": inputs,
"exception": exc,
"datetime": datetime,
})
# fix exception string
def fix_exc(exc, input_list, comm):
yr = input_list["year"]
try:
rc = input_list["race"]
if type(rc) == int:
rc = int(rc)
except:
rc = None
try:
sn = input_list["session"]
except:
sn = None
if exc.__contains__("The data you are trying to access has not been loaded yet.") and yr < 2018:
exc = "The FastF1 API has data starting from 2018. Please make sure you provided all the inputs correctly and try again.\n"
elif exc.__contains__("The data you are trying to access has not been loaded yet.") and (yr < datetime.datetime.now().year and yr >= 2018):
if rc != None:
if type(rc) == int:
exc = "I can't find any F1 data for the requested grand prix (" + str(yr) + " Round " + str(rc) + ").\n"
else:
exc = "I can't find any F1 data for the requested grand prix (" + str(yr) + " " + str(rc) + ").\n"
else:
exc = "I can't find any F1 data for the requested Grand Prix. It may have not been loaded yet.\n"
elif exc.__contains__("The data you are trying to access has not been loaded yet.") and yr >= datetime.datetime.now().year:
if rc != None and sn != None:
if type(rc) == int:
exc = "I can't find any F1 data for the requested grand prix (" + str(yr) + " Round " + str(rc) + " " + sn + "). It may have not been loaded yet.\n"
else:
exc = "I can't find any F1 data for the requested grand prix (" + str(yr) + " " + str(rc) + " " + sn + "). It may have not been loaded yet.\n"
else:
exc = "I can't find any F1 data for the requested Grand Prix. It may have not been loaded yet.\n"
elif exc.__contains__("'code'") or exc.__contains__("integer division or modulo by zero"):
exc = "There requested data is not available for the year " + str(yr) + ".\n"
elif exc.__contains__("cannot find race"):
exc = "I can't find the requested race (" + str(rc) + "). Please make sure you provided all the inputs correctly and try again.\n"
elif exc.__contains__("Invalid session type"):
exc = "I can't find the requested session (" + input_list["session"] + "). Please make sure you provided all the inputs correctly and try again.\n"
elif exc.__contains__("Invalid driver identifier"):
exc = "I can't find the requested driver(s). Make sure to provide a driver abbreviation, like 'VER' or 'LEC'.\n"
elif exc.__contains__("None of [Index") and exc.__contains__("are in the [columns]") and comm == "tires":
exc = "An error has occured. Try a different lap number\n"
elif exc.__contains__("single positional indexer is out-of-bounds") or exc=="0" or exc.__contains__ ("'Lap' object has no attribute 'session'") or exc.__contains__("attempt to get argmin of an empty sequence") or exc == "":
exc = "An unknown error occured. Please make sure you provided all the command inputs correctly.\n"
elif exc.__contains__("Cannot connect to host") or exc.__contains__("Unauthorized") or exc.__contains__("HTTP"):
exc = "A network error has occured. Please try again later.\n"
elif exc.__contains__("Expecting value: line 1 column 1 (char 0)"):
exc = "An error occured while fetching data from the API. Please try again later.\n"
else:
exc = "An unknown error occured.\n"
return exc
# command
async def command(user_id, input_list, comm, datetime):
try:
inputs = ""
message = ""
for i in input_list:
inputs += i + " " + str(input_list[i]) + " "
message = comm + " " + inputs
print("STARTED " + message + " " + datetime)
if comm == "drivers" or comm == "constructors":
res = aws_api.get_standings(comm, input_list["year"])
return res
elif comm == "points":
res = aws_api.get_points(input_list["year"])
return res
else:
if comm == "fastest":
res = fastest_func(input_list, datetime)
elif comm == "results":
res = results_func(input_list, datetime)
elif comm == "schedule":
res = schedule_func(input_list, datetime)
elif comm == "event":
res = event_func(input_list, datetime)
elif comm == "laps":
res = laps_func(input_list, datetime)
elif comm == "time":
res = time_func(input_list, datetime)
elif comm == "distance":
res = distance_func(input_list, datetime)
elif comm == "delta":
res = delta_func(input_list, datetime)
elif comm == "gear":
res = gear_func(input_list, datetime)
elif comm == "speed":
res = speed_func(input_list, datetime)
elif comm == "telemetry":
res = tel_func(input_list, datetime)
elif comm == "cornering":
res = cornering_func(input_list, datetime)
elif comm == "tires":
res = tires_func(input_list, datetime)
elif comm == "strategy":
res = strategy_func(input_list, datetime)
elif comm == "sectors":
res = sectors_func(input_list, datetime)
elif comm == "racetrace":
res = rt_func(input_list, datetime)
elif comm == "positions":
res = positions_func(input_list, datetime)
elif comm == "battles":
res = battles_func(input_list, datetime)
if res !="success" or res == None:
raise Exception("Internal Server Error. Please try again.")
else:
print("FINISHED " + message + " " + datetime)
exc = ""
flag = False
except Exception as exc:
exc = str(exc)
flag = True
if datetime in queue:
queue.remove(datetime)
print("FAILED " + message + " " + datetime + " ")
print(traceback.format_exc())
exc = fix_exc(exc, input_list, comm)
raise Exception(exc)
try:
log(user_id, message, exc, flag, datetime)
except:
pass
return datetime
# flask server
app = Flask('', static_folder='res')
CORS(app)
if HTTPS == "true":
sslify = SSLify(app)
async def update_helper():
yr = dt.datetime.now().year
stnd = ""
races = ""
data = ""
try:
# PY is string like "py" or"python" or "ptyhon3", etc.
os.system(PY + " stnd.py")
stnd = "stnd success"
except:
stnd = "stnd fail"
try:
update_races(yr)
races = "race success"
except:
races = "races fail"
try:
data = str(update_data(yr)) + "\naws success"
except:
data = "data fail"
return stnd + "<br />" + races + "<br />" + data
async def main_helper(request):
try:
data = request.get_json()
func_name = data.get('func_name')
input_list = data.get('input_list')
user_id = data.get('user_id')
datetime = get_datetime()
if not (func_name.lower() == "drivers" or func_name.lower() == "constructors" or func_name.lower() == "points"):
res = await command(user_id, input_list, func_name.lower(), datetime)
with open("res/output/" + res + ".png", "rb") as image:
f = image.read()
b = bytearray(f)
result = list(b)
os.remove("res/output/" + res + ".png")
else:
res = await command(user_id, input_list, func_name.lower(), datetime)
with open("res/output/" + res, "rb") as image:
f = image.read()
b = bytearray(f)
result = list(b)
os.remove("res/output/" + res)
try:
log(user_id, func_name, input_list, "", False, datetime)
except:
pass
return jsonify({'result': result, 'datetime': datetime}), 200
except Exception as exc:
return jsonify({'error': str(exc)}), 400
async def inputs_helper(request):
try:
data = request.get_json()
input_type = data.get('input')
input_data = data.get('data')
if input_type == "years":
try:
res = get_years(input_data["func"])
except:
res = []
elif input_type == "races":
try:
res = get_races_from_db(input_data["func"], input_data["year"])
except:
res = []
elif input_type == "sessions":
try:
res = get_sessions_from_db(input_data["year"], input_data["race"])
except:
res = []
elif input_type == "all":
try:
res = [[], "", ""]
res[0] = get_drivers_from_db(input_data["year"], input_data["race"], input_data["session"])
res[1] = get_laps_from_db(input_data["year"], input_data["race"], input_data["session"])
res[2] = get_distance_from_db(input_data["year"], input_data["race"], input_data["session"])
except:
print(traceback.format_exc())
res = []
return jsonify({'result': res}), 200
except Exception as exc:
return jsonify({'error': str(exc)}), 400
@app.route('/health', methods=['GET'])
def health_check():
return jsonify(status="UP"), 200
# update data
@app.route('/update', methods=['GET', 'POST'])
def update():
return asyncio.run(update_helper())
# execute function when user submits form
@app.route('/', methods=['GET', 'POST'])
def home():
if request.method == 'POST':
try:
return asyncio.run(main_helper(request))
except Exception as exc:
return jsonify({'error': str(exc)}), 400
else:
return "Backend is running."
# get inputs
@app.route('/inputs', methods=['GET', 'POST'])
def inputs():
if request.method == 'POST':
try:
return asyncio.run(inputs_helper(request))
except Exception as exc:
return jsonify({'error': str(exc)}), 400
# run the server
def run():
try:
delete_all()
except:
pass
if HTTPS == "true":
app.run(ssl_context=('cert/fullchain.pem', 'cert/privkey.pem'), host='0.0.0.0',port=5000)
else:
app.run(host='0.0.0.0',port=5000)
# keep the server running
def keep_alive():
t = Thread(target=run)
t.start()
# keep the server running
keep_alive()