Skip to content

Instantly share code, notes, and snippets.

@batpigandme
Last active August 29, 2016 13:00
Show Gist options
  • Save batpigandme/ecc27e1176a91c2f5f71 to your computer and use it in GitHub Desktop.
Save batpigandme/ecc27e1176a91c2f5f71 to your computer and use it in GitHub Desktop.
Get NBA games from the Stattleship API.
## install and load the stattleshipR package
devtools::install_github("stattleship/stattleship-r")
library(stattleshipR)
## get your API token stattleship.com
## set API token
set_token('YOUR_ACCESS_TOKEN')
## load NBA teams data frame
load("nba_teams.Rdata")
## Set up your env't
options(stringsAsFactors=FALSE)
## set params according to
## developers.stattleship.com
sport <- "basketball"
league <- "nba"
ep <- "games"
## leave q_body empty to get for all players
## for specific player or team use slug
## set time frame e.g. since="yesterday"
## for finished games status="ended"
## set season_id
q_body <- list(season_id="nba-2015-2016")
## get the games
games <- ss_get_result(sport=sport, league=league, ep=ep, query=q_body, version=1, verbose=TRUE, walk=TRUE)
## bind the data together
gs <- lapply(games, function(x) x$games)
nba_games <- rbindlist(gs)
## if planning to write as a csv drop officials var
## nested list won't write to csv
## nba_games <- nba_games[, official_ids := NULL]
## add team details from nba_teams if retrieved
## nba_games$home_team_slug <- nba_teams[match(nba_games$home_team_id, nba_teams$id),]$slug
## nba_games$away_team_slug <- nba_teams[match(nba_games$away_team_id, nba_teams$id),]$slug
## set the date
nba_games$date<-as.Date(nba_games$started_at)
## get rid of games not yet played if so desired
## nba_games_played <- subset(nba_games, away_team_outcome!="undecided")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment