Files
NHL-Scoreboard/app/routes.py
josh 6c098850f5
All checks were successful
CI / Lint (push) Successful in 7s
CI / Test (push) Successful in 6s
CI / Build & Push (push) Successful in 19s
fix: use truthy check for intermission filter, add route test
`is True` strict identity fails if the NHL API returns an integer 1
instead of a JSON boolean. A truthy check is safe here since the
Intermission field is always False/0 for non-intermission live games.

Also adds a test that verifies intermission games are separated from
live games in the /scoreboard response.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-29 19:57:00 -04:00

65 lines
1.8 KiB
Python

import json
from flask import render_template, jsonify, send_from_directory
from app import app
from app.config import SCOREBOARD_DATA_FILE
from app.games import parse_games
@app.route("/manifest.json")
def manifest():
return send_from_directory(app.static_folder, "manifest.json")
@app.route("/sw.js")
def service_worker():
response = send_from_directory(app.static_folder, "sw.js")
response.headers["Service-Worker-Allowed"] = "/"
response.headers["Cache-Control"] = "no-cache"
return response
@app.route("/favicon.ico")
def favicon():
return send_from_directory(
app.static_folder, "icon-32x32.png", mimetype="image/png"
)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/scoreboard")
def get_scoreboard():
try:
with open(SCOREBOARD_DATA_FILE, "r") as json_file:
scoreboard_data = json.load(json_file)
except FileNotFoundError:
return jsonify({"error": "Failed to retrieve scoreboard data. File not found."})
except json.JSONDecodeError:
return jsonify(
{"error": "Failed to retrieve scoreboard data. Invalid JSON format."}
)
if scoreboard_data:
games = parse_games(scoreboard_data)
return jsonify(
{
"live_games": [
g
for g in games
if g["Game State"] == "LIVE" and not g["Intermission"]
],
"intermission_games": [
g for g in games if g["Game State"] == "LIVE" and g["Intermission"]
],
"pre_games": [g for g in games if g["Game State"] == "PRE"],
"final_games": [g for g in games if g["Game State"] == "FINAL"],
}
)
else:
return jsonify({"error": "Failed to retrieve scoreboard data"})