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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
"Red Bull Racing Honda RBPT": "#ffe119",
|
| 63 |
+
"Ferrari": "#e6194b",
|
| 64 |
+
"Aston Martin Aramco Mercedes": "#3cb44b",
|
| 65 |
+
"Mercedes": "#00c0bf",
|
| 66 |
+
"Alpine Renault": "#f032e6",
|
| 67 |
+
"Haas Ferrari": "#ffffff",
|
| 68 |
+
"McLaren Mercedes": "#f58231",
|
| 69 |
+
"Alfa Romeo Ferrari": "#800000",
|
| 70 |
+
"AlphaTauri Honda RBPT": "#dcbeff",
|
| 71 |
+
"Williams Mercedes": "#4363d8",
|
| 72 |
+
"Red Bull": "#ffe119",
|
| 73 |
+
"Alpine F1 Team": "#f032e6",
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
}
|
| 78 |
+
if year == 2022:
|
| 79 |
+
|
| 80 |
+
team_colors = {
|
| 81 |
+
"Red Bull Racing": "#ffe119",
|
| 82 |
+
"Ferrari": "#e6194b",
|
| 83 |
+
"Aston Martin": "#3cb44b",
|
| 84 |
+
"Mercedes": "#00c0bf",
|
| 85 |
+
"Alpine": "#f032e6",
|
| 86 |
+
"Haas F1 Team": "#ffffff",
|
| 87 |
+
"McLaren": "#f58231",
|
| 88 |
+
"Alfa Romeo": "#800000",
|
| 89 |
+
"AlphaTauri": "#dcbeff",
|
| 90 |
+
"Williams": "#4363d8",
|
| 91 |
+
|
| 92 |
+
"Red Bull": "#ffe119",
|
| 93 |
+
"Alpine F1 Team": "#f032e6",
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
if year == 2021:
|
| 99 |
+
|
| 100 |
+
team_colors = {
|
| 101 |
+
"Red Bull Racing": "#ffe119",
|
| 102 |
+
"Mercedes": "#00c0bf",
|
| 103 |
+
"Ferrari": "#e6194b",
|
| 104 |
+
"Alpine": "#f032e6",
|
| 105 |
+
"McLaren": "#f58231",
|
| 106 |
+
"Alfa Romeo Racing": "#800000",
|
| 107 |
+
"Aston Martin": "#3cb44b",
|
| 108 |
+
"Haas F1 Team": "#ffffff",
|
| 109 |
+
"AlphaTauri": "#dcbeff",
|
| 110 |
+
"Williams": "#4363d8",
|
| 111 |
+
|
| 112 |
+
"Red Bull": "#ffe119",
|
| 113 |
+
"Alpine F1 Team": "#f032e6",
|
| 114 |
+
"Alfa Romeo": "#800000",
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
if year == 2020:
|
| 120 |
+
|
| 121 |
+
team_colors = {
|
| 122 |
+
"Red Bull Racing": "#000099",
|
| 123 |
+
"Renault": "#ffe119",
|
| 124 |
+
"Racing Point": "#f032e6",
|
| 125 |
+
"Mercedes": "#00c0bf",
|
| 126 |
+
"Ferrari": "#e6194b",
|
| 127 |
+
"McLaren": "#f58231",
|
| 128 |
+
"Alfa Romeo Racing": "#800000",
|
| 129 |
+
"Haas F1 Team": "#ffffff",
|
| 130 |
+
"AlphaTauri": "#dcbeff",
|
| 131 |
+
"Williams": "#4363d8",
|
| 132 |
+
|
| 133 |
+
"Red Bull": "#000099",
|
| 134 |
+
"Alfa Romeo": "#800000",
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
if year == 2019:
|
| 140 |
+
|
| 141 |
+
team_colors = {
|
| 142 |
+
"Red Bull Racing": "#000099",
|
| 143 |
+
"Renault": "#ffe119",
|
| 144 |
+
"Racing Point": "#f032e6",
|
| 145 |
+
"Toro Rosso": "#dcbeff",
|
| 146 |
+
"Mercedes": "#00c0bf",
|
| 147 |
+
"Ferrari": "#e6194b",
|
| 148 |
+
"McLaren": "#f58231",
|
| 149 |
+
"Alfa Romeo Racing": "#800000",
|
| 150 |
+
"Haas F1 Team": "#ffffff",
|
| 151 |
+
"Williams": "#4363d8",
|
| 152 |
+
|
| 153 |
+
"Red Bull": "#000099",
|
| 154 |
+
"Alfa Romeo": "#800000",
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
+
if year == 2018:
|
| 160 |
+
|
| 161 |
+
team_colors = {
|
| 162 |
+
"Red Bull Racing": "#000099",
|
| 163 |
+
"Renault": "#ffe119",
|
| 164 |
+
"Toro Rosso": "#dcbeff",
|
| 165 |
+
"Force India": "#f032e6",
|
| 166 |
+
"Sauber": "#800000",
|
| 167 |
+
"Mercedes": "#00c0bf",
|
| 168 |
+
"Ferrari": "#e6194b",
|
| 169 |
+
"McLaren": "#f58231",
|
| 170 |
+
"Haas F1 Team": "#ffffff",
|
| 171 |
+
"Williams": "#4363d8",
|
| 172 |
+
|
| 173 |
+
"Red Bull": "#000099",
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
}
|
| 178 |
+
|
| 179 |
+
return team_colors
|