refactor: rename functions across codebase for clarity
This commit is contained in:
@@ -12,7 +12,7 @@ logger = logging.getLogger(__name__)
|
||||
EASTERN = ZoneInfo("America/New_York")
|
||||
|
||||
|
||||
def get_scoreboard_data():
|
||||
def fetch_scores():
|
||||
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)
|
||||
@@ -31,8 +31,8 @@ def get_scoreboard_data():
|
||||
return None
|
||||
|
||||
|
||||
def store_scoreboard_data():
|
||||
scoreboard_data = get_scoreboard_data()
|
||||
def refresh_scores():
|
||||
scoreboard_data = fetch_scores()
|
||||
if scoreboard_data:
|
||||
with open(SCOREBOARD_DATA_FILE, "w") as json_file:
|
||||
json.dump(scoreboard_data, json_file)
|
||||
|
||||
24
app/games.py
24
app/games.py
@@ -7,7 +7,7 @@ from app.config import DB_PATH
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def process_record(record):
|
||||
def format_record(record):
|
||||
if record == "N/A":
|
||||
return "N/A"
|
||||
else:
|
||||
@@ -16,7 +16,7 @@ def process_record(record):
|
||||
return "-".join(formatted_parts)
|
||||
|
||||
|
||||
def extract_game_info(scoreboard_data):
|
||||
def parse_games(scoreboard_data):
|
||||
if not scoreboard_data:
|
||||
return []
|
||||
|
||||
@@ -36,8 +36,8 @@ def extract_game_info(scoreboard_data):
|
||||
"Home Logo": game["homeTeam"]["logo"],
|
||||
"Away Logo": game["awayTeam"]["logo"],
|
||||
"Game State": game_state,
|
||||
"Period": process_period(game),
|
||||
"Time Remaining": process_time_remaining(game),
|
||||
"Period": get_period(game),
|
||||
"Time Remaining": get_time_remaining(game),
|
||||
"Time Running": game["clock"]["running"]
|
||||
if game_state == "LIVE"
|
||||
else "N/A",
|
||||
@@ -45,11 +45,11 @@ def extract_game_info(scoreboard_data):
|
||||
if game_state == "LIVE"
|
||||
else "N/A",
|
||||
"Priority": calculate_game_priority(game),
|
||||
"Start Time": process_start_time(game),
|
||||
"Home Record": process_record(game["homeTeam"]["record"])
|
||||
"Start Time": get_start_time(game),
|
||||
"Home Record": format_record(game["homeTeam"]["record"])
|
||||
if game["gameState"] in ["PRE", "FUT"]
|
||||
else "N/A",
|
||||
"Away Record": process_record(game["awayTeam"]["record"])
|
||||
"Away Record": format_record(game["awayTeam"]["record"])
|
||||
if game["gameState"] in ["PRE", "FUT"]
|
||||
else "N/A",
|
||||
"Home Shots": game["homeTeam"]["sog"]
|
||||
@@ -77,7 +77,7 @@ def convert_game_state(game_state):
|
||||
return state_mapping.get(game_state, game_state)
|
||||
|
||||
|
||||
def process_period(game):
|
||||
def get_period(game):
|
||||
if game["gameState"] in ["PRE", "FUT"]:
|
||||
return 0
|
||||
elif game["gameState"] in ["FINAL", "OFF"]:
|
||||
@@ -86,7 +86,7 @@ def process_period(game):
|
||||
return game["periodDescriptor"]["number"]
|
||||
|
||||
|
||||
def process_time_remaining(game):
|
||||
def get_time_remaining(game):
|
||||
if game["gameState"] in ["PRE", "FUT"]:
|
||||
return "20:00"
|
||||
elif game["gameState"] in ["FINAL", "OFF"]:
|
||||
@@ -96,10 +96,10 @@ def process_time_remaining(game):
|
||||
return "END" if time_remaining == "00:00" else time_remaining
|
||||
|
||||
|
||||
def process_start_time(game):
|
||||
def get_start_time(game):
|
||||
if game["gameState"] in ["PRE", "FUT"]:
|
||||
utc_time = game["startTimeUTC"]
|
||||
est_time = utc_to_est_time(utc_time)
|
||||
est_time = utc_to_eastern(utc_time)
|
||||
return est_time.lstrip("0")
|
||||
else:
|
||||
return "N/A"
|
||||
@@ -224,7 +224,7 @@ def get_team_standings(team_name):
|
||||
}
|
||||
|
||||
|
||||
def utc_to_est_time(utc_time):
|
||||
def utc_to_eastern(utc_time):
|
||||
utc_datetime = datetime.strptime(utc_time, "%Y-%m-%dT%H:%M:%SZ")
|
||||
est_offset = timedelta(hours=-4)
|
||||
est_datetime = utc_datetime + est_offset
|
||||
|
||||
@@ -4,7 +4,7 @@ from flask import render_template, jsonify
|
||||
|
||||
from app import app
|
||||
from app.config import SCOREBOARD_DATA_FILE
|
||||
from app.games import extract_game_info
|
||||
from app.games import parse_games
|
||||
|
||||
|
||||
@app.route("/")
|
||||
@@ -27,17 +27,15 @@ def get_scoreboard():
|
||||
if scoreboard_data:
|
||||
live_games = [
|
||||
game
|
||||
for game in extract_game_info(scoreboard_data)
|
||||
for game in parse_games(scoreboard_data)
|
||||
if game["Game State"] == "LIVE"
|
||||
]
|
||||
pre_games = [
|
||||
game
|
||||
for game in extract_game_info(scoreboard_data)
|
||||
if game["Game State"] == "PRE"
|
||||
game for game in parse_games(scoreboard_data) if game["Game State"] == "PRE"
|
||||
]
|
||||
final_games = [
|
||||
game
|
||||
for game in extract_game_info(scoreboard_data)
|
||||
for game in parse_games(scoreboard_data)
|
||||
if game["Game State"] == "FINAL"
|
||||
]
|
||||
return jsonify(
|
||||
|
||||
@@ -3,15 +3,15 @@ import time
|
||||
|
||||
import schedule
|
||||
|
||||
from app.api import store_scoreboard_data
|
||||
from app.standings import update_nhl_standings
|
||||
from app.api import refresh_scores
|
||||
from app.standings import refresh_standings
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def schedule_tasks():
|
||||
schedule.every(600).seconds.do(update_nhl_standings)
|
||||
schedule.every(10).seconds.do(store_scoreboard_data)
|
||||
def start_scheduler():
|
||||
schedule.every(600).seconds.do(refresh_standings)
|
||||
schedule.every(10).seconds.do(refresh_scores)
|
||||
logger.info("Background scheduler started")
|
||||
while True:
|
||||
schedule.run_pending()
|
||||
|
||||
@@ -26,9 +26,9 @@ def truncate_standings_table(conn):
|
||||
conn.commit()
|
||||
|
||||
|
||||
def insert_standings_info(conn, standings_info):
|
||||
def insert_standings(conn, standings):
|
||||
cursor = conn.cursor()
|
||||
for team in standings_info:
|
||||
for team in standings:
|
||||
cursor.execute(
|
||||
"""
|
||||
INSERT INTO standings (team_common_name, league_sequence, league_l10_sequence)
|
||||
@@ -43,31 +43,32 @@ def insert_standings_info(conn, standings_info):
|
||||
conn.commit()
|
||||
|
||||
|
||||
def extract_standings_info():
|
||||
def fetch_standings():
|
||||
url = "https://api-web.nhle.com/v1/standings/now"
|
||||
try:
|
||||
response = requests.get(url, timeout=10)
|
||||
response.raise_for_status()
|
||||
standings_data = response.json()
|
||||
standings_info = []
|
||||
standings = []
|
||||
for team in standings_data.get("standings", []):
|
||||
team_info = {
|
||||
"team_common_name": team["teamCommonName"]["default"],
|
||||
"league_sequence": team["leagueSequence"],
|
||||
"league_l10_sequence": team["leagueL10Sequence"],
|
||||
}
|
||||
standings_info.append(team_info)
|
||||
return standings_info
|
||||
standings.append(
|
||||
{
|
||||
"team_common_name": team["teamCommonName"]["default"],
|
||||
"league_sequence": team["leagueSequence"],
|
||||
"league_l10_sequence": team["leagueL10Sequence"],
|
||||
}
|
||||
)
|
||||
return standings
|
||||
except requests.RequestException as e:
|
||||
logger.error("Failed to fetch standings: %s", e)
|
||||
return None
|
||||
|
||||
|
||||
def update_nhl_standings():
|
||||
def refresh_standings():
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
create_standings_table(conn)
|
||||
truncate_standings_table(conn)
|
||||
standings_info = extract_standings_info()
|
||||
if standings_info:
|
||||
insert_standings_info(conn, standings_info)
|
||||
standings = fetch_standings()
|
||||
if standings:
|
||||
insert_standings(conn, standings)
|
||||
conn.close()
|
||||
|
||||
Reference in New Issue
Block a user