Python - Get your team scores. | mlbgame python package

Submitted by john on Tue, 04/11/2017 - 15:27

pythonSo there you are at work in a 4 plex cube set up and people see your monitors all the time. Yeah, that's a thing. You want to see how your team is doing, right? Easy, pull up the scores in command line. Here is how you do it. 

 

 

 

 

#!/usr/bin/env python
import mlbgame
import datetime
import sys

year, month, day = str(datetime.datetime.now().strftime('%Y,%-m,%-d')).split(',')

if '-all' in sys.argv:
    scores = mlbgame.day(int(year), int(month), int(day))
else:
    scores = mlbgame.day(int(year), int(month), int(day), home="Cubs")

if not scores:
    print "No games are being played."
    exit()

for score in scores:
    print score

So what does all this mean? Let's break it down. 

At the top we see the shebang line. I am really not explaining that. Then we are importing the mlbgame Python library as well as datetime and sys.

I need to get the year, month, and day by themselves for the mlbgame package so I used the datetime package with strftime to create a datetime object that was comma delimited, I casted it as a string and split by the comma so we get a list. Notice I unpacked the list up front with year, month, day. The problem with these are that they are strings and need to be integers, so when I called the .day method of the mlbgame library, I casted the variables as integers as I used them.

The logic control uses argv and looks for the -all keyword to get all the teams, then I have Cubs set as default if I don't feed the script any arguments. Sometimes the scores variable will return empty if no games are scheduled. So I added a conditional statement for that as well allowing us to print No games are being played to stdout and exiting the program before we run a loop to go through all the team scores. 

That is all there is to it. Shout out  pazarino at https://github.com/panzarino/mlbgame for this awesome package. Of course, there is a lot more to this than what I am using it for.