Cookbook
Current edition
Find a competition's current season/year, then use it to scope other calls.
Discover this season's edition
import os, requests, datetime
KEY = os.environ["STATSHAWK_KEY"]
BASE = "https://api.statshawk.ai/v1"
editions = requests.get(
f"{BASE}/competitions/nba/editions",
headers={"X-API-Key": KEY},
timeout=10,
).json()["data"]["items"]
# Each edition carries an inclusive `span` (start..end). Pick the one that
# contains today; next season's edition can exist before this one ends, so
# "max season_year" would select a season with no games yet. During the
# offseason no span contains today — fall back to the most recently started
# edition rather than the newest year.
def parse(ts):
return datetime.datetime.fromisoformat(ts.replace("Z", "+00:00"))
now = datetime.datetime.now(datetime.timezone.utc)
started = [e for e in editions if parse(e["span"]["start"]) <= now]
current = next(
(e for e in started if now <= parse(e["span"]["end"])),
max(started, key=lambda e: parse(e["span"]["start"])),
)
year = current["season_year"]
today = datetime.date.today().isoformat()
contests = requests.get(
f"{BASE}/competitions/nba/editions/{year}/contests",
headers={"X-API-Key": KEY},
params={"date": today},
timeout=10,
).json()["data"]["items"]
print(f"Edition {year}: {len(contests)} contest(s) today")