Python Script for NFL Big Play Alerts

Football season officially starts this week!  To celebrate, I wrote a quick Python script to send me an email and/or text message any time something ‘major’ happens in a Colts game.  Right now, this script considers scoring, turnovers, and penalties as ‘major’ events.  This was a pretty quick script for me to write.  I hope to make the notifications better in the future.  The goal of this script was to simply get the base functionality of seeing an event and sending an alert working.

The Script

You can grab a copy of the script from my Gist here.

For this, we will need to install two Python modules:

I’ve discussed the nflgame module before.  In case you missed it, this is an awesome module that pulls in NFL stats data from NFL Game Center.

Import those two modules as well as the built in module time:

Next, we will write a function to send an email:

The input for this function will be the subject of the email as well as the body of the email.  Insert your own gmail email address and password into the first two lines of the password.  The next four lines (that start with server), set up the SMTP server and log you in.  Next, put the email address you want the email to come from (typically, this should be your own email).  Then, in the TO variable, put the email address(es) you want to send the message to.  Even if you only want to send the email to just one address, you still need to keep this variable as a list.  If you want your alert to be texted, you can email a text to your phone by doing something similar to ’’ (Google your phone provider to see the exact format).

Next, we will prepare the message.  Be careful here!  The message string cannot contain any tabs.  If you define a function (like I have), make sure there is no white space before each line in the message.

Finally, we will print out the message, send the email, and quit the server.

Next, we will initialize our variables:

First, we get the game data for the game we want.  In this instance, I am getting game data for the Indianapolis vs Cincinnati game in the 4th week of the 2016 preseason and setting it to the variable g.  Next, we will get the current number of scoring plays (scores0), number of home/away team turnovers (home/awayto0), number of home/away penalties (home/awaypenalty0), and finally, the number of yards that resulted from home/away penalties (home/awaypenyds0).

The rest of the script runs while the game is still in progress.  To check if the game is in progress, we use g.game_over().  If this object is False, the game is ongoing:

We grab the game stats again by calling the same line as before:

We do this so that we can get any of the plays that just happened (in the end, we will do this every 2 minutes).  We also grab the game stats using the line:

The objects and behave slightly differently and can provide slightly different stats.  I wanted a few things from both, so I called the game stats using both methods.  There is probably a better way to do this with the nflgame module, but I wanted to get the script done before our game started.

Next, I grab all of the current current scoring plays, turnovers, and penalties:

Notice that this is exactly what we did before to initialize our variables.  The next five If statements check to see if any of the variables were updated.  If any of them were updated, an email is sent using the send function with the appropriate subject and body to the message.  After all If statements are checked, we wait 2 minutes and do the loop again.  When the game is over, the While loop will exit and an email will be sent with the final score.

Next Steps

I’d like the body in the alerts to be a little bit better.  Right now, the emails typically just say, for instance, ‘Home Turnover’ with the body giving the current number of turnovers.  I would like to change that to have the team name instead of Home/Away as well as who turned the ball over, where it was at, etc.  Eventually, it would be cool to integrate this into a Twitter account (this idea is not new and already exists, but it could be fun to do that project myself).


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


3 thoughts on “Python Script for NFL Big Play Alerts

  1. Greetings. I came across your NFL database project. It is very interesting to me. However, it looks like the project stopped after part 1. Did you continue? How did it come out? Did you post any more code? One of the things I am interested in is the comparison between the projected fantasy football points (from Yahoo or ESPN for example) and the actual outcome. Have you thought about this, and if so, where would you get the data for the projections.

    1. Hi Dave!

      I lost some drive for this project and not much else has been done on it. There is a Python module for an NFL database that uses the same data as the nflgame module (which is what I used in this post). The documentation for that db is here( I haven’t looked into it too much, but I think it would be a good starting point.

      If you have any questions or if I can help in any way, let me know.

Leave a Reply

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