-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoi.py
More file actions
52 lines (39 loc) · 1.79 KB
/
oi.py
File metadata and controls
52 lines (39 loc) · 1.79 KB
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
import os
import requests
HOST = "sofascore.p.rapidapi.com"
BASE_URL = f"https://{HOST}"
HEADERS = {
"x-rapidapi-host": HOST,
"x-rapidapi-key": os.getenv("RAPIDAPI_KEY", "ac07569428mshc51ad594e33debcp15f460jsn2e6cb9ab7fa6") # mets ta key ici OU dans variable d'env
}
def rapidapi_get(path: str, params: dict):
"""GET robuste: gère 204 + erreurs dans le JSON."""
url = f"{BASE_URL}/{path}"
r = requests.get(url, headers=HEADERS, params=params, timeout=20)
if r.status_code == 204:
return None # pas de contenu
data = r.json()
# RapidAPI/SofaScore renvoie parfois {error:{...}} même avec HTTP=200
if isinstance(data, dict) and "error" in data:
raise RuntimeError(f"{path} -> {data['error']} (url={r.url})")
return data
def get_latest_season_id(tournament_id: int) -> int:
data = rapidapi_get("tournaments/get-seasons", {"tournamentId": str(tournament_id)})
seasons = data.get("seasons", [])
if not seasons:
raise RuntimeError("Aucune saison trouvée pour ce tournoi.")
# La plus récente = souvent la première, mais on sécurise en triant par id décroissant
season_id = max(s["id"] for s in seasons if "id" in s)
return season_id
def get_next_matches(tournament_id: int, season_id: int, page_index: int = 0) -> dict:
return rapidapi_get(
"tournaments/get-next-matches",
{"tournamentId": str(tournament_id), "seasonId": str(season_id), "pageIndex": str(page_index)}
)
if __name__ == "__main__":
tournament_id = 17 # Premier League chez toi
season_id = get_latest_season_id(tournament_id)
print("seasonId =", season_id)
data = get_next_matches(tournament_id, season_id, page_index=0)
print("nb events =", len(data.get("events", [])))
print("hasNextPage =", data.get("hasNextPage"))