2022-03-21 22:18:51 +00:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
from modules import util
|
|
|
|
from modules.util import Failed
|
|
|
|
|
|
|
|
logger = util.logger
|
|
|
|
|
|
|
|
base_url = "http://ergast.com/api/f1/"
|
|
|
|
|
2022-03-22 17:46:25 +00:00
|
|
|
translations = {
|
|
|
|
"nl": {
|
|
|
|
"70th Anniversary": "70th Anniversary", "Abu Dhabi": "Abu Dhabi", "Argentine": "Argentinië", "Australian": "Australië",
|
|
|
|
"Austrian": "Oostenrijk", "Azerbaijan": "Azerbeidzjan", "Bahrain": "Bahrein", "Belgian": "België", "Brazilian": "Brazilië",
|
|
|
|
"British": "Groot-Brittannië", "Caesars Palace": "Caesars Palace", "Canadian": "Canada", "Chinese": "China", "Dallas": "Dallas",
|
|
|
|
"Detroit": "Detroit", "Dutch": "Nederland", "Eifel": "Eifel", "Emilia Romagna": "Emilia Romagna", "European": "Europa",
|
|
|
|
"French": "Frankrijk", "German": "Duitsland", "Hungarian": "Hongarije", "Indian": "India", "Indianapolis 500": "Indianapolis 500",
|
|
|
|
"Italian": "Italië", "Japanese": "Japan", "Korean": "Zuid-Korea", "Luxembourg": "Luxemburg", "Malaysian": "Maleisië",
|
|
|
|
"Mexican": "Mexico", "Mexico City": "Mexico City", "Miami": "Miami", "Monaco": "Monaco", "Moroccan": "Marroko",
|
|
|
|
"Pacific": "Pacific", "Pescara": "Pescara", "Portuguese": "Portugal", "Qatar": "Qatar", "Russian": "Rusland",
|
|
|
|
"Sakhir": "Sakhir", "San Marino": "San Marino", "Saudi Arabian": "Saudi Arabië", "Singapore": "Singapore",
|
|
|
|
"South African": "Zuid-Afrika", "Spanish": "Spanje", "Styrian": "Stiermarken", "Swedish": "Zweden", "Swiss": "Zwitserland",
|
|
|
|
"São Paulo": "São Paulo", "Turkish": "Turkije", "Tuscan": "Toscane", "United States": "Verenigde Staten"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
terms = {
|
|
|
|
"free practice 1": ["free practice 1", "vrije training 1", "fp1", "vt1"],
|
|
|
|
"free practice 2": ["free practice 2", "vrije training 2", "fp2", "vt2"],
|
|
|
|
"free practice 3": ["free practice 3", "vrije training 3", "fp3", "vt3"],
|
|
|
|
"pre": ["pre", "voorbeschouwing"],
|
|
|
|
"post": ["post", "nabeschouwing"],
|
|
|
|
"quali": ["quali", "kwalificatie"],
|
2023-05-03 15:52:08 +00:00
|
|
|
"shootout": ["shootout"],
|
2023-03-07 22:19:00 +00:00
|
|
|
"notebook": ["notebook", "notitieboekje"],
|
2022-03-22 17:46:25 +00:00
|
|
|
"preview": ["preview", "seizoensvoorbeschouwing"],
|
|
|
|
"summary": ["summary", "samenvatting"],
|
|
|
|
"highlight": ["highlight", "hoogtepunten"],
|
|
|
|
}
|
|
|
|
|
|
|
|
names = {
|
|
|
|
"nl": {
|
|
|
|
"Formula 1 Cafe": "Formule 1 Cafe",
|
|
|
|
"Free Practice 1": "Vrije Training 1",
|
|
|
|
"Free Practice 2": "Vrije Training 2",
|
|
|
|
"Free Practice 3": "Vrije Training 3",
|
2023-05-03 15:52:08 +00:00
|
|
|
"Pre-Sprint Race Build-up": "Sprint Race Voorbeschouwing",
|
|
|
|
"Post-Sprint Race Analysis": "Sprint Race Nabeschouwing",
|
|
|
|
"Sprint Race Session": "Sprint Race",
|
2023-03-07 22:19:00 +00:00
|
|
|
"Ted's Sprint Notebook": "Ted's Sprint Notitieboekje",
|
2022-03-22 17:46:25 +00:00
|
|
|
"Pre-Qualifying Build-up": "Kwalificatie Voorbeschouwing",
|
|
|
|
"Post-Qualifying Analysis": "Kwalificatie Nabeschouwing",
|
|
|
|
"Qualifying Session": "Kwalificatie",
|
|
|
|
"Season Preview": "Seizoensvoorbeschouwing",
|
|
|
|
"Pre-Race Buildup": "Voorbeschouwing",
|
|
|
|
"Post-Race Analysis": "Nabeschouwing",
|
|
|
|
"Live from the Grid": "Vanaf de grid",
|
|
|
|
"Highlights": "Samenvatting",
|
|
|
|
"Race Session": "Race",
|
2023-03-07 22:19:00 +00:00
|
|
|
"Ted's Race Notebook": "Ted's Race Notitieboekje",
|
2023-05-03 15:52:08 +00:00
|
|
|
"Ted's Qualifying Notebook": "Ted's Kwalificatie Notitieboekje",
|
|
|
|
"Pre-Sprint Shootout Build-up": "Sprint Shootout Voorbeschouwing",
|
|
|
|
"Post-Sprint Shootout Analysis": "Sprint Shootout Nabeschouwing",
|
|
|
|
"Sprint Shootout Session": "Sprint Shootout",
|
2022-03-22 17:46:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-21 22:18:51 +00:00
|
|
|
class Race:
|
2022-03-22 17:46:25 +00:00
|
|
|
def __init__(self, data, language):
|
2022-03-21 22:18:51 +00:00
|
|
|
self._data = data
|
2022-03-22 17:46:25 +00:00
|
|
|
self._language = language
|
2022-03-21 22:18:51 +00:00
|
|
|
self.season = util.check_num(self._data["season"], is_int=True)
|
|
|
|
self.round = util.check_num(self._data["round"], is_int=True)
|
|
|
|
self.name = self._data["raceName"]
|
|
|
|
try:
|
|
|
|
self.date = datetime.strptime(self._data["date"], "%Y-%m-%d")
|
|
|
|
except (ValueError, TypeError):
|
|
|
|
self.date = None
|
|
|
|
|
|
|
|
def format_name(self, round_prefix, shorten_gp):
|
2022-03-22 17:46:25 +00:00
|
|
|
if self._language:
|
|
|
|
output = f"GP {self.name.replace(' Grand Prix', '')}" if shorten_gp else self.name
|
2022-03-23 07:45:26 +00:00
|
|
|
for eng_value, trans_value in translations[self._language].items():
|
2022-03-22 17:46:25 +00:00
|
|
|
output = output.replace(eng_value, trans_value)
|
|
|
|
else:
|
|
|
|
output = self.name.replace("Grand Prix", "GP") if shorten_gp else self.name
|
|
|
|
if round_prefix:
|
|
|
|
output = f"{self.round:02} - {output}"
|
|
|
|
return output
|
2022-03-21 22:18:51 +00:00
|
|
|
|
|
|
|
def session_info(self, title, sprint_weekend):
|
|
|
|
title = title.lower()
|
2022-03-22 17:46:25 +00:00
|
|
|
if "cafe" in title:
|
|
|
|
output = "Formula 1 Cafe"
|
|
|
|
elif any([x in title for x in terms["free practice 1"]]):
|
2022-03-21 22:18:51 +00:00
|
|
|
output = "Free Practice 1"
|
2022-03-22 17:46:25 +00:00
|
|
|
elif any([x in title for x in terms["free practice 2"]]):
|
2022-03-21 22:18:51 +00:00
|
|
|
output = "Free Practice 2"
|
2022-03-22 17:46:25 +00:00
|
|
|
elif any([x in title for x in terms["free practice 3"]]):
|
2022-03-21 22:18:51 +00:00
|
|
|
output = "Free Practice 3"
|
2023-05-03 15:52:08 +00:00
|
|
|
elif "shootout" in title:
|
|
|
|
if any([x in title for x in terms["pre"]]):
|
|
|
|
output = "Pre-Sprint Shootout Build-up"
|
|
|
|
elif any([x in title for x in terms["post"]]):
|
|
|
|
output = "Post-Sprint Shootout Analysis"
|
|
|
|
else:
|
|
|
|
output = "Sprint Shootout Session"
|
2022-03-21 22:18:51 +00:00
|
|
|
elif "sprint" in title:
|
2022-03-22 17:46:25 +00:00
|
|
|
if any([x in title for x in terms["pre"]]):
|
2023-05-03 15:52:08 +00:00
|
|
|
output = "Pre-Sprint Race Build-up"
|
2022-03-22 17:46:25 +00:00
|
|
|
elif any([x in title for x in terms["post"]]):
|
2023-05-03 15:52:08 +00:00
|
|
|
output = "Post-Sprint Race Analysis"
|
2023-03-07 22:19:00 +00:00
|
|
|
elif any([x in title for x in terms["notebook"]]):
|
|
|
|
output = "Ted's Sprint Notebook"
|
2022-03-22 17:46:25 +00:00
|
|
|
else:
|
2023-05-03 15:52:08 +00:00
|
|
|
output = "Sprint Race Session"
|
2022-03-22 17:46:25 +00:00
|
|
|
elif any([x in title for x in terms["quali"]]):
|
|
|
|
if any([x in title for x in terms["pre"]]):
|
|
|
|
output = "Pre-Qualifying Build-up"
|
2023-05-03 15:52:08 +00:00
|
|
|
elif any([x in title for x in terms["post"]]):
|
2022-03-22 17:46:25 +00:00
|
|
|
output = "Post-Qualifying Analysis"
|
2023-05-03 15:52:08 +00:00
|
|
|
elif any ([x in title for x in terms["notebook"]]):
|
|
|
|
output = "Ted's Qualifying Notebook"
|
2022-03-22 17:46:25 +00:00
|
|
|
else:
|
|
|
|
output = "Qualifying Session"
|
|
|
|
elif any([x in title for x in terms["preview"]]):
|
|
|
|
output = "Season Preview"
|
|
|
|
elif any([x in title for x in terms["pre"]]):
|
|
|
|
output = "Pre-Race Buildup"
|
|
|
|
elif any([x in title for x in terms["post"]]):
|
|
|
|
output = "Post-Race Analysis"
|
|
|
|
elif "grid" in title:
|
|
|
|
output = "Live from the Grid"
|
|
|
|
elif any([x in title for x in terms["summary"] + terms["highlight"]]):
|
2022-03-21 22:18:51 +00:00
|
|
|
output = "Highlights"
|
2023-03-07 22:19:00 +00:00
|
|
|
elif any([x in title for x in terms["notebook"]]):
|
|
|
|
output = "Ted's Race Notebook"
|
2022-03-21 22:18:51 +00:00
|
|
|
else:
|
|
|
|
output = "Race Session"
|
|
|
|
if "2160" in title or "4K" in title:
|
|
|
|
output = f"{output} (4K)"
|
|
|
|
|
2022-03-22 17:46:25 +00:00
|
|
|
if (sprint_weekend and any([x in output for x in ["Sprint", "Free Practice 2"]])) or \
|
|
|
|
(not sprint_weekend and any([x in output for x in ["Qualifying", "Free Practice 3"]])):
|
|
|
|
video_date = self.date - timedelta(days=1)
|
|
|
|
elif (sprint_weekend and any([x in output for x in ["Qualifying", "Free Practice 1", "Formula 1 Cafe"]])) or \
|
|
|
|
(not sprint_weekend and any([x in output for x in ["Free Practice 1", "Free Practice 2", "Formula 1 Cafe"]])):
|
|
|
|
video_date = self.date - timedelta(days=2)
|
2022-03-21 22:18:51 +00:00
|
|
|
else:
|
2022-03-22 17:46:25 +00:00
|
|
|
video_date = self.date
|
|
|
|
|
|
|
|
if self._language and self._language in names and output in names[self._language]:
|
|
|
|
output = names[self._language][output]
|
|
|
|
return output, video_date
|
|
|
|
|
2022-03-21 22:18:51 +00:00
|
|
|
|
|
|
|
class Ergast:
|
|
|
|
def __init__(self, config):
|
|
|
|
self.config = config
|
|
|
|
|
2022-03-22 17:46:25 +00:00
|
|
|
def get_races(self, year, language, ignore_cache=False):
|
2022-03-21 22:18:51 +00:00
|
|
|
expired = None
|
|
|
|
if self.config.Cache and not ignore_cache:
|
|
|
|
race_list, expired = self.config.Cache.query_ergast(year, self.config.Cache.expiration)
|
|
|
|
if race_list and expired is False:
|
2022-03-22 17:46:25 +00:00
|
|
|
return [Race(r, language) for r in race_list]
|
2022-03-21 22:18:51 +00:00
|
|
|
response = self.config.get(f"{base_url}{year}.json")
|
|
|
|
if response.status_code < 400:
|
2022-03-22 17:46:25 +00:00
|
|
|
races = [Race(r, language) for r in response.json()["MRData"]["RaceTable"]["Races"]]
|
2022-03-21 22:18:51 +00:00
|
|
|
if self.config.Cache and not ignore_cache:
|
|
|
|
self.config.Cache.update_ergast(expired, year, races, self.config.Cache.expiration)
|
|
|
|
return races
|
|
|
|
else:
|
|
|
|
raise Failed(f"Ergast Error: F1 Season: {year} Not found")
|