Lab 11: JSON and Baseball

Sports statistics is big business, and with related events like a booming sports betting industry, more people are looking to automated methods for data analysis. This lab exposes you to data access from an online API, and shows you how to pull data from an easy-to-use Major League Baseball (MLB) player search program. The best part of the lab is that we do not have any data files. Instead, you'll make 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.

Step 1 (40%): Write the get_team_ids function

Create a file part1.py

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 = { 'sportId':'1', 'season':'2024' }
r = requests.get(url="https://statsapi.mlb.com/api/v1/teams", 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 type(), .keys(), and print() liberally as you debug to see what it has! Pull out the debugger if you need it. There are lots of name fields, so find the right one to match our output :)

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 part1.py
{'Oakland Athletics': 133, 'Pittsburgh Pirates': 134, 'San Diego Padres': 135, 'Seattle Mariners': 136, 'San Francisco Giants': 137, 'St. Louis Cardinals': 138, 'Tampa Bay Rays': 139, 'Texas Rangers': 140, 'Toronto Blue Jays': 141, 'Minnesota Twins': 142, 'Philadelphia Phillies': 143, 'Atlanta Braves': 144, 'Chicago White Sox': 145, 'Miami Marlins': 146, 'New York Yankees': 147, 'Milwaukee Brewers': 158, 'Los Angeles Angels': 108, 'Arizona Diamondbacks': 109, 'Baltimore Orioles': 110, 'Boston Red Sox': 111, 'Chicago Cubs': 112, 'Cincinnati Reds': 113, 'Cleveland Guardians': 114, 'Colorado Rockies': 115, 'Detroit Tigers': 116, 'Houston Astros': 117, 'Kansas City Royals': 118, 'Los Angeles Dodgers': 119, 'Washington Nationals': 120, 'New York Mets': 121}

Step 2 (80%): Write the get_players function

Copy your part1.py to part2.py so you have a working backup.

After Step 1, you have a dictionary of teams so you can lookup the user's team ID. You must now query for all players on that team. This is one more remote call to the website. Your output here will be this:

Required output:

Team? Baltimore Orioles
Adley Rutschman C 35
Albert Suárez P 49
Blake Hunt C 81
Bryan Baker P 43
Cade Povich P 37
Cedric Mullins CF 31
Chayce McDermott P 60
Cionel Pérez P 58
Coby Mayo 3B 16
Colin Selby P 60
Colton Cowser LF 17
  ...

Required: Define a Player class:

  1. Your class must be instantiated by the following statement:
    p = Player(name, position, number)
  2. Your class must have one function: pretty_print()

Required: Write a function called get_players(id):

  1. It has one argument: a team's int ID (e.g., 110 for Baltimore Orioles)
  2. It returns a List of Player objects (players on that team)

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 to query for a team's roster of players. Here is an example for Baltimore Orioles:


r = requests.get(url="https://statsapi.mlb.com/api/v1/teams/110/roster?season=2024")
data = r.json()

Remember, Baltimore Orioles' team_id is 110. Note that the url argument to request.get is just a string -- what you need to do within the body of your function is to "construct" this string so that it contains the id you passed as an argument.

Your function must return a List of Player objects. You will define a Player class, and then you will make a Player object for each player in the JSON, and put them in a list. Your job here is to use your class to then create a list of Player objects. Your class must be usable like so:

# Create one player with all attributes
p = Player(name, position, number)
# Print the player in a nice format
p.pretty_print()

Still confused? We want you to explore the JSON that is returned. You will make a Player object, giving it three different values ... so your job is to find those three 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 (100%): Complete the program

Copy part2.py to part3.py to keep a working backup.

You're almost finished. You now have a function to get all the players for one team. This final step is to write the core program that interacts with the user to retrieve positions. 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.

Your program should be able to produce this:

Team? Baltimore Orioles
Position? C
Adley Rutschman C 35
Blake Hunt C 81
René Pinto C
Position? P
Albert Suárez P 49
Bryan Baker P 43
Cade Povich P 37
Chayce McDermott P 60
Cionel Pérez P 58
Colin Selby P 60
Dean Kremer P 64
Félix Bautista P 74
Grayson Rodriguez P 30
Gregory Soto P 65
Jacob Webb P 71
Keegan Akin P 45
Kyle Bradish P 38
Luis González P
Seranthony Domínguez P 56
Thaddeus Ward P
Trevor Rogers P 28
Tyler Wells P 68
Yennier Cano P 78
Zach Eflin P 24
Position? quit

(optional) Extra Credit: make it more useful

Create a separate extracredit.py program for this.

  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 their jersey number. Search the Web for how to sort a list of custom objects like we have here.

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.

What to turn in

Visit the submit website and upload your three programs.

submit -c=sd211 -p=lab11 part1.py part2.py part3.py

Visit the submit website and upload your programs.