Spaces:
Runtime error
Runtime error
tracinginsights
commited on
Commit
•
751f2b0
1
Parent(s):
2d12f2a
Create available_data.py
Browse files- available_data.py +143 -0
available_data.py
ADDED
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
|
3 |
+
import requests
|
4 |
+
|
5 |
+
|
6 |
+
# make sure that year can be from 2018 to current year
|
7 |
+
class LatestData:
|
8 |
+
|
9 |
+
def __init__(self, year):
|
10 |
+
|
11 |
+
self.year = year
|
12 |
+
self.data = self.get_f1_data()
|
13 |
+
self.events = self.get_events()
|
14 |
+
|
15 |
+
def get_f1_data(self):
|
16 |
+
response = requests.get(
|
17 |
+
f"https://livetiming.formula1.com/static/{self.year}/Index.json", timeout=5)
|
18 |
+
if response.status_code == 200:
|
19 |
+
try:
|
20 |
+
data = response.content.decode("utf-8-sig")
|
21 |
+
return json.loads(data)
|
22 |
+
except json.JSONDecodeError as e:
|
23 |
+
print("Failed to parse JSON data:", e)
|
24 |
+
return None
|
25 |
+
else:
|
26 |
+
print("Failed to get data. Status code:", response.status_code)
|
27 |
+
return None
|
28 |
+
|
29 |
+
def get_events(self):
|
30 |
+
events = []
|
31 |
+
for meeting in self.data['Meetings']:
|
32 |
+
events.append(meeting['Name'])
|
33 |
+
|
34 |
+
return events
|
35 |
+
|
36 |
+
def get_sessions(self, event):
|
37 |
+
sessions = []
|
38 |
+
for meeting in self.data['Meetings']:
|
39 |
+
if meeting['Name'] == event:
|
40 |
+
for session in meeting['Sessions']:
|
41 |
+
sessions.append(session['Name'])
|
42 |
+
|
43 |
+
return sessions
|
44 |
+
|
45 |
+
|
46 |
+
def team_colors(year: int) -> dict:
|
47 |
+
team_colors = {}
|
48 |
+
|
49 |
+
if year == 2023:
|
50 |
+
team_colors = {
|
51 |
+
"Red Bull Racing": "#ffe119",
|
52 |
+
"Ferrari": "#e6194b",
|
53 |
+
"Aston Martin": "#3cb44b",
|
54 |
+
"Mercedes": "#00c0bf",
|
55 |
+
"Alpine": "#f032e6",
|
56 |
+
"Haas F1 Team": "#ffffff",
|
57 |
+
"McLaren": "#f58231",
|
58 |
+
"Alfa Romeo": "#800000",
|
59 |
+
"AlphaTauri": "#dcbeff",
|
60 |
+
"Williams": "#4363d8",
|
61 |
+
}
|
62 |
+
if year == 2022:
|
63 |
+
|
64 |
+
team_colors = {
|
65 |
+
"Red Bull Racing": "#ffe119",
|
66 |
+
"Ferrari": "#e6194b",
|
67 |
+
"Aston Martin": "#3cb44b",
|
68 |
+
"Mercedes": "#00c0bf",
|
69 |
+
"Alpine": "#f032e6",
|
70 |
+
"Haas F1 Team": "#ffffff",
|
71 |
+
"McLaren": "#f58231",
|
72 |
+
"Alfa Romeo": "#800000",
|
73 |
+
"AlphaTauri": "#dcbeff",
|
74 |
+
"Williams": "#4363d8",
|
75 |
+
|
76 |
+
}
|
77 |
+
|
78 |
+
if year == 2021:
|
79 |
+
|
80 |
+
team_colors = {
|
81 |
+
"Red Bull Racing": "#ffe119",
|
82 |
+
"Mercedes": "#00c0bf",
|
83 |
+
"Ferrari": "#e6194b",
|
84 |
+
"Alpine": "#f032e6",
|
85 |
+
"McLaren": "#f58231",
|
86 |
+
"Alfa Romeo Racing": "#800000",
|
87 |
+
"Aston Martin": "#3cb44b",
|
88 |
+
"Haas F1 Team": "#ffffff",
|
89 |
+
"AlphaTauri": "#dcbeff",
|
90 |
+
"Williams": "#4363d8",
|
91 |
+
|
92 |
+
|
93 |
+
}
|
94 |
+
|
95 |
+
if year == 2020:
|
96 |
+
|
97 |
+
team_colors = {
|
98 |
+
"Red Bull Racing": "#000099",
|
99 |
+
"Renault": "#ffe119",
|
100 |
+
"Racing Point": "#f032e6",
|
101 |
+
"Mercedes": "#00c0bf",
|
102 |
+
"Ferrari": "#e6194b",
|
103 |
+
"McLaren": "#f58231",
|
104 |
+
"Alfa Romeo Racing": "#800000",
|
105 |
+
"Haas F1 Team": "#ffffff",
|
106 |
+
"AlphaTauri": "#dcbeff",
|
107 |
+
"Williams": "#4363d8",
|
108 |
+
}
|
109 |
+
|
110 |
+
if year == 2019:
|
111 |
+
|
112 |
+
team_colors = {
|
113 |
+
"Red Bull Racing": "#000099",
|
114 |
+
"Renault": "#ffe119",
|
115 |
+
"Racing Point": "#f032e6",
|
116 |
+
"Toro Rosso": "#dcbeff",
|
117 |
+
"Mercedes": "#00c0bf",
|
118 |
+
"Ferrari": "#e6194b",
|
119 |
+
"McLaren": "#f58231",
|
120 |
+
"Alfa Romeo Racing": "#800000",
|
121 |
+
"Haas F1 Team": "#ffffff",
|
122 |
+
"Williams": "#4363d8",
|
123 |
+
|
124 |
+
}
|
125 |
+
|
126 |
+
if year == 2018:
|
127 |
+
|
128 |
+
team_colors = {
|
129 |
+
"Red Bull Racing": "#000099",
|
130 |
+
"Renault": "#ffe119",
|
131 |
+
"Toro Rosso": "#dcbeff",
|
132 |
+
"Force India": "#f032e6",
|
133 |
+
"Sauber": "#800000",
|
134 |
+
"Mercedes": "#00c0bf",
|
135 |
+
"Ferrari": "#e6194b",
|
136 |
+
"McLaren": "#f58231",
|
137 |
+
"Haas F1 Team": "#ffffff",
|
138 |
+
"Williams": "#4363d8",
|
139 |
+
|
140 |
+
|
141 |
+
}
|
142 |
+
|
143 |
+
return team_colors
|