Vscode / app /core /competitions.py
Erinaldorodrigues's picture
Handle unavailable seasons with validated fallback
ea721f9
Raw
History Blame Contribute Delete
2.12 kB
from __future__ import annotations
from dataclasses import dataclass
from datetime import date
MIN_COMPETITION_HISTORY = 40
@dataclass(frozen=True)
class CompetitionSpec:
sport_key: str
football_data_code: str
label: str
calendar_season: bool = False
COMPETITIONS: dict[str, CompetitionSpec] = {
"soccer_epl": CompetitionSpec("soccer_epl", "PL", "Premier League"),
"soccer_efl_champ": CompetitionSpec("soccer_efl_champ", "ELC", "EFL Championship"),
"soccer_germany_bundesliga": CompetitionSpec("soccer_germany_bundesliga", "BL1", "Bundesliga"),
"soccer_italy_serie_a": CompetitionSpec("soccer_italy_serie_a", "SA", "Serie A"),
"soccer_spain_la_liga": CompetitionSpec("soccer_spain_la_liga", "PD", "La Liga"),
"soccer_france_ligue_one": CompetitionSpec("soccer_france_ligue_one", "FL1", "Ligue 1"),
"soccer_brazil_campeonato": CompetitionSpec("soccer_brazil_campeonato", "BSA", "Brasileirão Série A", True),
"soccer_netherlands_eredivisie": CompetitionSpec("soccer_netherlands_eredivisie", "DED", "Eredivisie"),
"soccer_portugal_primeira_liga": CompetitionSpec("soccer_portugal_primeira_liga", "PPL", "Primeira Liga"),
"soccer_uefa_champs_league": CompetitionSpec("soccer_uefa_champs_league", "CL", "UEFA Champions League"),
}
def competition_for_sport_key(sport_key: str) -> CompetitionSpec | None:
return COMPETITIONS.get(sport_key)
def season_start_year(spec: CompetitionSpec, today: date) -> int:
if spec.calendar_season:
return today.year
# Top European competitions represented here start in the second half
# of the calendar year. June also covers early UEFA qualification.
return today.year if today.month >= 6 else today.year - 1
def requested_competitions(sport_keys: tuple[str, ...]) -> tuple[CompetitionSpec, ...]:
seen: set[str] = set()
out: list[CompetitionSpec] = []
for key in sport_keys:
spec = competition_for_sport_key(key)
if spec and spec.football_data_code not in seen:
seen.add(spec.football_data_code)
out.append(spec)
return tuple(out)