R/get_shot_chart_data.R

#' @title Scrape Shot Chart Data From NBA.com
#' @description scrapes all shot chart data from nba.com API
#' @keywords NBA stat.nba.com
#' @importFrom magrittr %>%
#' @export
#' @examples
#' get_player_shot_chart(player_name)

get_shot_chart_data <- function(player, season, season_type='Regular%20Season', player_position = '',
                                  context_measure = 'FGA', date_from = '', date_to = '', game_id = '',
                                  game_segment = '', last_n_games = 0, league_id = '00', location = '',
                                  month = 0, opp_team_id = 0, outcome = '', period = 0, position = '',
                                  rookie_year = '', season_segment = '', team_id = 0, vs_conf = '',
                                  vs_div = '', team = FALSE) {


  if (team == FALSE) {
    if (player %in% c('all','league','0')) {
      player_id <- 0

    } else {

      player_ids_df <- readr::read_csv('https://raw.githubusercontent.com/emilykuehler/basketballstatsR/master/data-raw/player_ids.csv') %>%
        dplyr::filter(player_name==tolower(player))

      if (nrow(player_ids_df)==1) {
        player_id <- player_ids_df$player_id
      } else if (nrow(player_ids_df)==0) {
        print ('Player name spelled incorrectly or player not in NBA.')
        player_id <- NA
      } else {
        print ("TO DO: write code for duplicate names")
        player_id <- "dups"
      }
    }

  } else {
    player_id <- 0
  }



  base_url = 'https://stats.nba.com/stats/shotchartdetail/?'

  shots_url <- paste0(base_url, 'PlayerID=', player_id,
                      '&Season=', season,
                      '&SeasonType=', season_type,
                      '&PlayerPosition=', player_position,
                      '&ContextMeasure=', context_measure,
                      '&DateFrom=', date_from,
                      '&DateTo=', date_to,
                      '&GameID=', game_id,
                      '&GameSegment=', game_segment,
                      '&LastNGames=',  last_n_games,
                      '&LeagueID=', league_id,
                      '&Location=', location,
                      '&Month=', month,
                      '&OpponentTeamID=', opp_team_id,
                      '&Outcome=', outcome,
                      '&Position=', position,
                      '&RookieYear=', rookie_year,
                      '&SeasonSegment=', season_segment,
                      '&TeamID=', team_id,
                      '&VsConference=', vs_conf,
                      '&VsDivision=', vs_div,
                      '&Period=', period)

  print (shots_url)

  shots_json <-
    curl::curl(shots_url) %>%
    jsonlite::fromJSON()

  player_shot_df <- data.frame(shots_json$resultSets$rowSet[[1]], stringsAsFactors = F)
  names(player_shot_df) = tolower(shots_json$resultSets$headers[[1]])

  player_shot_df <- player_shot_df %>%
    dplyr::mutate(game_id = as.numeric(game_id),
                  game_event_id = as.numeric(game_id),
                  player_id = as.numeric(player_id),
                  team_id = as.numeric(team_id),
                  period = as.numeric(period),
                  minutes_remaining = as.numeric(minutes_remaining),
                  seconds_remaining = as.numeric(seconds_remaining),
                  shot_distance = as.numeric(shot_distance),
                  loc_x = as.numeric(loc_x),
                  loc_y = as.numeric(loc_y),
                  shot_attempted_flag = as.numeric(shot_attempted_flag),
                  shot_made_flag = as.numeric(shot_made_flag))

  return (player_shot_df)
}
emilykuehler/basketballstatsR documentation built on May 31, 2019, 10:01 a.m.