Skip to content

Commit

Permalink
wakeup_time for nflgame.live
Browse files Browse the repository at this point in the history
  • Loading branch information
derek-adair committed Aug 23, 2019
1 parent cb546f3 commit bed2f52
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 41 deletions.
72 changes: 32 additions & 40 deletions nflgame/live.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
before the current time. Once a game completes, the live module stops asking
NFL.com for data for that game.
If there are no games being actively played (i.e., it's been more than N hours
since the last game started), then the live module sleeps for longer periods
of time.
If there are no games being actively played (i.e., no games are within the
wakeup_time), then the live module sleeps for longer periods of time.
Thus, the live module can switch between two different modes: active and
inactive.
Expand All @@ -22,21 +21,13 @@
short intervals. A transition to an inactive mode occurs when no more games
are being played.
In the inactive mode, the live module only checks if a game is playing (or
about to play) every 15 minutes. If a game is playing or about to play, the
In the inactive mode, the live module only checks if a game is within the
wakeup_time (default 15 minutes). If a game is playing or about to play, the
live module switches to the active mode. Otherwise, it stays in the inactive
mode.
With this strategy, if the live module is working properly, you could
theoretically keep it running for the entire season.
(N.B. Half-time is ignored. Games are either being actively played or not.)
Alpha status
============
This module is emphatically in alpha status. I believe things will work OK for
the regular season, but the postseason brings new challenges. Moreover, it
will probably affect the API at least a little bit.
"""
import datetime
import time
Expand Down Expand Up @@ -96,6 +87,9 @@


def current_season_phase():
"""
Returns the current season phase
"""
_update_week_number()
return _cur_season_phase

Expand All @@ -110,7 +104,7 @@ def current_year_and_week():
return _cur_year, _cur_week


def current_games(year=None, week=None, kind='REG'):
def current_games(year=None, week=None, kind=_cur_season_phase):
"""
Returns a list of game.Games of games that are currently playing.
Expand Down Expand Up @@ -153,7 +147,7 @@ def current_games(year=None, week=None, kind='REG'):
return current


def run(callback, active_interval=15, inactive_interval=900, stop=None):
def run(callback, active_interval=15, inactive_interval=900, wakeup_time=900, stop=None):
"""
Starts checking for games that are currently playing.
Expand All @@ -167,11 +161,9 @@ def run(callback, active_interval=15, inactive_interval=900, stop=None):
not be in either the active or completed lists. No game can ever
be in both the `active` and `completed` lists at the same time.
It is possible that a game in the active list is not yet playing because
it hasn't started yet. It ends up in the active list because the "pregame"
has started on NFL.com's GameCenter web site, and sometimes game data is
partially filled. When this is the case, the 'playing' method on
a nflgame.game.Game will return False.
The active list will be populated with any game that starts within the
wakeup_time interval provided. If a game has not started, game.playing()
will be False.
When in the active mode (see live module description), active_interval
specifies the number of seconds to wait between checking for updated game
Expand All @@ -184,7 +176,8 @@ def run(callback, active_interval=15, inactive_interval=900, stop=None):
When in the inactive mode (see live module description), inactive_interval
specifies the number of seconds to wait between checking whether any games
have started or are about to start.
have started or are about to start. wakeup_time is used to add games to
the active list.
With the default parameters, run will never stop. However, you may set
stop to a Python datetime.datetime value. After time passes the stopping
Expand Down Expand Up @@ -218,7 +211,7 @@ def run(callback, active_interval=15, inactive_interval=900, stop=None):
if time.time() - last_week_check > _WEEK_INTERVAL:
last_week_check = _update_week_number()

games = _active_games(inactive_interval)
games = _active_games(wakeup_time)

if active:
active = _run_active(callback, games)
Expand All @@ -237,12 +230,12 @@ def _run_active(callback, games):
The active mode traverses each of the active games and fetches info for
each from NFL.com.
Then each game (that has info available on NFL.com---that is, the game
has started) is added to one of two lists: active and completed, which
Then each game is added to one of two lists: active and completed, which
are passed as the first and second parameters to callback. A game is
put in the active list if it's still being played, and into the completed
list if it has finished. In the latter case, it is added to a global store
of completed games and will never be passed to callback again.
put in the active list if it's still being played, or a bout to play
and into the completed list if it has finished. In the latter case,
it is added to a global store of completed games and will never be
passed to callback again.
"""
global _last

Expand Down Expand Up @@ -272,7 +265,7 @@ def _run_active(callback, games):
diffs = []
for game in active + completed:
for last_game in _last or []:
if game.eid != last_game.eid:
if game.eid != last_game.eid or not game.gcJsonAvailable:
continue
diffs.append(game - last_game)

Expand All @@ -283,26 +276,25 @@ def _run_active(callback, games):

def _run_inactive(games):
"""
The inactive mode simply checks if there are any active games. If there
are, inactive mode needs to stop and transition to active mode---thus
we return False. If there aren't any active games, then the inactive
mode should continue, where we return True.
The inactive mode simply checks if there are any active games (start w/in
wakeup_time). If there are, inactive mode needs to stop and transition to
active mode---thus we return False. If there aren't any active games, then
the inactive mode should continue, where we return True.
That is, so long as there are no active games, we go back to sleep.
i.e. There are no active games, we go back to sleep.
"""
return len(games) == 0


def _active_games(inactive_interval):
def _active_games(wakeup_time):
"""
Returns a list of all active games. In this case, an active game is a game
that will start within inactive_interval seconds, or has started within
_MAX_GAME_TIME seconds in the past.
that will start within wakeup_time seconds
"""
games = _games_in_week(_cur_year, _cur_week, _cur_season_phase)
active = []
for info in games:
if not _game_is_active(info, inactive_interval):
if not _game_is_active(info, wakeup_time):
continue
active.append(info)
return active
Expand All @@ -318,17 +310,17 @@ def _games_in_week(year, week, kind='REG'):
return nflgame._search_schedule(year, week, kind=kind)


def _game_is_active(gameinfo, inactive_interval):
def _game_is_active(gameinfo, wakeup_time):
"""
Returns true if the game is active. A game is considered active if the
game start time is in the past and not in the completed list (which is
a private module level variable that is populated automatically) or if the
game start time is within inactive_interval seconds from starting.
game start time is within wakeup_time seconds from starting.
"""
gametime = _game_datetime(gameinfo)
now = _now()
if gametime >= now:
return (gametime - now).total_seconds() <= inactive_interval
return (gametime - now).total_seconds() <= wakeup_time
return gameinfo['eid'] not in _completed


Expand Down
2 changes: 1 addition & 1 deletion nflgame/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '2.0.1a4'
__version__ = '2.0.1a5'

0 comments on commit bed2f52

Please sign in to comment.