tracinginsights/2020
0
1import json2 3import requests4 5 6# make sure that year can be from 2018 to current year7class LatestData:8 9 def __init__(self, year):10 11 self.year = year12 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 None25 else:26 print("Failed to get data. Status code:", response.status_code)27 return None28 29 def get_events(self):30 events = []31 for meeting in self.data['Meetings']:32 events.append(meeting['Name'])33 34 return events35 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 sessions44 45 46def 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 }73 if year == 2022:74 75 team_colors = {76 "Red Bull Racing": "#ffe119",77 "Ferrari": "#e6194b",78 "Aston Martin": "#3cb44b",79 "Mercedes": "#00c0bf",80 "Alpine": "#f032e6",81 "Haas F1 Team": "#ffffff",82 "McLaren": "#f58231",83 "Alfa Romeo": "#800000",84 "AlphaTauri": "#dcbeff",85 "Williams": "#4363d8",86 87 }88 89 if year == 2021:90 91 team_colors = {92 "Red Bull Racing": "#ffe119",93 "Mercedes": "#00c0bf",94 "Ferrari": "#e6194b",95 "Alpine": "#f032e6",96 "McLaren": "#f58231",97 "Alfa Romeo Racing": "#800000",98 "Aston Martin": "#3cb44b",99 "Haas F1 Team": "#ffffff",100 "AlphaTauri": "#dcbeff",101 "Williams": "#4363d8",102 103 104 }105 106 if year == 2020:107 108 team_colors = {109 "Red Bull Racing": "#000099",110 "Renault": "#ffe119",111 "Racing Point": "#f032e6",112 "Mercedes": "#00c0bf",113 "Ferrari": "#e6194b",114 "McLaren": "#f58231",115 "Alfa Romeo Racing": "#800000",116 "Haas F1 Team": "#ffffff",117 "AlphaTauri": "#dcbeff",118 "Williams": "#4363d8",119 }120 121 if year == 2019:122 123 team_colors = {124 "Red Bull Racing": "#000099",125 "Renault": "#ffe119",126 "Racing Point": "#f032e6",127 "Toro Rosso": "#dcbeff",128 "Mercedes": "#00c0bf",129 "Ferrari": "#e6194b",130 "McLaren": "#f58231",131 "Alfa Romeo Racing": "#800000",132 "Haas F1 Team": "#ffffff",133 "Williams": "#4363d8",134 135 }136 137 if year == 2018:138 139 team_colors = {140 "Red Bull Racing": "#000099",141 "Renault": "#ffe119",142 "Toro Rosso": "#dcbeff",143 "Force India": "#f032e6",144 "Sauber": "#800000",145 "Mercedes": "#00c0bf",146 "Ferrari": "#e6194b",147 "McLaren": "#f58231",148 "Haas F1 Team": "#ffffff",149 "Williams": "#4363d8",150 151 152 }153 154 return team_colors155 