Lab 11: JSON and Baseball

Major League Baseball is back! This lab builds an easy-to-use MLB player search program. The best part of the lab is that we do not have any data files. Instead, you'll be making LIVE queries to a web server database to get the information you need. The lab will use a few of our recently learned skills: JSON, web queries, and classes.

Goal

Let's start with the goal this week, and then the instructions will step you through it. We want a program that searches for a team, and then searches for players on that team based on their positions. Your program should look like this when run (red is user input):

Team? Orioles
Position? CF
Ryan McKenna (CF)	bats R	height 5'11''	weight 195
Cedric Mullins (CF)	bats L	height 5'8''	weight 175
Position? C
Anthony Bemboom (C)	bats L	height 6'2''	weight 200
Robinson Chirinos (C)	bats R	height 6'1''	weight 220
Nick Ciuffo  (C)	bats L	height 6'0''	weight 205
Pedro Severino (C)	bats R	height 6'1''	weight 235
Chance Sisco (C)	bats L	height 6'3''	weight 210
Austin Wynns (C)	bats R	height 6'0''	weight 190
Position? 2B
Jahmai Jones (2B)	bats R	height 6'0''	weight 210
Domingo Leyba (2B)	bats S	height 5'11''	weight 205
Rougned Odor (2B)	bats L	height 5'11''	weight 200
Chris Owings (2B)	bats R	height 5'10''	weight 185
Rio Ruiz     (2B)	bats L	height 6'2''	weight 220
Ramon Urias  (2B)	bats R	height 6'0''	weight 190
Pat Valaika  (2B)	bats R	height 6'0''	weight 200
Stevie Wilkerson (2B)	bats S	height 6'2''	weight 200
Position? 1B
Trey Mancini (1B)	bats R	height 6'3''	weight 230
Ryan Mountcastle (1B)	bats R	height 6'4''	weight 230
Position? SS
Freddy Galvis (SS)	bats S	height 5'10''	weight 190
Richie Martin (SS)	bats R	height 6'0''	weight 190
Position? quit  

Step 1: Write the get_team_ids function

Team? Orioles

When the user enters their team, we have to first find the team's ID in the baseball database. We want to ask for all the players on this team, but we can't do that without knowing the team's ID first. Step one is thus to ask for ALL known team IDs from the Web database. You'll then save these in a dictionary.

Required: Write a function called get_team_ids():

  1. This function takes zero arguments.
  2. This function returns a dictionary of team names mapped to IDs.

The function's only job is to query the baseball website, process the JSON of teams that the website returns, and then build a dictionary that maps team names (strings) to IDs (ints). The function returns this dictionary.

You'll need this code to make the website call and get the JSON:

import requests
      
params = { 'sport_code':"'mlb'", 'sort_order':'name_asc', 'season':'2022' }
r = requests.get(url="http://lookup-service-prod.mlb.com/json/named.team_all_season.bam", params=params)
data = r.json()

That's all you need to get the data. Now climb through the data variable (a dictionary) and get those names and IDs! It's up to you to figure out the format that was returned. Use print() liberally as you debug to see what it has!

In this step you must (1) write your function, (2) call your function, and (3) print out the resulting dictionary. Your output for this Step should look like this:

$ python3 mlb.py
{'AL Div. Winner #1': '11', 'AL Lower Seed': '', 'AL Wild Card #1': '11', 'AL Wild Card #2': '11', 'AL All-Stars': '', 'D-backs': '109', 'Braves': '144', 'ATL / LAD': '', 'Orioles': '110', 'BOS / NY': '11', 'Red Sox': '111', 'Cubs': '112', 'White Sox': '145', 'Reds': '113', 'Guardians': '114', 'Rockies': '115', 'Tigers': '116', 'HOU / ATL': '11', 'HOU / CWS': '11', 'Astros': '117', 'Royals': '118', 'LAD / HOU': '11', 'LA / STL': '11', 'Angels': '108', 'Dodgers': '119', 'Marlins': '146', 'Brewers': '158', 'Twins': '142', 'NL All-Stars': '', 'Mets': '121', 'Yankees': '147', 'NL Central champ': '11', 'NL East champ': '11', 'NL West champ': '11', 'NL West runner-up': '11', 'NL Wild Card #2': '11', 'No team': '', 'Athletics': '133', 'OOC': '', 'Phillies': '143', 'Pirates': '134', 'Padres': '135', 'Giants': '137', 'Mariners': '136', 'SF / ATL': '', 'Cardinals': '138', 'Rays': '139', 'Rangers': '140', 'To Be Determined': '', 'Blue Jays': '141', 'Nationals': '120'}

Step 2: Write the get_players function

Position? CF

After Step 1, you have a dictionary of teams so you can lookup the user's team ID in it. The user will then enter a position. You must now query for all players on that team. This is one more remote call to the website.

Required: Write a function called get_players(id):

  1. It has one argument: a team's int ID (e.g., 118)
  2. It must return a List of Player objects

Your function will query the MLB website for the given team's ID, process the JSON of all the players, and build a List of Player objects. You need the query code, so here it is:

params = { 'start_season':2021, 'end_season':2022, 'team_id':team_id }
r = requests.get(url="http://lookup-service-prod.mlb.com/json/named.roster_team_alltime.bam", params=params)
data = r.json()

Look at the team_id variable in the params dictionary. That should be your ID given to the function, so adjust the name accordingly to your variable.

Your function must return a List of Player objects. This is new! You will make an object Player for each player in the JSON, and put them in a list. We are giving you the Player class. Feel free to copy this into the top of your program. You don't need to make any changes to it. Your job here is to use the class we give you to then create a list of Player instances/objects.

class Player:
    def __init__(self, name, position, bats, heightf, heightin, weight):
        self.name = name
        self.position = position
        self.bats = bats
        self.heightin = int(heightf)*12 + int(heightin)
        self.weight = int(weight)

    def pretty_print(self):
        pname = self.name
        while len(pname) < 12:
            pname += ' '
        print("%s (%s)\tbats %s\theight %d'%d''\tweight %d" % (pname, self.position, self.bats, self.heightin//12, self.heightin%12, self.weight))

Still confused? Well I want you to explore the JSON that is returned. In order to make a Player object, you need to give it 6 different values (see the __init__ function?) ... so your job is to find those 6 values in the JSON for each player. Then make a Player object and append it to a list that you'll return after looping over the JSON.

Still confused? You might want to review the examples on the bottom of the lecture notes. Those examples are looping over JSON values of baseball teams. You'll do something similar but obviously with different dictionary attributes for the players.

Step 3: Complete the program

You're almost finished. You now have a function to get team IDs and a function to get all players for one team. This final step is to write the core program that interacts with the user. The main loop that asks the user for their player positions is straightforward. Remember, they just enter a team name once at the start, and then you loop for player positions until they type "quit".

You must use the functions you wrote above.

Required: your output should match EXACTLY the program output at the top of this lab.

(optional) Extra Credit: make it more useful

  1. Allow the user to switch to a different team without quitting the program. You can makeup a new user command as needed.
  2. When you print the matching players, sort them by height. Search the Web for how to sort a list of custom objects like we have here.
  3. The API for the baseball database is here. Add some other neat search feature...up to you!

Document what you did if you completed any of this extra credit. Include instructions on the proper user commands to execute your features. Points awarded will be based on difficulty and substance. #1 above is minimal, but #3 is more.

What to turn in

Visit the submit website and upload your python program.