Created
November 25, 2017 17:17
-
-
Save abresler/0ae2ea42a4f50182b4443d2545f701ea to your computer and use it in GitHub Desktop.
new nba stats player ids
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(dplyr) | |
library(stringr) | |
library(jsonlite) | |
library(purrr) | |
library(readr) | |
library(glue) | |
library(tidyr) | |
parse_for_players <- | |
function() { | |
url <- "http://stats.nba.com/js/data/ptsd/stats_ptsd.js" | |
json <- | |
url %>% | |
readr::read_lines() %>% | |
str_replace_all("var stats_ptsd =|\\;", "") %>% | |
jsonlite::fromJSON(flatten = TRUE, | |
simplifyDataFrame = TRUE) | |
df_players <- | |
json$data$players %>% | |
dplyr::as_data_frame() %>% | |
purrr::set_names( | |
c( | |
"idPlayer", | |
"namePlayerLastFirst", | |
"isActive", | |
"yearSeasonFirst", | |
"yearSeasonLast", | |
"idTeam", | |
"hasGamesPlayedFlag" | |
) | |
) %>% | |
mutate_at( | |
c( | |
"idPlayer", | |
"isActive", | |
"idTeam", | |
"yearSeasonLast", | |
"yearSeasonFirst" | |
), | |
funs(. %>% as.integer()) | |
) %>% | |
mutate( | |
isActive = as.logical(isActive), | |
countSeasons = (yearSeasonLast - yearSeasonFirst) | |
) | |
most_recent <- | |
df_players %>% pull(yearSeasonFirst) %>% max(na.rm = T) | |
df_players <- | |
df_players %>% | |
mutate(hasGamesPlayedFlag = ifelse(hasGamesPlayedFlag == "Y", TRUE, FALSE), | |
idTeam = ifelse(idTeam == 0, NA, idTeam), | |
isRookie = ifelse(countSeasons == 0 & yearSeasonFirst == most_recent, TRUE, FALSE), | |
urlPlayerStats = glue::glue("http://stats.nba.com/player/{idPlayer}"), | |
urlPlayerThumbnail = glue::glue("http://stats.nba.com/media/players/230x185/{idPlayer}.png") %>% as.character(), | |
urlPlayerHeadshot = ifelse(idTeam %>% is.na(), | |
"http://stats.nba.com/media/img/league/nba-headshot-fallback.png", | |
glue::glue("https://ak-static.cms.nba.com/wp-content/uploads/headshots/nba/{idTeam}/{yearSeasonLast}/260x190/{idPlayer}.png") | |
)) | |
df_players <- | |
df_players %>% | |
mutate(NP = namePlayerLastFirst %>% sub("\\,", "\\:",.)) %>% | |
tidyr::separate(NP, into = c("namePlayerLast", "namePlayerFirst"), sep = "\\:") %>% | |
mutate(namePlayer = ifelse(namePlayerFirst %>% is.na(), namePlayerLast, | |
str_c(namePlayerFirst, namePlayerLast, sep = " ")) %>% str_trim()) %>% | |
dplyr::select(idPlayer, namePlayer, everything()) %>% | |
suppressWarnings() | |
df_players <- | |
df_players %>% | |
mutate(urlPlayerThumbnail = if_else(yearSeasonFirst >= 2017, urlPlayerHeadshot, urlPlayerThumbnail)) | |
df_players | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, i am curious as to where you were able to find this json link. How were you able to acquire this url?