diff --git a/app.py b/app.py index b04ec7f..477344d 100644 --- a/app.py +++ b/app.py @@ -1,4 +1,4 @@ -from flask import Flask, render_template +from flask import Flask, render_template, jsonify import requests from datetime import datetime, timedelta from waitress import serve @@ -231,16 +231,22 @@ def utc_to_est_time(utc_time): # Routes @app.route('/') def index(): - game_info = extract_game_info() - if game_info: - live_games_exist = any(game["Game State"] == "LIVE" for game in game_info) - pre_games_exist = any(game["Game State"] == "PRE" for game in game_info) - final_games_exist = any(game["Game State"] == "FINAL" for game in game_info) - return render_template('index.html', games=game_info, live_games_exist=live_games_exist, - pre_games_exist=pre_games_exist, final_games_exist=final_games_exist) + return render_template('index.html') + +@app.route('/scoreboard') +def get_scoreboard(): + global scoreboard_data + if scoreboard_data: + live_games = [game for game in extract_game_info() if game["Game State"] == "LIVE"] + pre_games = [game for game in extract_game_info() if game["Game State"] == "PRE"] + final_games = [game for game in extract_game_info() if game["Game State"] == "FINAL"] + return jsonify({ + "live_games": live_games, + "pre_games": pre_games, + "final_games": final_games + }) else: - print("Failed to retrieve scoreboard data") - return "Failed to retrieve scoreboard data" + return jsonify({"error": "Failed to retrieve scoreboard data"}) if __name__ == '__main__': store_scoreboard_data() diff --git a/static/script.js b/static/script.js new file mode 100644 index 0000000..329263f --- /dev/null +++ b/static/script.js @@ -0,0 +1,165 @@ +// Function to fetch scoreboard data using AJAX +function fetchScoreboardData() { + var xhr = new XMLHttpRequest(); + xhr.open("GET", "/scoreboard", true); + xhr.onreadystatechange = function () { + if (xhr.readyState === XMLHttpRequest.DONE) { + if (xhr.status === 200) { + updateScoreboard(JSON.parse(xhr.responseText)); + } else { + console.error("Failed to fetch scoreboard data."); + } + } + }; + xhr.send(); +} + +// Function to update scoreboard with fetched data +function updateScoreboard(data) { + var liveGamesSection = document.getElementById("live-games-section"); + var preGamesSection = document.getElementById('pre-games-section'); + var finalGamesSection = document.getElementById('final-games-section'); + + if (liveGamesSection) { + var liveGamesExist = data && data.live_games && data.live_games.length > 0; + if (liveGamesExist) { + document.getElementById('live-games').innerText = "Live Games" + liveGamesSection.innerHTML = generateGameBoxes(data.live_games, 'LIVE'); + } + } + + if (preGamesSection) { + var preGamesExist = data && data.pre_games && data.pre_games.length > 0; + if (preGamesExist) { + document.getElementById('on-later').innerText = "On Later" + preGamesSection.innerHTML = generateGameBoxes(data.pre_games, 'PRE'); + } + } + + if (finalGamesSection) { + var finalGamesExist = data && data.final_games && data.final_games.length > 0; + if (finalGamesExist) { + document.getElementById('game-over').innerText = "Game Over" + finalGamesSection.innerHTML = generateGameBoxes(data.final_games, 'FINAL'); + } + } +} + +// Function to generate HTML for game boxes +function generateGameBoxes(games, state) { + var html = ''; + games.forEach(function(game) { + if (game['Game State'] === state) { + html += '
'; + if (state === 'LIVE') { + if (game['Time Running']) { + html += '
'; // Display the red dot if the game is live + } + html += '
'; + html += ''; + html += '
'; + html += '' + game['Away Team'] + ''; + html += 'SOG: ' + game['Away Shots'] + ''; + html += '' + game['Away Power Play'] + ''; + html += '
'; + html += '' + game['Away Score'] + ''; + html += '
'; + html += '
'; + html += ''; + html += '
'; + html += '' + game['Home Team'] + ''; + html += 'SOG: ' + game['Home Shots'] + ''; + html += '' + game['Home Power Play'] + ''; + html += '
'; + html += '' + game['Home Score'] + ''; + html += '
'; + if (game['Intermission']) { + html += '
' + if (game['Period'] == 1 ) { + html += '1st Int'; + } + if (game['Period'] == 2 ) { + html += '2nd Int'; + } + if (game['Period'] == 3 ) { + html += '3rd Int'; + } + html += '
'; + html += '
' + game['Time Remaining'] + '
'; + } else { + html += '
'; + if (game['Period'] == 1 ) { + html += '1st'; + } + else if (game['Period'] == 2 ) { + html += '2nd'; + } + else if (game['Period'] == 3 ) { + html += '3rd'; + } + else { + html += 'OT'; + } + html += '
'; + html += '
' + game['Time Remaining'] + '
'; + } + html += '
'; + html += '
' + game['Start Time'] + '
'; + html += '
'; + html += ''; + html += '' + game['Away Team'] + ''; + html += '' + game['Away Record'] + ''; + html += '
'; + html += '
'; + html += ''; + html += '' + game['Home Team'] + ''; + html += '' + game['Home Record'] + ''; + html += '
'; + } else if (state === 'FINAL') { + html += '
'; + if (game['Last Period Type'] === 'REG') { + html += 'FINAL'; + } else if (game['Last Period Type'] === 'OT') { + html += 'FINAL/OT'; + } else { + html += 'FINAL/SO'; + } + html += '
'; + html += '
'; + html += ''; + html += '
'; + html += '' + game['Away Team'] + ''; + html += 'SOG: ' + game['Away Shots'] + ''; + html += '
'; + html += '' + game['Away Score'] + ''; + html += '
'; + html += '
'; + html += ''; + html += '
'; + html += '' + game['Home Team'] + ''; + html += 'SOG: ' + game['Home Shots'] + ''; + html += '
'; + html += '' + game['Home Score'] + ''; + html += '
'; + } + html += '
'; + } + }); + return html; +} + +// Function to reload the scoreboard every 20 seconds +function autoRefresh() { + fetchScoreboardData(); + setTimeout(autoRefresh, 5000); // 20 seconds +} + +// Call the autoRefresh function when the page loads +window.onload = function() { + autoRefresh(); +}; \ No newline at end of file diff --git a/static/styles.css b/static/styles.css index 7c9156b..bcf7574 100644 --- a/static/styles.css +++ b/static/styles.css @@ -240,4 +240,4 @@ h1 { .final-state { font-size: 12px; /* Decrease font size for better readability */ } -} +} \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index 0503833..7dc2d9a 100644 --- a/templates/index.html +++ b/templates/index.html @@ -4,136 +4,17 @@ NHL Scoreboard - - {% if live_games_exist %} -

