Files
NHL-Scoreboard/app/api.py
josh a4dc7dff52
All checks were successful
CI / Lint (push) Successful in 5s
CI / Test (push) Successful in 6s
CI / Build & Push (push) Successful in 16s
refactor: flatten app/scoreboard/ subpackage and rename files for clarity
2026-03-29 10:16:35 -04:00

41 lines
1.2 KiB
Python

import json
import logging
from datetime import datetime
from zoneinfo import ZoneInfo
import requests
from app.config import SCOREBOARD_DATA_FILE
logger = logging.getLogger(__name__)
EASTERN = ZoneInfo("America/New_York")
def get_scoreboard_data():
now = datetime.now(EASTERN)
start_time_evening = now.replace(hour=19, minute=0, second=0, microsecond=0)
end_time_morning = now.replace(hour=3, minute=0, second=0, microsecond=0)
if now >= start_time_evening or now < end_time_morning:
nhle_api_url = "https://api-web.nhle.com/v1/score/now"
else:
nhle_api_url = f"https://api-web.nhle.com/v1/score/{now.strftime('%Y-%m-%d')}"
try:
response = requests.get(nhle_api_url, timeout=10)
response.raise_for_status()
return response.json()
except requests.RequestException as e:
logger.error("Failed to fetch scoreboard data from %s: %s", nhle_api_url, e)
return None
def store_scoreboard_data():
scoreboard_data = get_scoreboard_data()
if scoreboard_data:
with open(SCOREBOARD_DATA_FILE, "w") as json_file:
json.dump(scoreboard_data, json_file)
return scoreboard_data
return None