Spaces:
Runtime error
Runtime error
File size: 3,862 Bytes
751f2b0 |
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 |
import json
import requests
# make sure that year can be from 2018 to current year
class LatestData:
def __init__(self, year):
self.year = year
self.data = self.get_f1_data()
self.events = self.get_events()
def get_f1_data(self):
response = requests.get(
f"https://livetiming.formula1.com/static/{self.year}/Index.json", timeout=5)
if response.status_code == 200:
try:
data = response.content.decode("utf-8-sig")
return json.loads(data)
except json.JSONDecodeError as e:
print("Failed to parse JSON data:", e)
return None
else:
print("Failed to get data. Status code:", response.status_code)
return None
def get_events(self):
events = []
for meeting in self.data['Meetings']:
events.append(meeting['Name'])
return events
def get_sessions(self, event):
sessions = []
for meeting in self.data['Meetings']:
if meeting['Name'] == event:
for session in meeting['Sessions']:
sessions.append(session['Name'])
return sessions
def team_colors(year: int) -> dict:
team_colors = {}
if year == 2023:
team_colors = {
"Red Bull Racing": "#ffe119",
"Ferrari": "#e6194b",
"Aston Martin": "#3cb44b",
"Mercedes": "#00c0bf",
"Alpine": "#f032e6",
"Haas F1 Team": "#ffffff",
"McLaren": "#f58231",
"Alfa Romeo": "#800000",
"AlphaTauri": "#dcbeff",
"Williams": "#4363d8",
}
if year == 2022:
team_colors = {
"Red Bull Racing": "#ffe119",
"Ferrari": "#e6194b",
"Aston Martin": "#3cb44b",
"Mercedes": "#00c0bf",
"Alpine": "#f032e6",
"Haas F1 Team": "#ffffff",
"McLaren": "#f58231",
"Alfa Romeo": "#800000",
"AlphaTauri": "#dcbeff",
"Williams": "#4363d8",
}
if year == 2021:
team_colors = {
"Red Bull Racing": "#ffe119",
"Mercedes": "#00c0bf",
"Ferrari": "#e6194b",
"Alpine": "#f032e6",
"McLaren": "#f58231",
"Alfa Romeo Racing": "#800000",
"Aston Martin": "#3cb44b",
"Haas F1 Team": "#ffffff",
"AlphaTauri": "#dcbeff",
"Williams": "#4363d8",
}
if year == 2020:
team_colors = {
"Red Bull Racing": "#000099",
"Renault": "#ffe119",
"Racing Point": "#f032e6",
"Mercedes": "#00c0bf",
"Ferrari": "#e6194b",
"McLaren": "#f58231",
"Alfa Romeo Racing": "#800000",
"Haas F1 Team": "#ffffff",
"AlphaTauri": "#dcbeff",
"Williams": "#4363d8",
}
if year == 2019:
team_colors = {
"Red Bull Racing": "#000099",
"Renault": "#ffe119",
"Racing Point": "#f032e6",
"Toro Rosso": "#dcbeff",
"Mercedes": "#00c0bf",
"Ferrari": "#e6194b",
"McLaren": "#f58231",
"Alfa Romeo Racing": "#800000",
"Haas F1 Team": "#ffffff",
"Williams": "#4363d8",
}
if year == 2018:
team_colors = {
"Red Bull Racing": "#000099",
"Renault": "#ffe119",
"Toro Rosso": "#dcbeff",
"Force India": "#f032e6",
"Sauber": "#800000",
"Mercedes": "#00c0bf",
"Ferrari": "#e6194b",
"McLaren": "#f58231",
"Haas F1 Team": "#ffffff",
"Williams": "#4363d8",
}
return team_colors
|