Updated NFL Alerts Python Script

Earlier this week, I posted about a Python script to send alerts when the Colts or their opponent have a big play in a game.  I will not be able to watch their regular season opener game, so I wanted to broaden the script and schedule it to be run during that day.  This way, I’ll still be up to date with the big plays even though I know I won’t be able to watch the game.

Broadening the script was simple.  I added the following variables to the beginning of the script and replaced the corresponding values with their new variables:

Down the line, I could completely functionize the script, and allow these variables to be declared within the command prompt when the script is called.  For now, I decided to just leave them in the script itself.


Scoring Plays

Next, I wanted to make the alerts be a little more meaningful.  The alert for a scoring play was already pretty good – it sends something like: BUF – Q4 – TD – J.Boykin 4 yd. pass from C.Jones (pass failed) Drive: 8 plays, 83 yards in 1:08 IND (19) at BUF (18).  This is good, and in fact it is what I want the rest of the alerts to look like.  However, I’d like the subject of the email to have the name of the team that scored (before it was just ‘Scoring Play’).

To do that, I needed to find out how to get the name of the scoring team.  This was a little tricky because the documentation for the nflgame library, though pretty good, doesn’t give a good indication on how to find this.  I was able to figure out how to get a list of all of the plays:

There were several issues with this, though.  For one, you can only call the plays instance once.  I also couldn’t figure out how to get the last play.  After reading Issue #26 on the project’s Github page, I figured out how to populate all of the plays in a list.  To do this, we use the list function:

Grabbing the last play is easy after we have all of the plays in a list:

If we print out lastplay, we get:

This isn’t very helpful, though.  To see the properties of that object, I created a really useful function:

Any time I’m not sure what properties are part of a variable, I’ll set up that function.  It is incredibly useful for troubleshooting.  If I run props(lastplay) I get the output:

[‘down’, ‘passing_twopta’, ‘time’, ‘drive’, ‘passing_twoptmissed’, ‘note’, ‘players’, ‘playid’, ‘yards_togo’, ‘yardline’, ‘team’, ‘touchdown’, ‘home’, ‘data’, ‘events’, ‘desc’]

That means I can run lastplay.down, lastplay.passing_twopta, lastplay.time, etc.  The one I care about here is team.  I change the subject of my alert to be ‘Team Scoring Play’:

I let the body of the message be ‘Score : Team Name Play Description’:

In the end, the if statement for a scoring play is:


For turnovers, I want the subject to be ‘Team Name Turnover # (number)’ and the body to be a description of the play.  To do this, the If statement will be similar to the last one:



Again, this will be similar to the others:

This will send me an email with the subject ‘(Team name) Penalty for (number of yards) yds’, where the body is a description of the play.

The final step is to schedule this to run (as we went through here).

I’m excited to see how this works during a game.  Unfortunately, I won’t be around for the next game so if it doesn’t work, I’ll be out of luck.  The suspense of that is a lot of fun for me, though, so I can’t wait!


Have questions or suggestions?  Please feel free to comment below or contact me.


One thought on “Updated NFL Alerts Python Script

Leave a Reply

Your email address will not be published. Required fields are marked *