refactor: flatten app/scoreboard/ subpackage and rename files for clarity
This commit is contained in:
40
app/api.py
Normal file
40
app/api.py
Normal file
@@ -0,0 +1,40 @@
|
||||
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
|
||||
Reference in New Issue
Block a user