Skip to content

Commit

Permalink
Use whole season instead of weekly to get prev/next game
Browse files Browse the repository at this point in the history
  • Loading branch information
ELepolt committed Jan 27, 2024
1 parent 21cfe1b commit c06c9b3
Showing 1 changed file with 16 additions and 26 deletions.
42 changes: 16 additions & 26 deletions src/nhl_api/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import json

from nhl_api_client import Client
from nhl_api_client.api.default import get_season_standings_by_date, get_team_week_schedule_by_date
from nhl_api_client.models import SeasonStandings, WeekSchedule, Game
from nhl_api_client.api.default import get_season_standings_by_date, get_team_season_schedule
from nhl_api_client.models import SeasonStandings, Game, SeasonSchedule

def team_info():
"""
Expand Down Expand Up @@ -127,35 +127,25 @@ def team_info():
# debug.error("Missing data in current team info")


# This one is a little funky for the time. I'll loop through the what and why
def team_previous_game(team_code, date, pg = None, ng = None):
# This response returns the next three games, starting from the date given
def team_previous_game(team_code, season_id):
# This response returns whole schedule for the current season
client = Client(base_url="https://api-web.nhle.com")
teams_response = {}
with client as client:
teams_response: WeekSchedule = get_team_week_schedule_by_date.sync(team_code, date, client=client)
teams_response: SeasonSchedule = get_team_season_schedule.sync(team_code, season_id, client=client)

# Probably breaks in the off season/preseason
pg = teams_response.games[0]

# If the first game in the list is a future game, the that's the next game. I think this will always be the case...
# TODO: Get a better situation for a LIVE game when showing a team summary during intermission
if pg.game_state == "FUT" or pg.game_state == "PRE" or pg.game_state == "LIVE":
ng = pg
# Then we take the previous_start_date given from the response and run through it again
pg, ng = team_previous_game(team_code, teams_response.previous_start_date, None, ng)
else:
# I _think_ that realistically the previous_game will always be the last game in this list, but
# I'm going to simply loop through for now.
for game in teams_response.games[1:]:
if (game.game_state == "FINAL" or game.game_state == "OFF") and game.game_date > pg.game_date:
pg = game
else:
if ng is None or ng.game_date < game.game_date:
ng = game

# Then return. I'd like to say we could be smart and take a few days off from the initial request,
# but I'm already questioning how that'd act with the likes of the all-star break.
# I think two requests is fine for now
ng = teams_response.games[0]
# # TODO: Get a better situation for a LIVE game when showing a team summary during intermission
for game in teams_response.games:
if (game.game_state == "FINAL" or game.game_state == "OFF") and game.game_date > pg.game_date:
pg = game
else:
if ng is None or ng.game_date < game.game_date:
ng = game
break

return pg, ng

def player_info(playerId):
Expand Down

0 comments on commit c06c9b3

Please sign in to comment.