Live Games

-
- {% for game in games if game['Game State'] == 'LIVE' %} -
- {% if game['Time Running'] %} -
- {% endif %} -
- -
- {{ game['Away Team'] }} - SOG: {{ game['Away Shots'] }} - {{ game['Away Power Play'] }} -
- {{ game['Away Score'] }} -
-
- -
- {{ game['Home Team'] }} - SOG: {{ game['Home Shots'] }} - {{ game['Home Power Play'] }} -
- {{ game['Home Score'] }} -
-
- {% if game['Intermission'] %} -
- {% if game['Period'] == 1 %} - 1st Int - {% elif game['Period'] == 2 %} - 2nd Int - {% elif game['Period'] == 3 %} - 3rd Int - {% endif %} -
-
{{ game['Time Remaining'] }}
- - {% else %} -
- {% if game['Period'] == 1 %} - 1st - {% elif game['Period'] == 2 %} - 2nd - {% elif game['Period'] == 3 %} - 3rd - {% else %} - OT - {% endif %} -
-
{{ game['Time Remaining'] }}
- {% endif %} - -
-
- Game Score: {{ game['Priority'] }} -
-
- {% endfor %} -
- {% endif %} +

+
- {% if pre_games_exist %} -

On Later

-
- {% for game in games if game['Game State'] == 'PRE' %} -
-
{{ game['Start Time'] }}
-
- - {{ game['Away Team'] }} - {{ game['Away Record'] }} -
-
- - {{ game['Home Team'] }} - {{ game['Home Record'] }} -
-
- {% endfor %} -
- {% endif %} +

+
- {% if final_games_exist %} -

Game Over

-
- {% for game in games if game['Game State'] == 'FINAL' %} -
-
- {% if game['Last Period Type'] == 'REG' %} - FINAL - {% elif game['Last Period Type'] == 'OT' %} - FINAL/OT - {% else %} - FINAL/SO - {% endif %} -
-
- -
- {{ game['Away Team'] }} - SOG: {{ game['Away Shots'] }} -
- {{ game['Away Score'] }} -
-
- -
- {{ game['Home Team'] }} - SOG: {{ game['Home Shots'] }} -
- {{ game['Home Score'] }} -
-
- {% endfor %} -
- {% endif %} +

+
+ +