Package nflgame :: Module live
[frames] | no frames]

Module live

source code

The live module provides a mechanism of periodically checking which games are being actively played.

It requires the third party library pytz to be installed, which makes sure game times are compared properly with respect to time zones. pytz can be downloaded from PyPI: http://pypi.python.org/pypi/pytz/

It works by periodically downloading data from NFL.com for games that started 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.

Thus, the live module can switch between two different modes: active and inactive.

In the active mode, the live module downloads data from NFL.com in 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 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.

Functions
 
current_year_and_week()
Returns a tuple (year, week) where year is the current year of the season and week is the current week number of games being played.
source code
 
current_games(year=None, week=None, regular=True, postseason=False, preseason=False)
Returns a list of game.Games of games that are currently playing.
source code
 
run(callback, active_interval=15, inactive_interval=900, stop=None)
Starts checking for games that are currently playing.
source code
Variables
  __package__ = 'nflgame'
Function Details

current_year_and_week()

source code 

Returns a tuple (year, week) where year is the current year of the season and week is the current week number of games being played. i.e., (2012, 3).

N.B. This always downloads the schedule XML data.

current_games(year=None, week=None, regular=True, postseason=False, preseason=False)

source code 

Returns a list of game.Games of games that are currently playing. This fetches all current information from NFL.com.

If either year or week is none, then the current year and week are fetched from the schedule on NFL.com. If they are *both* provided, then the schedule from NFL.com won't need to be downloaded, and thus saving time.

So for example:

   year, week = nflgame.live.current_year_and_week()
   while True:
       games = nflgame.live.current_games(year, week)
       # Do something with games
       time.sleep(60)

Finally, if the optional parameter postseason/preseason is True, then the week parameter will refer to the postseason/preseason week rather than the regular season week.

run(callback, active_interval=15, inactive_interval=900, stop=None)

source code 

Starts checking for games that are currently playing.

Every time there is an update, callback will be called with two lists: active and completed. The active list is a list of game.Game that are currently being played. The completed list is a list of game.Game that have just finished. A game will appear in the completed list only once, after which that game will not be in either the active or completed lists. No game can ever be in both 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.

When in the active mode (see live module description), active_interval specifies the number of seconds to wait between checking for updated game data. Please do not make this number too low to avoid angering NFL.com. If you anger them too much, it is possible that they could ban your IP address.

Note that NFL.com's GameCenter page is updated every 15 seconds, so setting the active_interval much smaller than that is wasteful.

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.

With the default parameters, run will never stop. However, you may set stop to a Python datetime.datetime value. After time passes the stopping point, run will quit. (Technically, it's possible that it won't quit until at most inactive_interval seconds after the stopping point is reached.) The stop value is compared against datetime.datetime.now().