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 411 forks source link

Trouble Connecting the Dots #174

Open ToddAtWSU opened 8 years ago

ToddAtWSU commented 8 years ago

I am trying to parse through a week's worth of stats for use in my fantasy football league which has a very unique scoring system. Normally I spend a couple hours each week entering all the stats into a Qt/C++ application I wrote for calculating our team scores. However, I am attempting to write a Python script to parse my rosters file, and then use nflgame to get the pertinent stats for the needed players and output these into a CSV file for my Qt application to load.

I followed the basic introduction that just called nflgame.combine() and it returns a list of players with "First Name Initial.Last Name". I had this working until I noticed when I expected Matt Bryant's stats, I got Martavis Bryant instead. This confirmed my fear of the First Initial not working.

So I have modified my script to use nflgame.find() on the whole Player Name. When I have a conflict, I use the keyboard to select which index I want to correctly choose the right player.

Unfortunately, this returns me a different object than I used to get. Previously, I would say:

playerStats = nflgame.combine(week)
for player in players: #players is list of "First Initial.Last Name"
    member = playerStats.name(player)
    print member.player.full_name
    if member.has_cat("passing"):
        print str(member.passing_yds)

Now however, once I know which index find() returns, all I can figure out what to do is something like this:

member2 = nflgame.find(player, None)[playerIndex]
print member2.full_name

So it appears to me that member2 is essentially the same as member.player. How do I get to the statistics for member2?

Any advice would be greatly appreciated and please let me know if anything confuses you. Thanks! Todd

ToddAtWSU commented 8 years ago

Wow, I was premature in posting my question. Shortly after posting it, I realized I could do this:

member = playerStats.playerid(nflgame.find(player, None)[playerIndex].player_id);

playerIndex is found with the nflgame.find command and playerStats is from nflgame.combine_game_stats(week)

Thanks anyways!

ochawkeye commented 8 years ago

Worth noting...if you know the team the player you are interested in plays for, .find can use that info as well.

import nflgame
for player in nflgame.find('Charles Johnson'):  # Here I didn't specify a team
    print player
print '-'*79
for player in nflgame.find('Charles Johnson', 'MIN'):  # Here I did specify a team
    print player
Charles Johnson (DE, CAR)
Charles Johnson (WR, MIN)
-------------------------------------------------------------------------------
Charles Johnson (WR, MIN)
ochawkeye commented 8 years ago

And before @BurntSushi can have an opportunity to usher you that way, an example of how nfldb is superior:

import nfldb
db = nfldb.connect()

q = nfldb.Query(db)
q.player(gsis_name='C.Johnson')
for player in q.as_players():
    print player

print '-'*79

q = nfldb.Query(db)
q.player(gsis_name='C.Johnson', team='MIN')  # Only "C.Johnson" from a specific team
for player in q.as_players():
    print player

print '-'*79

q = nfldb.Query(db)
q.player(gsis_name='C.Johnson', position='WR')  # Only "C.Johnson" from a particular position
for player in q.as_players():
    print player
Cort Johnson (UNK, UNK)
Charles Johnson (CAR, DE)
Cam Johnson (UNK, UNK)
Curtis Johnson (UNK, UNK)
Chris Johnson (ARI, RB)
Calvin Johnson (DET, WR)
Charlie Johnson (UNK, UNK)
Charles Johnson (MIN, WR)
Chris Johnson (UNK, UNK)
Chris Johnson (UNK, UNK)
Chad Johnson (UNK, UNK)
-------------------------------------------------------------------------------
Charles Johnson (MIN, WR)
-------------------------------------------------------------------------------
Calvin Johnson (DET, WR)
Charles Johnson (MIN, WR)
ToddAtWSU commented 8 years ago

I know I read that nfldb is faster, but nflgame gave me the player stats I was looking for and I output them to a CSV file within 30 seconds for over 60 players I was interested in. What kind of gains would using nfldb provide me? It seems like more of a hassle since I need to install PostgreSQL to handle the database, right?

The only stats I am currently missing are the lengths of the FGs made as we use different points for various lengths, and figuring out how to get some team based stats, i.e. pts scored, pts given up, offensive turnovers, and defensive turnovers to name a few.

Are these possible in either system? I am assuming the team stats are, but not the field goal lengths unless I want to traverse every play which seems like that would take a lot of time.

Reopening since this conversation is continuing...please close if you would prefer it closed.

ochawkeye commented 8 years ago

Field goals you're going to have to evaluate each play, but it doesn't take that long. In our league, we award different points for field goals less than 40 yards, less than 50 yards, and 50+ yards. Going through all of week 8 took 1.6s on my hunk-o-junk PC.

import nflgame
games = nflgame.games(year=2015, week=8)
plays = nflgame.combine_plays(games)
for play in plays.filter(kicking_fgm=True):
    for player in play.players:
        print player.player, play.kicking_fgm, play.kicking_fgm_yds
Stephen Gostkowski (K, NE) 1 52
Stephen Gostkowski (K, NE) 1 36
Matt Prater (K, DET) 1 35
Cairo Santos (K, KC) 1 33
Justin Tucker (K, BAL) 1 48
...
Adam Vinatieri (K, IND) 1 50
Graham Gano (K, CAR) 1 42
Graham Gano (K, CAR) 1 52
ochawkeye commented 8 years ago

On the defensive points given up side of things, my approach is so convoluted I'm not even sure how I can properly explain it.

Tells me that it is definitely time to refactor that gibberish I wrote a few years ago for my league scoring.

BurntSushi commented 8 years ago

For reference, this is the module I wrote which does scoring based on any scheme you give it: https://github.com/BurntSushi/nflfan/blob/master/nflfan/score.py I don't necessarily recommend using nflfan, but certainly, looking at that code for inspiration or copying parts of it might be useful. e.g., Computing "defense points allowed" is pretty hairy: https://github.com/BurntSushi/nflfan/blob/master/nflfan/score.py#L124 Porting that code to nflgame, I suspect, would be quite slow.