Files
NHL-Scoreboard/app/scoreboard/get_data.py
josh 3994943757
Some checks failed
CI / Lint (push) Successful in 58s
CI / Test (push) Successful in 8s
CI / Build & Push (push) Failing after 1m33s
good luck
2026-03-29 09:20:21 -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