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

Inconsistency with gsis_id #213

Open hackerDEpandas opened 8 years ago

hackerDEpandas commented 8 years ago

To preface this I'm fairly new to programming so a solution to this issue might be quite obvious but just not to me yet. I'm using the following code to get players gsis_id:

import nflgame as nf

player_id = nf.find('Julio Jones', 'ATL')[0].gsis_id
print player_id

This works fine for the player Julio Jones, but not so great for his teammate Mohamed Sanu. So when I change the names like such:

import nflgame as nf

player_id = nf.find('Mohamed Sanu', 'ATL')[0].gsis_id
print player_id

I get an error message that says

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

Is there an easy explanation for this inconsistency? Thanks in advanced!

ochawkeye commented 8 years ago
import nflgame

player_id = nflgame.find('Julio Jones', 'ATL')[0].gsis_id
print player_id

player_id = nflgame.find('Mohamed Sanu', 'ATL')[0].gsis_id
print player_id
00-0027944
00-0029632

Works fine on my side...though it's possible NFL.com recently changed his name and you've got a freshly scraped version of Sanu Sr's name? (http://www.ajc.com/news/sports/football/sanu-sr-and-jr-hit-falcons-training-camp/nr6cW/)

The thing about nflgame.find is that it must be an exact match.

import nflgame
games = nflgame.games(2015, kind='REG')
falcon_wrs = [player for player in nflgame.combine_game_stats(games) if player.player.team == 'ATL' and player.player.position == 'WR']
for wr in falcon_wrs:
    print wr.player.name, wr.player.player_id
Mohamed Sanu 00-0029632
Nick Williams 00-0029848
Julio Jones 00-0027944
Eric Weems 00-0024535
Justin Hardy 00-0031570
Devin Hester 00-0024272

(FYI - nfldb offers a far superior fuzzy matching capability when it comes to finding players.)

ochawkeye commented 8 years ago

While it doesn't appear that NFL.com has updated that particular player's name, I do see that this is the time of year that rosters are a bit in flux.

I believe I have a scheduled task run on Tuesday mornings so my nflgame.players were a bit dated. Updating shows Devin Hester vanished from the Atlanta roster.

P:\Projects\Home Computer\Fantasy Football\2015>python player_find.py
Mohamed Sanu 00-0029632
Nick Williams 00-0029848
Julio Jones 00-0027944
Eric Weems 00-0024535
Justin Hardy 00-0031570
Devin Hester 00-0024272

P:\Projects\Home Computer\Fantasy Football\2015>python d:\Python27\Scripts\nflgame-update-players
Loading games for PRE 2016 week 0
Downloading team rosters...
32/32 complete. (100.00%)
Done!
Fetching GSIS identifiers for players not in nflgame...
13/13 complete. (100.00%)
Done!
.
.
.

P:\Projects\Home Computer\Fantasy Football\2015>python player_find.py
Mohamed Sanu 00-0029632
Nick Williams 00-0029848
Julio Jones 00-0027944
Eric Weems 00-0024535
Justin Hardy 00-0031570

Which coincides with reality: http://espn.go.com/nfl/story/_/id/17145704/devin-hester-released-atlanta-falcons

ochawkeye commented 8 years ago

The above was a terrible method of finding a player by position, though I will leave it just so @BurntSushi can cringe at me.

import nflgame
players = nflgame.players
for p_id, player in players.items():
    if player.team=='ATL' and player.position=='WR':
        print p_id, player
00-0029632 Mohamed Sanu (WR, ATL)
00-0032451 Devin Fuller (WR, ATL)
00-0024535 Eric Weems (WR, ATL)
00-0031801 Jordan Leslie (WR, ATL)
00-0032602 J.D. McKissic (WR, ATL)
00-0032595 Daje Johnson (WR, ATL)
00-0032824 David Glidden (WR, ATL)
00-0031570 Justin Hardy (WR, ATL)
00-0029848 Nick Williams (WR, ATL)
00-0028116 Aldrick Robinson (WR, ATL)
00-0031357 C.J. Goodwin (WR, ATL)
00-0027944 Julio Jones (WR, ATL)
00-0030897 Corey Washington (WR, ATL)

It is true that the best way to learn Python is by finding a project you love...

import nflgame
for wr in filter(lambda x: x.team=='ATL' and x.position=='WR', nflgame.players.values()):
    print wr.player_id, wr
hackerDEpandas commented 8 years ago

This is extremely helpful! Thanks for the thorough response! To even further "out" my novice to this, I knew there was a database but had no idea how to call it in my programs. However after your response I'm guessing players = nflgame.players is the code that allows me to talk to the database. Blowing my mind over here! Anyways thanks for the help!

hackerDEpandas commented 8 years ago

If I can just ask one follow up. Is retrieving statistics from each player the same? This is naturally what I thought of:

import nflgame
players = nflgame.players
for p_id, player in players.items():
    if player.team=='ATL' and player.position=='WR':
        print p_id.receiving_rec

but of course it was not successful!

ochawkeye commented 8 years ago

However after your response I'm guessing players = nflgame.players is the code that allows me to talk to the database.

Actually, nfldb is a completely different package put together by the creator of nflgame.

An overly simplified explanation of it is that is takes all of the data that nflgame uses and populates it into a PostgreSQL database. Setup is a bit more entailed, but the benefit is that queries can be made much more quickly against a database than from reading in hundreds of .json files for the various games.

players = nflgame.players is native nflgame code. Think of nflgame.players as all of the player meta data that got harvested off their NFL.com profile page. It contains information like where a player went to school, how tall they are, their uniform number, etc. It does not contain any information about statistics they have recorded and can be looked at outside of any game context.

import nflgame
for player in nflgame.players:
    print player

nflgame.players includes only players that are currently listed on a roster.

In order to peek at a player's statistics, nflgame needs to first generate the stats from play-by-play (technically it's not parsing the play-by-play; the data is present in the json that accompanies the play-by-play).

import nflgame
games = nflgame.games(year=2015, week=1, kind='REG')
for game in games:
    print game

This lists the results of every game played in week 1, 2015.

Extending from there:

import nflgame
games = nflgame.games(year=2015, week=1, kind='REG')
game_participants = nflgame.combine_game_stats(games)
for gp in game_participants:
    print gp, gp.formatted_stats(), gp.player.player_id
print '-'*79
print dir(gp)  # A peek at what can be pulled from this gp
print '-'*79
print dir(gp.player)  # A futher deep dive into what can be looked at from one of gp's attributes.

This provides a pre-formatted display of the statistics for every player that recorded a statistic in week 1, 2015.

hackerDEpandas commented 8 years ago

Excellent! Thanks again! Looks like I have a lot of practice ahead of me.