-
Notifications
You must be signed in to change notification settings - Fork 132
/
fetch_shots.R
88 lines (78 loc) · 2.57 KB
/
fetch_shots.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
77
78
79
80
81
82
83
84
85
86
87
88
fetch_shots_by_player_id_and_season = function(player_id, season, season_type = "Regular Season") {
req(player_id, season, season_type)
request = GET(
"https://stats.nba.com/stats/shotchartdetail",
query = list(
PlayerID = player_id,
Season = season,
SeasonType = season_type,
PlayerPosition = "",
ContextMeasure = "FGA",
DateFrom = "",
DateTo = "",
GameID = "",
GameSegment = "",
LastNGames = 0,
LeagueID = "00",
Location = "",
Month = 0,
OpponentTeamID = 0,
Outcome = "",
Period = 0,
Position = "",
RookieYear = "",
SeasonSegment = "",
TeamID = 0,
VsConference = "",
VsDivision = ""
),
add_headers(request_headers)
)
stop_for_status(request)
data = content(request)
raw_shots_data = data$resultSets[[1]]$rowSet
col_names = tolower(as.character(data$resultSets[[1]]$headers))
if (length(raw_shots_data) == 0) {
shots = data.frame(
matrix(nrow = 0, ncol = length(col_names))
)
} else {
shots = data.frame(
matrix(
unlist(raw_shots_data),
ncol = length(col_names),
byrow = TRUE
)
)
}
shots = as_tibble(shots)
names(shots) = col_names
shots = mutate(shots,
loc_x = -as.numeric(as.character(loc_x)) / 10,
loc_y = as.numeric(as.character(loc_y)) / 10 + hoop_center_y,
shot_distance = as.numeric(as.character(shot_distance)),
shot_made_numeric = as.numeric(as.character(shot_made_flag)),
shot_made_flag = factor(shot_made_flag, levels = c("1", "0"), labels = c("made", "missed")),
shot_attempted_flag = as.numeric(as.character(shot_attempted_flag)),
shot_value = ifelse(tolower(shot_type) == "3pt field goal", 3, 2),
game_date = as.Date(game_date, format = "%Y%m%d")
)
raw_league_avg_data = data$resultSets[[2]]$rowSet
league_avg_names = tolower(as.character(data$resultSets[[2]]$headers))
league_averages = as_tibble(data.frame(
matrix(unlist(raw_league_avg_data), ncol = length(league_avg_names), byrow = TRUE)
))
names(league_averages) = league_avg_names
league_averages = mutate(league_averages,
fga = as.numeric(as.character(fga)),
fgm = as.numeric(as.character(fgm)),
fg_pct = as.numeric(as.character(fg_pct)),
shot_value = ifelse(shot_zone_basic %in% c("Above the Break 3", "Backcourt", "Left Corner 3", "Right Corner 3"), 3, 2)
)
return(list(player = shots, league_averages = league_averages))
}
default_shots = fetch_shots_by_player_id_and_season(
default_player$person_id,
default_season,
default_season_type
)