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 for p in players.rushing().filter(rushing_att=lambda x: x > 10).sort("rushing_yds"):
80 print p, p.rushing_att, p.rushing_yds, p.rushing_tds
81
82 And the final output::
83
84 A.Peterson 18 133 2
85 C.Johnson 26 132 2
86 S.Jackson 26 131 1
87 M.Jones-Drew 24 123 1
88 J.Forsett 17 123 1
89 M.Bush 14 119 0
90 L.Betts 26 114 1
91 F.Gore 25 104 1
92 J.Charles 18 103 1
93 R.Williams 20 102 0
94 K.Moreno 18 97 0
95 L.Tomlinson 24 96 2
96 D.Williams 19 92 0
97 R.Rice 20 89 1
98 C.Wells 16 85 2
99 J.Stewart 11 82 2
100 R.Brown 12 82 1
101 R.Grant 19 79 0
102 K.Faulk 12 79 0
103 T.Jones 21 77 1
104 J.Snelling 18 61 1
105 K.Smith 12 55 0
106 C.Williams 14 52 1
107 M.Forte 20 41 0
108 P.Thomas 11 37 0
109 R.Mendenhall 13 36 0
110 W.McGahee 13 35 0
111 B.Scott 13 33 0
112 L.Maroney 13 31 1
113
114 You could do the same for the entire 2009 season::
115
116 players = nflgame.combine(nflgame.games(2009))
117 for p in players.rushing().sort("rushing_yds").limit(35):
118 print p, p.rushing_att, p.rushing_yds, p.rushing_tds
119
120 And the output::
121
122 C.Johnson 322 1872 12
123 S.Jackson 305 1361 4
124 A.Peterson 306 1335 17
125 T.Jones 305 1324 12
126 M.Jones-Drew 296 1309 15
127 R.Rice 240 1269 7
128 R.Grant 271 1202 10
129 C.Benson 272 1118 6
130 D.Williams 210 1104 7
131 R.Williams 229 1090 11
132 R.Mendenhall 222 1014 7
133 F.Gore 206 1013 8
134 J.Stewart 205 1008 9
135 K.Moreno 233 897 5
136 M.Turner 177 864 10
137 J.Charles 165 861 5
138 F.Jackson 205 850 2
139 M.Barber 200 841 7
140 B.Jacobs 218 834 5
141 M.Forte 242 828 4
142 J.Addai 213 788 9
143 C.Williams 190 776 4
144 C.Wells 170 774 7
145 A.Bradshaw 156 765 7
146 L.Maroney 189 735 9
147 J.Harrison 161 735 4
148 P.Thomas 141 733 5
149 L.Tomlinson 221 729 12
150 Kv.Smith 196 678 4
151 L.McCoy 154 633 4
152 M.Bell 155 626 5
153 C.Buckhalter 114 624 1
154 J.Jones 163 602 2
155 F.Jones 101 594 2
156 T.Hightower 137 574 8
157
158 Load data into Excel
159 --------------------
160 Every sequence of Players can be easily dumped into a file formatted
161 as comma-separated values (CSV). CSV files can then be opened directly
162 with programs like Excel, Google Docs, Open Office and Libre Office.
163
164 You could dump every statistic from a game like so::
165
166 game.players.csv('player-stats.csv')
167
168 Or if you want to get crazy, you could dump the statistics of every player
169 from an entire season::
170
171 nflgame.combine(nflgame.games(2010)).csv('season2010.csv')
172 """
173
174 from collections import OrderedDict
175
176 import nflgame.game as game
177 import nflgame.player as player
178 import nflgame.schedule as schedule
179
180 VERSION = "1.0.4"
181
182 NoPlayers = player.Players(None)
183 """
184 NoPlayers corresponds to the identity element of a Players sequences.
185
186 Namely, adding it to any other Players sequence has no effect.
187 """
188
189 -def games(year, week=None, home=None, away=None, preseason=False):
190 """
191 games returns a list of all games matching the given criteria. Each
192 game can then be queried for player statistics and information about
193 the game itself (score, winner, scoring plays, etc.).
194
195 Note that if a game's JSON data is not cached to disk, it is retrieved
196 from the NFL web site. A game's JSON data is *only* cached to disk once
197 the game is over, so be careful with the number of times you call this
198 while a game is going on. (i.e., don't piss off NFL.com.)
199 """
200 eids = __search_schedule(year, week, home, away, preseason)
201 if not eids:
202 return None
203 return [game.Game(eid) for eid in eids]
204
205 -def one(year, week, home, away, preseason=False):
206 """
207 one returns a single game matching the given criteria. The
208 game can then be queried for player statistics and information about
209 the game itself (score, winner, scoring plays, etc.).
210
211 one returns either a single game or no games. If there are multiple games
212 matching the given criteria, an assertion is raised.
213
214 Note that if a game's JSON data is not cached to disk, it is retrieved
215 from the NFL web site. A game's JSON data is *only* cached to disk once
216 the game is over, so be careful with the number of times you call this
217 while a game is going on. (i.e., don't piss off NFL.com.)
218 """
219 eids = __search_schedule(year, week, home, away, preseason)
220 if not eids:
221 return None
222 assert len(eids) == 1, 'More than one game matches the given criteria.'
223 return game.Game(eids[0])
224
226 """
227 Combines a list of games into one big player sequence.
228
229 This can be used, for example, to get Player objects corresponding to
230 statistics across an entire week, some number of weeks or an entire season.
231 """
232 players = OrderedDict()
233 for game in games:
234 for p in game.players:
235 if p.playerid not in players:
236 players[p.playerid] = p
237 else:
238 players[p.playerid] += p
239 return player.Players(players)
240
242 """
243 Searches the schedule to find the game identifiers matching the criteria
244 given.
245 """
246 ids = []
247 for (y, t, w, h, a), eid in schedule.gameids:
248 if y != year:
249 continue
250 if week is not None:
251 if isinstance(week, list) and w not in week:
252 continue
253 if not isinstance(week, list) and w != week:
254 continue
255 if home is not None and h != home:
256 continue
257 if away is not None and a != away:
258 continue
259 if preseason and t != "PRE":
260 continue
261 if not preseason and t != "REG":
262 continue
263 ids.append(eid)
264 return ids
265