-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
135 lines (122 loc) · 6.68 KB
/
main.py
File metadata and controls
135 lines (122 loc) · 6.68 KB
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
from flask import Flask, render_template
import requests
import os
import json
import arrow
cwd = os.path.abspath(os.getcwd()).replace("\\","/")
appCounter = 0
app = Flask(__name__)
def download_image(url, destination):
data = requests.get(url)
with open(destination, "wb") as f:
f.write(data.content)
def hexToRGB(value):
value = value.lstrip('#')
lv = len(value)
return tuple(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3))
def getThumbnails(day, month, year):
driverData = requests.get('https://api.openf1.org/v1/drivers?session_key=latest').json()
for i in driverData:
code = i["name_acronym"]
if not os.path.exists(f"{cwd}/static/driverThumbnails/{code}.png"):
file = open(f"{cwd}/static/driverThumbnails/{code}.png","x")
file.close()
download_image(i["headshot_url"],f"{cwd}/static/driverThumbnails/{code}.png")
with open(f"{cwd}/appData.json","r") as f:
appData = json.load(f)
appData["yearUpdated"] = year
appData["monthUpdated"] = month
appData["dayUpdated"] = day
with open("appData.json","w") as f:
json.dump(appData,f)
def setup():
with open("appData.json","r") as f:
appData = json.load(f)
utc = arrow.utcnow()
year = int(utc.format("YYYY"))
month = int(utc.format("MM"))
day = int(utc.format("DD"))
if appData["yearUpdated"] != year or appData["monthUpdated"] != month or appData["dayUpdated"] != day:
thumbnails = os.listdir(f"{cwd}/static/driverThumbnails")
for file in thumbnails:
filePath = f"{cwd}/static/driverThumbnails/{file}"
if os.path.isfile(filePath):
os.remove(filePath)
getThumbnails(day, month, year)
def getChampionshipsStandings(): #requests all data and then returns needed data.
#constructorStanding and driverStanding are the leaderboards,
#driverData provides extra needed data like team colours.
constructorStanding = requests.get('https://api.jolpi.ca/ergast/f1/2025/constructorstandings/').json()["MRData"]["StandingsTable"]["StandingsLists"][0]["ConstructorStandings"]
driverStanding = requests.get('https://api.jolpi.ca/ergast/f1/2025/driverstandings/').json()["MRData"]["StandingsTable"]["StandingsLists"][0]["DriverStandings"]
driverData = requests.get('https://api.openf1.org/v1/drivers?session_key=latest').json()
constructorTable = []
constructorTable.append(["Position", "Name", "Points"]) #adds header row to constructorTable
for constructor in constructorStanding:
name = constructor["Constructor"]["name"]
if name.split(" ")[0] == "RB": #Renaming RB to Racing Bulls to match driverData
name = "Racing Bulls"
position = constructor["position"]
points = constructor["points"]
colour = "#FFFFFF" #Default if real colour couldn't be found.
count = 0
done = False
while not done and count < len(driverData): #Use while so that can exit once colour has been found
driver = driverData[count] # will select every driver from driverData until finds one from the current team.
if (name != "Racing Bulls" and name.split()[0] in driver["team_name"]) or (name == "Racing Bulls" and driver["team_name"] == "Racing Bulls"): #Checks first word of current team's name is in the driverData driver's team name. For some reason Alpine didn't work otherwise. Exception is Racing Bulls as Racing is also in Red Bull Racing
teamColour = "#" + driver["team_colour"]
done = True #don't have to check more drivers as have found the team colour
count += 1
constructorTable.append([position, name, points, hexToRGB(teamColour)]) #adds all of the data as a row in the constructorTable list. teamColour won't be displayed, just used for row colour
tempDriverTable = []
tempDriverTable.append(["", "Position", "Number", "Points", "Wins",]) #adds header row to tempDriverTable
for driver in driverStanding:
name = f"{driver["Driver"]["givenName"]} {driver["Driver"]["familyName"]}"
position = driver["position"]
points = driver["points"]
wins = driver["wins"]
number = driver["Driver"]["permanentNumber"]
constructor = driver["Constructors"][0]["name"]
if name == "Max Verstappen":
number = "1" #Max's permanent number is 33 but he is current world champion so gets number 1.
code = driver["Driver"]["code"] #Eg VER for Verstappen.
thumbnailPath = f"/static/driverThumbnails/{code}.png"
teamColour = "#FFFFFF" #default colour if driver's team colour can't be found.
count = 0
done = False
while not done and count < len(driverData): #Use while so that can exit once colour found.
selectedDriver = driverData[count]
if code == selectedDriver["name_acronym"]: #check is the same driver by comparing the driver code/acronym (eg VER for Verstappen) - more unique than first name.
teamColour = "#" + selectedDriver["team_colour"]
done = True #don't have to check more driverData drivers as have found the one we need.
count += 1
tempDriverTable.append([position,number,points,wins,thumbnailPath,hexToRGB(teamColour)]) #adds all of the data as a row in the tempDriverTable list. teamColour only used for row colour, not displayed as text.
driverTable = [tempDriverTable[0]]
del tempDriverTable[0]
numOfDrivers = len(tempDriverTable)
numOfRows = numOfDrivers//2
for i in range(numOfRows):
row = []
row.append(tempDriverTable[i])
row.append(tempDriverTable[i+numOfRows])
driverTable.append(row)
return constructorTable, driverTable #returns both lists
def getdata(): #gets all data in format to be passed to tables.
constructorTable, driverTable = getChampionshipsStandings()
constructorHeadings = constructorTable[0]
constructorData = constructorTable[1:]
driverHeadings = driverTable[0]
driverData = driverTable[1:]
return constructorHeadings, constructorData, driverHeadings, driverData
@app.route("/")
def table():
global appCounter
appCounter += 1
if appCounter >= 144:
setup()
appCounter = 0
constructorHeadings, constructorData, driverHeadings, driverData = getdata()
print(driverData)
timeout = 3600000 #change timeout based on whether it is a session or not.
return render_template("tables.html", constructorHeadings=constructorHeadings, constructorData=constructorData, driverHeadings=driverHeadings, driverData=driverData, timeout=timeout) #renders the html file, passing it the table headers and data, and telling how long before reload.
setup()
app.run()