-
Notifications
You must be signed in to change notification settings - Fork 130
/
players_data.R
76 lines (59 loc) · 2.68 KB
/
players_data.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
players_url = "https://stats.nba.com/stats/commonallplayers?LeagueID=00&Season=2020-21&IsOnlyCurrentSeason=0"
request_headers = c(
"Accept" = "application/json, text/plain, */*",
"Accept-Language" = "en-US,en;q=0.8",
"Cache-Control" = "no-cache",
"Connection" = "keep-alive",
"Host" = "stats.nba.com",
"Pragma" = "no-cache",
"Referer" = "https://www.nba.com/",
"Upgrade-Insecure-Requests" = "1",
"User-Agent" = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/601.3.9 (KHTML, like Gecko) Version/9.0.2 Safari/601.3.9"
)
request = GET(players_url, add_headers(request_headers))
players_data = fromJSON(content(request, as = "text"))
players = as_tibble(data.frame(players_data$resultSets$rowSet[[1]], stringsAsFactors = FALSE))
names(players) = tolower(players_data$resultSets$headers[[1]])
players = mutate(players,
person_id = as.numeric(person_id),
rosterstatus = as.logical(as.numeric(rosterstatus)),
from_year = as.numeric(from_year),
to_year = as.numeric(to_year),
team_id = as.numeric(team_id)
)
if (Sys.Date() <= as.Date("2017-10-20")) {
players = mutate(players, to_year = pmin(to_year, 2016))
}
players$name = sapply(players$display_last_comma_first, function(s) {
paste(rev(strsplit(s, ", ")[[1]]), collapse = " ")
})
first_year_of_data = 1996
last_year_of_data = max(players$to_year)
season_strings = paste(first_year_of_data:last_year_of_data,
substr(first_year_of_data:last_year_of_data + 1, 3, 4),
sep = "-")
names(season_strings) = first_year_of_data:last_year_of_data
available_players = filter(players, to_year >= first_year_of_data)
names_table = table(available_players$name)
dupe_names = names(names_table[which(names_table > 1)])
available_players$name[available_players$name %in% dupe_names] = paste(
available_players$name[available_players$name %in% dupe_names],
available_players$person_id[available_players$name %in% dupe_names]
)
available_players$lower_name = tolower(available_players$name)
available_players = arrange(available_players, lower_name)
find_player_by_name = function(n) {
filter(available_players, lower_name == tolower(n))
}
find_player_id_by_name = function(n) {
find_player_by_name(n)$person_id
}
default_player = find_player_by_name("LeBron James")
default_years = as.character(default_player$from_year:default_player$to_year)
default_seasons = as.character(season_strings[default_years])
default_season_ix = ifelse(as.numeric(format(Sys.Date(), "%m")) %in% 6:12, 2, 1)
default_season = rev(default_seasons)[default_season_ix]
default_season_type = "Regular Season"
player_photo_url = function(player_id) {
paste0("https://stats.nba.com/media/players/230x185/", player_id, ".png")
}