This commit is contained in:
Executable
+62
@@ -0,0 +1,62 @@
|
||||
"""
|
||||
Top leagues loader utility.
|
||||
|
||||
Loads league IDs from top_leagues.json with light validation and caching.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from functools import lru_cache
|
||||
from pathlib import Path
|
||||
from typing import Any, Set
|
||||
|
||||
|
||||
def _candidate_paths() -> list[Path]:
|
||||
here = Path(__file__).resolve()
|
||||
# .../ai-engine/utils/top_leagues.py
|
||||
repo_root = here.parents[2]
|
||||
ai_engine_root = here.parents[1]
|
||||
return [
|
||||
repo_root / "top_leagues.json",
|
||||
ai_engine_root / "top_leagues.json",
|
||||
]
|
||||
|
||||
|
||||
def _extract_ids(payload: Any) -> Set[str]:
|
||||
ids: Set[str] = set()
|
||||
if not isinstance(payload, list):
|
||||
return ids
|
||||
|
||||
for item in payload:
|
||||
if isinstance(item, str):
|
||||
val = item.strip()
|
||||
if val:
|
||||
ids.add(val)
|
||||
continue
|
||||
|
||||
if isinstance(item, dict):
|
||||
for key in ("id", "league_id", "leagueId"):
|
||||
raw = item.get(key)
|
||||
if raw is not None:
|
||||
val = str(raw).strip()
|
||||
if val:
|
||||
ids.add(val)
|
||||
break
|
||||
|
||||
return ids
|
||||
|
||||
|
||||
@lru_cache(maxsize=1)
|
||||
def load_top_league_ids() -> Set[str]:
|
||||
for path in _candidate_paths():
|
||||
if not path.exists():
|
||||
continue
|
||||
try:
|
||||
with path.open("r", encoding="utf-8") as f:
|
||||
payload = json.load(f)
|
||||
return _extract_ids(payload)
|
||||
except Exception:
|
||||
continue
|
||||
return set()
|
||||
|
||||
Reference in New Issue
Block a user