{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# # creating a function which takes in a game id and returns the dataframe of the game's play by play data\n", "\n", "\n", "# def get_game_pbp(game_id):\n", "# # Construct the URL for the play-by-play endpoint\n", "# url = f\"https://api.sportradar.us/nba/trial/v8/en/games/{game_id}/pbp.json?api_key={api_key}\"\n", "\n", "# # Make a GET request to the endpoint\n", "# game_data = requests.get(url)\n", "\n", "# # Check if the request was successful\n", "# if game_data.status_code == 200:\n", "# # Convert the response to JSON\n", "# data = game_data.json()\n", "\n", "# # Extract the relevant information\n", "# scheduled = data[\"scheduled\"] # This is when the game was scheduled\n", "# home_team_name = data[\"home\"][\"market\"] + \" \" + data[\"home\"][\"name\"]\n", "# away_team_name = data[\"away\"][\"market\"] + \" \" + data[\"away\"][\"name\"]\n", "# descriptions = []\n", "# clocks = []\n", "# event_types = []\n", "# home_points = []\n", "# away_points = []\n", "# quarters = []\n", "\n", "# for period in data[\"periods\"]:\n", "# for event in period[\"events\"]:\n", "# descriptions.append(\n", "# event.get(\"description\", None)\n", "# ) # This is what happened\n", "# clocks.append(event.get(\"clock\", None)) # This is when it happened\n", "# event_types.append(\n", "# event.get(\"event_type\", None)\n", "# ) # This is the type of event\n", "# home_points.append(\n", "# event.get(\"home_points\", None)\n", "# ) # This is the home team's points\n", "# away_points.append(\n", "# event.get(\"away_points\", None)\n", "# ) # This is the visitor team's points\n", "# quarters.append(period.get(\"number\", None)) # This is the quarter\n", "\n", "# # Create a DataFrame from the extracted information\n", "# df = pd.DataFrame(\n", "# {\n", "# \"date\": scheduled, # This is when the game was scheduled\n", "# \"home_team_name\": home_team_name,\n", "# \"away_team_name\": away_team_name,\n", "# \"description\": descriptions,\n", "# \"clock\": clocks,\n", "# \"event_type\": event_types,\n", "# \"home_points\": home_points,\n", "# \"away_points\": away_points,\n", "# \"quarter\": quarters,\n", "# }\n", "# )\n", "\n", "# # Return the DataFrame\n", "# return df\n", "\n", "# else:\n", "# # If the request was not successful, return None\n", "# return None" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "json_files = []\n", "\n", "# Walk through current directory\n", "for dirpath, dirnames, filenames in os.walk('.'):\n", " # Find JSON files in each directory\n", " for filename in filenames:\n", " if filename.endswith('.json'):\n", " # Append the path to the file\n", " json_files.append(os.path.join(dirpath, filename))" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "import os\n", "import json\n", "import pandas as pd\n", "\n", "def get_game_pbp_from_file(file_path):\n", " # Load JSON data from the file\n", " with open(file_path, 'r') as file:\n", " data = json.load(file)\n", "\n", " # Extract the relevant information\n", " id = data[\"id\"] # This is the game's ID\n", " scheduled = data[\"scheduled\"] # This is when the game was scheduled\n", " home_team_name = data[\"home\"][\"market\"] + \" \" + data[\"home\"][\"name\"]\n", " away_team_name = data[\"away\"][\"market\"] + \" \" + data[\"away\"][\"name\"]\n", " descriptions = []\n", " clocks = []\n", " event_types = []\n", " home_points = []\n", " away_points = []\n", " quarters = []\n", "\n", " if \"periods\" not in data:\n", " print (f\"File {file_path} does not contain play-by-play data\")\n", " return None\n", " \n", " else:\n", "\n", " for period in data[\"periods\"]:\n", " for event in period[\"events\"]:\n", " descriptions.append(event.get(\"description\", None)) # This is what happened\n", " clocks.append(event.get(\"clock\", None)) # This is when it happened\n", " event_types.append(event.get(\"event_type\", None)) # This is the type of event\n", " home_points.append(event.get(\"home_points\", None)) # This is the home team's points\n", " away_points.append(event.get(\"away_points\", None)) # This is the visitor team's points\n", " quarters.append(period.get(\"number\", None)) # This is the quarter\n", "\n", " # Create a DataFrame from the extracted information\n", " df = pd.DataFrame({\n", " \"id\": id, # This is the game's ID\n", " \"date\": scheduled, # This is when the game was scheduled\n", " \"home_team_name\": home_team_name,\n", " \"away_team_name\": away_team_name,\n", " \"description\": descriptions,\n", " \"clock\": clocks,\n", " \"event_type\": event_types,\n", " \"home_points\": home_points,\n", " \"away_points\": away_points,\n", " \"quarter\": quarters,\n", " })\n", "\n", " # Return the DataFrame\n", " return df\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Just testing the function with one file" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | id | \n", "date | \n", "home_team_name | \n", "away_team_name | \n", "description | \n", "clock | \n", "event_type | \n", "home_points | \n", "away_points | \n", "quarter | \n", "
---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "6decf9f6-cf39-4bf4-94b1-0acc349bfb69 | \n", "2023-01-23T02:00:00+00:00 | \n", "Portland Trail Blazers | \n", "Los Angeles Lakers | \n", "Lakers lineup change (Thomas Bryant, LeBron Ja... | \n", "12:00 | \n", "lineupchange | \n", "0 | \n", "0 | \n", "1 | \n", "
1 | \n", "6decf9f6-cf39-4bf4-94b1-0acc349bfb69 | \n", "2023-01-23T02:00:00+00:00 | \n", "Portland Trail Blazers | \n", "Los Angeles Lakers | \n", "Trail Blazers lineup change (Damian Lillard, J... | \n", "12:00 | \n", "lineupchange | \n", "0 | \n", "0 | \n", "1 | \n", "
2 | \n", "6decf9f6-cf39-4bf4-94b1-0acc349bfb69 | \n", "2023-01-23T02:00:00+00:00 | \n", "Portland Trail Blazers | \n", "Los Angeles Lakers | \n", "Thomas Bryant vs. Jusuf Nurkic (Dennis Schrode... | \n", "12:00 | \n", "opentip | \n", "0 | \n", "0 | \n", "1 | \n", "
3 | \n", "6decf9f6-cf39-4bf4-94b1-0acc349bfb69 | \n", "2023-01-23T02:00:00+00:00 | \n", "Portland Trail Blazers | \n", "Los Angeles Lakers | \n", "LeBron James makes two point cutting dunk (Den... | \n", "11:47 | \n", "twopointmade | \n", "0 | \n", "2 | \n", "1 | \n", "
4 | \n", "6decf9f6-cf39-4bf4-94b1-0acc349bfb69 | \n", "2023-01-23T02:00:00+00:00 | \n", "Portland Trail Blazers | \n", "Los Angeles Lakers | \n", "Damian Lillard makes two point jump shot (Josh... | \n", "11:31 | \n", "twopointmade | \n", "2 | \n", "2 | \n", "1 | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
458 | \n", "6decf9f6-cf39-4bf4-94b1-0acc349bfb69 | \n", "2023-01-23T02:00:00+00:00 | \n", "Portland Trail Blazers | \n", "Los Angeles Lakers | \n", "Dennis Schroder makes regular free throw 1 of 2 | \n", "00:30 | \n", "freethrowmade | \n", "112 | \n", "120 | \n", "4 | \n", "
459 | \n", "6decf9f6-cf39-4bf4-94b1-0acc349bfb69 | \n", "2023-01-23T02:00:00+00:00 | \n", "Portland Trail Blazers | \n", "Los Angeles Lakers | \n", "Dennis Schroder makes regular free throw 2 of 2 | \n", "00:30 | \n", "freethrowmade | \n", "112 | \n", "121 | \n", "4 | \n", "
460 | \n", "6decf9f6-cf39-4bf4-94b1-0acc349bfb69 | \n", "2023-01-23T02:00:00+00:00 | \n", "Portland Trail Blazers | \n", "Los Angeles Lakers | \n", "Damian Lillard misses three point pullup jump ... | \n", "00:24 | \n", "threepointmiss | \n", "112 | \n", "121 | \n", "4 | \n", "
461 | \n", "6decf9f6-cf39-4bf4-94b1-0acc349bfb69 | \n", "2023-01-23T02:00:00+00:00 | \n", "Portland Trail Blazers | \n", "Los Angeles Lakers | \n", "LeBron James defensive rebound | \n", "00:21 | \n", "rebound | \n", "112 | \n", "121 | \n", "4 | \n", "
462 | \n", "6decf9f6-cf39-4bf4-94b1-0acc349bfb69 | \n", "2023-01-23T02:00:00+00:00 | \n", "Portland Trail Blazers | \n", "Los Angeles Lakers | \n", "End of 4th Quarter. | \n", "00:00 | \n", "endperiod | \n", "112 | \n", "121 | \n", "4 | \n", "
463 rows × 10 columns
\n", "\n", " | id | \n", "date | \n", "home_team_name | \n", "away_team_name | \n", "description | \n", "clock | \n", "event_type | \n", "home_points | \n", "away_points | \n", "quarter | \n", "
---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "6decf9f6-cf39-4bf4-94b1-0acc349bfb69 | \n", "2023-01-23T02:00:00+00:00 | \n", "Portland Trail Blazers | \n", "Los Angeles Lakers | \n", "Lakers lineup change (Thomas Bryant, LeBron Ja... | \n", "12:00 | \n", "lineupchange | \n", "0 | \n", "0 | \n", "1 | \n", "
1 | \n", "6decf9f6-cf39-4bf4-94b1-0acc349bfb69 | \n", "2023-01-23T02:00:00+00:00 | \n", "Portland Trail Blazers | \n", "Los Angeles Lakers | \n", "Trail Blazers lineup change (Damian Lillard, J... | \n", "12:00 | \n", "lineupchange | \n", "0 | \n", "0 | \n", "1 | \n", "
2 | \n", "6decf9f6-cf39-4bf4-94b1-0acc349bfb69 | \n", "2023-01-23T02:00:00+00:00 | \n", "Portland Trail Blazers | \n", "Los Angeles Lakers | \n", "Thomas Bryant vs. Jusuf Nurkic (Dennis Schrode... | \n", "12:00 | \n", "opentip | \n", "0 | \n", "0 | \n", "1 | \n", "
3 | \n", "6decf9f6-cf39-4bf4-94b1-0acc349bfb69 | \n", "2023-01-23T02:00:00+00:00 | \n", "Portland Trail Blazers | \n", "Los Angeles Lakers | \n", "LeBron James makes two point cutting dunk (Den... | \n", "11:47 | \n", "twopointmade | \n", "0 | \n", "2 | \n", "1 | \n", "
4 | \n", "6decf9f6-cf39-4bf4-94b1-0acc349bfb69 | \n", "2023-01-23T02:00:00+00:00 | \n", "Portland Trail Blazers | \n", "Los Angeles Lakers | \n", "Damian Lillard makes two point jump shot (Josh... | \n", "11:31 | \n", "twopointmade | \n", "2 | \n", "2 | \n", "1 | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
88964 | \n", "81509f8f-9014-460b-b383-70b65a0efdaf | \n", "2023-03-20T01:00:00+00:00 | \n", "Portland Trail Blazers | \n", "LA Clippers | \n", "Paul George defensive rebound | \n", "00:47 | \n", "rebound | \n", "102 | \n", "117 | \n", "4 | \n", "
88965 | \n", "81509f8f-9014-460b-b383-70b65a0efdaf | \n", "2023-03-20T01:00:00+00:00 | \n", "Portland Trail Blazers | \n", "LA Clippers | \n", "Clippers turnover (shot clock violation) | \n", "00:24 | \n", "turnover | \n", "102 | \n", "117 | \n", "4 | \n", "
88966 | \n", "81509f8f-9014-460b-b383-70b65a0efdaf | \n", "2023-03-20T01:00:00+00:00 | \n", "Portland Trail Blazers | \n", "LA Clippers | \n", "Damian Lillard misses three point pullup jump ... | \n", "00:19 | \n", "threepointmiss | \n", "102 | \n", "117 | \n", "4 | \n", "
88967 | \n", "81509f8f-9014-460b-b383-70b65a0efdaf | \n", "2023-03-20T01:00:00+00:00 | \n", "Portland Trail Blazers | \n", "LA Clippers | \n", "Ivica Zubac defensive rebound | \n", "00:16 | \n", "rebound | \n", "102 | \n", "117 | \n", "4 | \n", "
88968 | \n", "81509f8f-9014-460b-b383-70b65a0efdaf | \n", "2023-03-20T01:00:00+00:00 | \n", "Portland Trail Blazers | \n", "LA Clippers | \n", "End of 4th Quarter. | \n", "00:00 | \n", "endperiod | \n", "102 | \n", "117 | \n", "4 | \n", "
88969 rows × 10 columns
\n", "