BurntSushi / nflgame

An API to retrieve and read NFL Game Center JSON data. It can work with real-time data, which can be used for fantasy football.
http://pdoc.burntsushi.net/nflgame
The Unlicense
1.27k stars 413 forks source link

Getting individual kicking_fgm information #116

Closed mesee298 closed 9 years ago

mesee298 commented 9 years ago

First, I want to say how amazing this is. Our fantasy football league (www.wffl.net) has been scraping nfl.com for many years. We knew about the jsons, but never got to a point where this little gem of a program got to. Okay, enough brown nosing. :)

How would you find out every Field Goal Made (or missed) for every player/game per week? The kicking_fgm seems to only show a total, but we calculate on each fgm.

You can look at http://www.wffl.net/rules.asp#scoring to get more information on how we score.

ochawkeye commented 9 years ago

There are a few ways to go about this using nflgame.

One option: If you are only interested in seeing the made field goals you can use the built-in filter.

import nflgame

yr, wks, season_type = 2014, [3, 5], 'POST'
games = nflgame.games(yr, week=wks, kind=season_type)

plays = nflgame.combine_plays(games)
all_made_fgs = plays.filter(kicking_fgm=True)
for p in all_made_fgs:
    print p
(GB, SEA 1, Q1, 4 and 1) (8:10) M.Crosby 18 yard field goal is GOOD, Center-B.Goode, Holder-T.Masthay.
(GB, SEA 1, Q1, 4 and 1) (5:10) M.Crosby 19 yard field goal is GOOD, Center-B.Goode, Holder-T.Masthay.
(GB, SEA 22, Q2, 4 and 1) (9:42) M.Crosby 40 yard field goal is GOOD, Center-B.Goode, Holder-T.Masthay.
(GB, SEA 30, Q4, 4 and 7) (10:58) M.Crosby 48 yard field goal is GOOD, Center-B.Goode, Holder-T.Masthay.
(GB, SEA 30, Q4, 4 and 4) (:19) M.Crosby 48 yard field goal is GOOD, Center-B.Goode, Holder-T.Masthay.
(NE, IND 3, Q2, 4 and 3) (:13) S.Gostkowski 21 yard field goal is GOOD, Center-D.Aiken, Holder-R.Allen. NE 3-Gostkowski now has 109 career postseason points, 8th all-time.
(SEA, NE 8, Q3, 4 and 1) (11:13) S.Hauschka 27 yard field goal is GOOD, Center-C.Gresham, Holder-J.Ryan.

You can gain deeper access to that play data by accessing p.xxx info:

plays = nflgame.combine_plays(games)
all_made_fgs = plays.filter(kicking_fgm=True)
for p in all_made_fgs:
    print p.players, p.kicking_fgm_yds
[M.Crosby] 18
[M.Crosby] 19
[M.Crosby] 40
[M.Crosby] 48
[M.Crosby] 48
[S.Gostkowski] 21
[S.Hauschka] 27

Please note that stringing together back to back iterations over that filtered data doesn't work.

plays = nflgame.combine_plays(games)
all_made_fgs = plays.filter(kicking_fgm=True)
for p in all_made_fgs:
    print p
for p in all_made_fgs:
    print p.players, p.kicking_fgm_yds
(GB, SEA 1, Q1, 4 and 1) (8:10) M.Crosby 18 yard field goal is GOOD, Center-B.Goode, Holder-T.Masthay.
(GB, SEA 1, Q1, 4 and 1) (5:10) M.Crosby 19 yard field goal is GOOD, Center-B.Goode, Holder-T.Masthay.
(GB, SEA 22, Q2, 4 and 1) (9:42) M.Crosby 40 yard field goal is GOOD, Center-B.Goode, Holder-T.Masthay.
(GB, SEA 30, Q4, 4 and 7) (10:58) M.Crosby 48 yard field goal is GOOD, Center-B.Goode, Holder-T.Masthay.
(GB, SEA 30, Q4, 4 and 4) (:19) M.Crosby 48 yard field goal is GOOD, Center-B.Goode, Holder-T.Masthay.
(NE, IND 3, Q2, 4 and 3) (:13) S.Gostkowski 21 yard field goal is GOOD, Center-D.Aiken, Holder-R.Allen. NE 3-Gostkowski now has 109 career postseason points, 8th all-time.
(SEA, NE 8, Q3, 4 and 1) (11:13) S.Hauschka 27 yard field goal is GOOD, Center-C.Gresham, Holder-J.Ryan.

Since that is the case, you would need to "rebuild" the plays before filtering again on missed field goals.

import nflgame

yr, wks, season_type = 2014, [3, 5], 'POST'
games = nflgame.games(yr, week=wks, kind=season_type)

plays = nflgame.combine_plays(games)
all_made_fgs = plays.filter(kicking_fgm=True)
for p in all_made_fgs:
    print p

print '-'*50

plays = nflgame.combine_plays(games)
all_missed_fgs = plays.filter(kicking_fgmissed=True)
for p in all_missed_fgs:
    print p, p.players, p.kicking_fgmissed_yds
(GB, SEA 1, Q1, 4 and 1) (8:10) M.Crosby 18 yard field goal is GOOD, Center-B.Goode, Holder-T.Masthay.
(GB, SEA 1, Q1, 4 and 1) (5:10) M.Crosby 19 yard field goal is GOOD, Center-B.Goode, Holder-T.Masthay.
(GB, SEA 22, Q2, 4 and 1) (9:42) M.Crosby 40 yard field goal is GOOD, Center-B.Goode, Holder-T.Masthay.
(GB, SEA 30, Q4, 4 and 7) (10:58) M.Crosby 48 yard field goal is GOOD, Center-B.Goode, Holder-T.Masthay.
(GB, SEA 30, Q4, 4 and 4) (:19) M.Crosby 48 yard field goal is GOOD, Center-B.Goode, Holder-T.Masthay.
(NE, IND 3, Q2, 4 and 3) (:13) S.Gostkowski 21 yard field goal is GOOD, Center-D.Aiken, Holder-R.Allen. NE 3-Gostkowski now has 109 career postseason points, 8th all-time.
(SEA, NE 8, Q3, 4 and 1) (11:13) S.Hauschka 27 yard field goal is GOOD, Center-C.Gresham, Holder-J.Ryan.
--------------------------------------------------
(IND, NE 33, Q1, 4 and 10) (4:55) A.Vinatieri 51 yard field goal is No Good, Wide Right, Center-M.Overton, Holder-P.McAfee. IND 4-Vinatieri 30th career postseason game, new NFL record (Jerry Rice, 29). [A.Vinatieri] 51

I'm sure @BurntSushi will be along shortly to offer a more elegant solution, but this could get you started now...

mesee298 commented 9 years ago

That worked great. Thank you @ochawkeye. Would you be interested in building a scoring program for our site?

rjhaz commented 9 years ago

Interesting… While I am not sure anyone is interested in programming your site for you, my partner Jim and I converted several of these Python programs to PHP, including one that reads the NFL JSON file and creates the scoring for our web site (Jim did the scoring program conversion)… our web site is www.raysffl.com. Our site is LAMP based and has been around, in one form or another, for a long, long time… (A brief history is on the home page). It is not hard to do and Andrew’s code is very easy to follow (and convert for that matter)…. Give it a try…

Ray

From: mesee298 [mailto:notifications@github.com] Sent: Monday, March 30, 2015 12:51 PM To: BurntSushi/nflgame Subject: Re: [nflgame] Getting individual kicking_fgm information (#116)

That worked great. Thank you @ochawkeye https://github.com/ochawkeye . Would you be interested in building a scoring program for our site?

— Reply to this email directly or view it on GitHub https://github.com/BurntSushi/nflgame/issues/116#issuecomment-87805784 . https://github.com/notifications/beacon/ACPnt29TzccIT3sMG5-lBg9EVdimuC_Oks5n6aCfgaJpZM4D3Pym.gif