Package nflgame
[frames] | no frames]

Source Code for Package nflgame

  1  """ 
  2  Introduction 
  3  ============ 
  4  An API to retrieve and read NFL Game Center JSON data. 
  5  It can work with real-time data, which can be used for fantasy football. 
  6   
  7  nflgame works by parsing the same JSON data that powers NFL.com's live 
  8  GameCenter. Therefore, nflgame can be used to report game statistics while 
  9  a game is being played. 
 10   
 11  The package comes pre-loaded with game data from every pre- and regular 
 12  season game from 2009 up until August 28, 2012. Therefore, querying such data 
 13  does not actually ping NFL.com. 
 14   
 15  However, if you try to search for data in a game that is being currently 
 16  played, the JSON data will be downloaded from NFL.com at each request (so be 
 17  careful not to inspect for data too many times while a game is being played). 
 18  If you ask for data for a particular game that hasn't been cached to disk 
 19  but is no longer being played, it will be automatically cached to disk 
 20  so that no further downloads are required. 
 21   
 22  Examples 
 23  ======== 
 24   
 25  Finding games 
 26  ------------- 
 27  Games can be selected in bulk, e.g., every game in week 1 of 2010:: 
 28   
 29      games = nflgame.games(2010, week=1) 
 30   
 31  Or pin-pointed exactly, e.g., the Patriots week 17 whomping against the Bills:: 
 32   
 33      game = nflgame.game(2011, 17, "NE", "BUF") 
 34   
 35  This season's (2012) pre-season games can also be accessed:: 
 36   
 37      pregames = nflgame.games(2012, preseason=True) 
 38   
 39  Find passing leaders of a game 
 40  ------------------------------ 
 41  Given some game, the player statistics can be easily searched. For example, 
 42  to find the passing leaders of a particular game:: 
 43   
 44      for p in game.players.passing().sort("passing_yds"): 
 45          print p, p.passing_att, p.passing_cmp, p.passing_yds, p.passing_tds 
 46   
 47  Output:: 
 48   
 49      T.Brady 35 23 338 3 
 50      R.Fitzpatrick 46 29 307 2 
 51      B.Hoyer 1 1 22 0 
 52   
 53  See every player that made an interception 
 54  ------------------------------------------ 
 55  We can filter all players on whether they had more than zero defensive 
 56  interceptions, and then sort those players by the number of picks:: 
 57   
 58      for p in game.players.filter(defense_int=lambda x:x>0).sort("defense_int"): 
 59          print p, p.defense_int 
 60   
 61  Output:: 
 62   
 63      S.Moore 2 
 64      A.Molden 1 
 65      D.McCourty 1 
 66      N.Barnett 1 
 67   
 68  Finding weekly rushing leaders 
 69  ------------------------------ 
 70  Sequences of players can be added together, and their sum can then be used 
 71  like any other sequence of players. For example, to get every player 
 72  that played in week 10 of 2009:: 
 73   
 74      week10 = nflgame.games(2009, 10) 
 75      players = nflgame.combine(week10) 
 76   
 77  And then to list all rushers with at least 10 carries sorted by rushing yards:: 
 78   
 79      rushers = players.rushing() 
 80      for p in rushers.filter(rushing_att=lambda x: x > 10).sort("rushing_yds"): 
 81          print p, p.rushing_att, p.rushing_yds, p.rushing_tds 
 82   
 83  And the final output:: 
 84   
 85      A.Peterson 18 133 2 
 86      C.Johnson 26 132 2 
 87      S.Jackson 26 131 1 
 88      M.Jones-Drew 24 123 1 
 89      J.Forsett 17 123 1 
 90      M.Bush 14 119 0 
 91      L.Betts 26 114 1 
 92      F.Gore 25 104 1 
 93      J.Charles 18 103 1 
 94      R.Williams 20 102 0 
 95      K.Moreno 18 97 0 
 96      L.Tomlinson 24 96 2 
 97      D.Williams 19 92 0 
 98      R.Rice 20 89 1 
 99      C.Wells 16 85 2 
100      J.Stewart 11 82 2 
101      R.Brown 12 82 1 
102      R.Grant 19 79 0 
103      K.Faulk 12 79 0 
104      T.Jones 21 77 1 
105      J.Snelling 18 61 1 
106      K.Smith 12 55 0 
107      C.Williams 14 52 1 
108      M.Forte 20 41 0 
109      P.Thomas 11 37 0 
110      R.Mendenhall 13 36 0 
111      W.McGahee 13 35 0 
112      B.Scott 13 33 0 
113      L.Maroney 13 31 1 
114   
115  You could do the same for the entire 2009 season:: 
116   
117      players = nflgame.combine(nflgame.games(2009)) 
118      for p in players.rushing().sort("rushing_yds").limit(35): 
119          print p, p.rushing_att, p.rushing_yds, p.rushing_tds 
120   
121  And the output:: 
122   
123      C.Johnson 322 1872 12 
124      S.Jackson 305 1361 4 
125      A.Peterson 306 1335 17 
126      T.Jones 305 1324 12 
127      M.Jones-Drew 296 1309 15 
128      R.Rice 240 1269 7 
129      R.Grant 271 1202 10 
130      C.Benson 272 1118 6 
131      D.Williams 210 1104 7 
132      R.Williams 229 1090 11 
133      R.Mendenhall 222 1014 7 
134      F.Gore 206 1013 8 
135      J.Stewart 205 1008 9 
136      K.Moreno 233 897 5 
137      M.Turner 177 864 10 
138      J.Charles 165 861 5 
139      F.Jackson 205 850 2 
140      M.Barber 200 841 7 
141      B.Jacobs 218 834 5 
142      M.Forte 242 828 4 
143      J.Addai 213 788 9 
144      C.Williams 190 776 4 
145      C.Wells 170 774 7 
146      A.Bradshaw 156 765 7 
147      L.Maroney 189 735 9 
148      J.Harrison 161 735 4 
149      P.Thomas 141 733 5 
150      L.Tomlinson 221 729 12 
151      Kv.Smith 196 678 4 
152      L.McCoy 154 633 4 
153      M.Bell 155 626 5 
154      C.Buckhalter 114 624 1 
155      J.Jones 163 602 2 
156      F.Jones 101 594 2 
157      T.Hightower 137 574 8 
158   
159  Load data into Excel 
160  -------------------- 
161  Every sequence of Players can be easily dumped into a file formatted 
162  as comma-separated values (CSV). CSV files can then be opened directly 
163  with programs like Excel, Google Docs, Open Office and Libre Office. 
164   
165  You could dump every statistic from a game like so:: 
166   
167      game.players.csv('player-stats.csv') 
168   
169  Or if you want to get crazy, you could dump the statistics of every player 
170  from an entire season:: 
171   
172      nflgame.combine(nflgame.games(2010)).csv('season2010.csv') 
173  """ 
174   
175  from collections import OrderedDict 
176   
177  import nflgame.game as game 
178  import nflgame.player as player 
179  import nflgame.schedule as schedule 
180   
181  VERSION = "1.0.4" 
182   
183  NoPlayers = player.Players(None) 
184  """ 
185  NoPlayers corresponds to the identity element of a Players sequences. 
186   
187  Namely, adding it to any other Players sequence has no effect. 
188  """ 
189   
190   
191 -def games(year, week=None, home=None, away=None, preseason=False):
192 """ 193 games returns a list of all games matching the given criteria. Each 194 game can then be queried for player statistics and information about 195 the game itself (score, winner, scoring plays, etc.). 196 197 Note that if a game's JSON data is not cached to disk, it is retrieved 198 from the NFL web site. A game's JSON data is *only* cached to disk once 199 the game is over, so be careful with the number of times you call this 200 while a game is going on. (i.e., don't piss off NFL.com.) 201 """ 202 eids = __search_schedule(year, week, home, away, preseason) 203 if not eids: 204 return None 205 return [game.Game(eid) for eid in eids]
206 207
208 -def one(year, week, home, away, preseason=False):
209 """ 210 one returns a single game matching the given criteria. The 211 game can then be queried for player statistics and information about 212 the game itself (score, winner, scoring plays, etc.). 213 214 one returns either a single game or no games. If there are multiple games 215 matching the given criteria, an assertion is raised. 216 217 Note that if a game's JSON data is not cached to disk, it is retrieved 218 from the NFL web site. A game's JSON data is *only* cached to disk once 219 the game is over, so be careful with the number of times you call this 220 while a game is going on. (i.e., don't piss off NFL.com.) 221 """ 222 eids = __search_schedule(year, week, home, away, preseason) 223 if not eids: 224 return None 225 assert len(eids) == 1, 'More than one game matches the given criteria.' 226 return game.Game(eids[0])
227 228
229 -def combine(games):
230 """ 231 Combines a list of games into one big player sequence. 232 233 This can be used, for example, to get Player objects corresponding to 234 statistics across an entire week, some number of weeks or an entire season. 235 """ 236 players = OrderedDict() 237 for game in games: 238 for p in game.players: 239 if p.playerid not in players: 240 players[p.playerid] = p 241 else: 242 players[p.playerid] += p 243 return player.Players(players)
244 245
246 -def __search_schedule(year, week=None, home=None, away=None, preseason=False):
247 """ 248 Searches the schedule to find the game identifiers matching the criteria 249 given. 250 """ 251 ids = [] 252 for (y, t, w, h, a), info in schedule.games: 253 if y != year: 254 continue 255 if week is not None: 256 if isinstance(week, list) and w not in week: 257 continue 258 if not isinstance(week, list) and w != week: 259 continue 260 if home is not None and h != home: 261 continue 262 if away is not None and a != away: 263 continue 264 if preseason and t != "PRE": 265 continue 266 if not preseason and t != "REG": 267 continue 268 ids.append(info['eid']) 269 return ids
270