BurntSushi / nfldb

A library to manage and update NFL data in a relational database.
The Unlicense
1.08k stars 264 forks source link

Noob Request - How to have Quarter show during active games #241

Open rororomyboat opened 7 years ago

rororomyboat commented 7 years ago

Hi Folks, Extremely limited programming experience so I may be asking a stupid q which if so then apologies in advance.

Looking to create a live feed scoreboard for all current games in a given week which will update as they happen.

Output should look like: start_date, home_team, home_score, away_score, away_team, quarter

I can pull everything except quarter. How to I pull a quarter of a given game and combine it to the above? I have the following working:

import nfldb
db = nfldb.connect() 
q = nfldb.Query(db)

q.game(season_year=2016, season_type='Regular')
for pp in q.sort('start_time').limit(10).as_games():
    print pp.start_time, pp.home_team, pp.home_score, pp.away_score, pp.away_team, pp.finished

Thanks in advance for any help!

ochawkeye commented 7 years ago

Have a look at #200

You could look at the last play for a particular game and

import nfldb
db = nfldb.connect()
q = nfldb.Query(db)

def game_phase(g):
    if g.is_playing or g.finished:
        return g.plays[-1].time.phase
    return None

q.game(season_year=2017, week=2, season_type='Preseason')
for pp in q.sort('start_time').as_games():
    print pp.start_time, pp.home_team, pp.home_score, pp.away_score, pp.away_team, pp.finished, game_phase(pp)

Oddly, despite BUF @ PHI being finished, my nfldb tells me there is still 0:11 remaining in Q4.

2017-08-21 19:00:00-05:00 CLE 0 0 NYG False None
2017-08-20 19:00:00-05:00 UNK 0 0 NO False None
2017-08-20 15:00:00-05:00 PIT 0 0 ATL False None
2017-08-19 21:00:00-05:00 ARI 0 0 CHI False None
2017-08-19 21:00:00-05:00 SF 0 0 DEN False None
2017-08-19 21:00:00-05:00 OAK 0 0 LA False None
2017-08-19 19:00:00-05:00 HOU 0 0 NE False None
2017-08-19 18:30:00-05:00 WAS 0 0 GB False None
2017-08-19 18:30:00-05:00 DET 0 0 NYJ False None
2017-08-19 18:00:00-05:00 CIN 0 0 KC False None
2017-08-19 18:00:00-05:00 DAL 0 0 IND False None
2017-08-19 14:00:00-05:00 TEN 0 0 CAR False None
2017-08-18 21:00:00-05:00 SEA 0 0 MIN False None
2017-08-17 19:00:00-05:00 JAC 8 12 TB True Final
2017-08-17 18:00:00-05:00 PHI 20 16 BUF True Q4
2017-08-17 18:00:00-05:00 MIA 7 31 BAL True Final