Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dthyresson/ef6cb037afd73ad4053e to your computer and use it in GitHub Desktop.
Save dthyresson/ef6cb037afd73ad4053e to your computer and use it in GitHub Desktop.
Get all of the game logs for the NBA teams without pre-loading team information.
#########################################################################
## Get NBA Team Game Logs From the Stattleship API
#########################################################################
## install and load the stattleshipR package
devtools::install_github("stattleship/stattleship-r")
library(stattleshipR)
## set API token
set_token('YOUR_ACCESS_TOKEN')
## set up envt
options(stringsAsFactors=FALSE)
## set params
league <- "nba"
sport <- "basketball"
ep <- "team_game_logs"
## leave q_body empty to get all
## or set for specific team or dates
q_body <- list()
## get the team game logs
nba_team_gls <- ss_get_result(sport=sport, league=league, ep=ep, query=q_body, version=1, verbose=TRUE, walk=TRUE)
## bind together data
tgls <- lapply(nba_team_gls, function(x) x$team_game_logs)
team_game_logs <- do.call('rbind', tgls)
team_game_logs <- rbindlist(tgls)
## team game log data cleanup
team_game_logs$coach_technical_fouls[which(is.na(team_game_logs$coach_technical_fouls))] <- 0
team_game_logs$points_biggest_lead[which(is.na(team_game_logs$points_biggest_lead))] <- 0
team_game_logs$technical_fouls[which(is.na(team_game_logs$technical_fouls))] <- 0
## add team details
teams <- lapply(team_game_logs, function(x) x$teams)
all_teams <- do.call('rbind', teams)
the_teams <- all_teams[match(team_game_logs$team_id, all_teams$id),]
team_game_logs$team_slug <- the_teams[match(team_game_logs$team_id, the_teams$id),]$slug
team_game_logs$opponent_slug <- the_teams[match(team_game_logs$opponent_id, the_teams$id),]$slug
## add game details
games <- lapply(games, function(x) x$games)
the_games <- do.call('rbind', games)
the_games <- the_games[match(team_game_logs$game_id, the_games$id),]
team_game_logs$scoreline <- the_games[match(team_game_logs$game_id, the_games$id),]$scoreline
team_game_logs$winning_team_id <- the_games[match(team_game_logs$game_id, the_games$id),]$winning_team_id
team_game_logs$score_differential <- the_games[match(team_game_logs$game_id, the_games$id),]$score_differential
team_game_logs$game_started_at <- the_games[match(team_game_logs$game_id, the_games$id),]$started_at
team_game_logs$game_ended_at <- the_games[match(team_game_logs$game_id, the_games$id),]$ended_at
team_game_logs$game_slug <- the_games[match(team_game_logs$game_id, the_games$id),]$slug
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment