-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_current_season_ncaa_playerbox.R
272 lines (181 loc) · 9.11 KB
/
get_current_season_ncaa_playerbox.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
install.packages("tidyverse")
library(tidyverse)
install.packages("janitor")
library(janitor)
install.packages("rvest")
library(rvest)
install.packages("anytime")
library(anytime)
install.packages("glue")
library(glue)
options(warn = -1)
get_pitching_box <- function(id){
raw <- glue::glue("https://stats.ncaa.org/contests/{id}/box_score") %>%
readLines()
pitching_url <- raw[grep("Pitching", raw)] %>%
trimws() %>%
stringr::str_remove_all("\\<a href=\"|\"\\>Pitching\\</a\\> \\|") %>%
paste0("https://stats.ncaa.org/", .)
raw <- pitching_url %>%
rvest::read_html() %>%
rvest::html_table()
first_team <- as.character(raw[[6]][1,1])
second_team <- as.character(raw[[7]][1,1])
upd <- rbind(raw[[6]],raw[[7]]) %>%
`names<-`(raw[[6]][2,]) %>%
janitor::clean_names() %>%
dplyr::filter(!(player %in% c(first_team, second_team,"Player","Totals")))
upd$team <- ifelse(upd$player %in% raw[[6]]$X1, first_team, second_team)
upd$opponent <- ifelse(upd$team == first_team, second_team, first_team)
upd[upd == ""] <- "0"
upd[] <- lapply(upd, gsub, pattern="/", replacement="")
upd <- upd %>%
dplyr::mutate(across(3:35, as.numeric)) %>%
dplyr::filter(ip > 0) %>%
dplyr::mutate(game_id = id)
return(upd)
}
get_hitting_box <- function(id){
raw <- glue::glue("https://stats.ncaa.org/contests/{id}/box_score") %>%
rvest::read_html() %>%
rvest::html_table()
first_team <- as.character(raw[[6]][1,1])
second_team <- as.character(raw[[7]][1,1])
upd <- rbind(raw[[6]],raw[[7]]) %>%
`names<-`(raw[[6]][2,]) %>%
janitor::clean_names() %>%
dplyr::filter(!(player %in% c(first_team, second_team,"Player","Totals")))
upd$team <- ifelse(upd$player %in% raw[[6]]$X1, first_team, second_team)
upd$opponent <- ifelse(upd$team == first_team, second_team, first_team)
upd[upd == ""] <- "0"
upd <- upd %>%
dplyr::mutate(across(.cols = 3:76, .fns = \(col) as.numeric(str_remove(col, "/")))) %>%
dplyr::mutate(game_id = id)
return(upd)
}
url_d1 <- glue::glue("https://github.com/tmking2002/softballR-data/blob/main/data/ncaa_scoreboard_2024.RDS?raw=true")
url_d2 <- glue::glue("https://github.com/tmking2002/softballR-data/blob/main/data/ncaa_scoreboard_D2_2024.RDS?raw=true")
url_d3 <- glue::glue("https://github.com/tmking2002/softballR-data/blob/main/data/ncaa_scoreboard_D3_2024.RDS?raw=true")
con <- url(url_d1)
on.exit(close(con))
scoreboard_d1 <- try(readRDS(con), silent = TRUE) %>%
distinct(game_id, game_date)
con <- url(url_d2)
on.exit(close(con))
scoreboard_d2 <- try(readRDS(con), silent = TRUE) %>%
distinct(game_id, game_date)
con <- url(url_d3)
on.exit(close(con))
scoreboard_d3 <- try(readRDS(con), silent = TRUE) %>%
distinct(game_id, game_date)
most_recent_d1 <- max(anydate(readRDS(url("https://github.com/sportsdataverse/softballR-data/raw/main/data/d1_hitting_box_scores_2024.RDS")) %>% pull(game_date)))
most_recent_d2 <- max(anydate(readRDS(url("https://github.com/sportsdataverse/softballR-data/raw/main/data/d2_hitting_box_scores_2024.RDS")) %>% pull(game_date)))
most_recent_d3 <- max(anydate(readRDS(url("https://github.com/sportsdataverse/softballR-data/raw/main/data/d3_hitting_box_scores_2024.RDS")) %>% pull(game_date)))
d1_hitting_box <- readRDS(url("https://github.com/sportsdataverse/softballR-data/raw/main/data/d1_hitting_box_scores_2024.RDS"))
d2_hitting_box <- readRDS(url("https://github.com/sportsdataverse/softballR-data/raw/main/data/d2_hitting_box_scores_2024.RDS"))
d3_hitting_box <- readRDS(url("https://github.com/sportsdataverse/softballR-data/raw/main/data/d3_hitting_box_scores_2024.RDS"))
scoreboard_d1$in_box <- scoreboard_d1$game_id %in% d1_hitting_box$game_id
scoreboard_d2$in_box <- scoreboard_d2$game_id %in% d2_hitting_box$game_id
scoreboard_d3$in_box <- scoreboard_d3$game_id %in% d3_hitting_box$game_id
game_ids_d1 <- scoreboard_d1 %>% filter(anydate(game_date) > most_recent_d1 | !in_box) %>% pull(game_id) %>% sort
game_ids_d2 <- scoreboard_d2 %>% filter(anydate(game_date) > most_recent_d2) %>% pull(game_id) %>% sort
game_ids_d3 <- scoreboard_d3 %>% filter(anydate(game_date) > most_recent_d3) %>% pull(game_id) %>% sort
get_ncaa_hitter_player_box <- function(game_id){
print(i)
i <<- i + 1
hitting <- try(get_hitting_box(game_id))
return(hitting)
}
get_ncaa_pitcher_player_box <- function(game_id){
print(i)
i <<- i + 1
pitching <- try(get_pitching_box(game_id))
return(pitching)
}
# Hitter box scores
i <- 0
prev_box <- d1_hitting_box
box <- do.call(rbind, lapply(X = game_ids_d1, FUN = get_ncaa_hitter_player_box))
if(!(is.null(box))){
box <- box %>%
filter(!str_detect(player,"Error : Document is empty|subscript out of bounds|Timeout was reached")) %>%
merge(scoreboard_d1, by = "game_id") %>%
mutate(season = 2024) %>%
select(-bb) %>%
rename(bb = bb_2) %>%
select(player, pos, g, rbi, ab, r, h, x2b, x3b, tb, hr, ibb, bb, hbp, sf, sh, k, kl, dp, gdp, tp, sb, cs, picked, go, fo, team, opponent, game_id, game_date, season) %>%
mutate(across(3:26, as.numeric))
saveRDS(object = rbind(prev_box, box), file = "data/d1_hitting_box_scores_2024.RDS")
}
i <- 0
prev_box <- d2_hitting_box
box <- do.call(rbind, lapply(X = game_ids_d2, FUN = get_ncaa_hitter_player_box))
if(!(is.null(box))){
box <- box %>%
filter(!str_detect(player,"Error : Document is empty|subscript out of bounds|Timeout was reached")) %>%
merge(scoreboard_d2, by = "game_id") %>%
mutate(season = 2024) %>%
select(-bb) %>%
rename(bb = bb_2) %>%
select(player, pos, g, rbi, ab, r, h, x2b, x3b, tb, hr, ibb, bb, hbp, sf, sh, k, kl, dp, gdp, tp, sb, cs, picked, go, fo, team, opponent, game_id, game_date, season) %>%
mutate(across(3:26, as.numeric))
saveRDS(object = rbind(prev_box, box), file = "data/d2_hitting_box_scores_2024.RDS")
}
i <- 0
box <- do.call(rbind, lapply(X = game_ids_d3, FUN = get_ncaa_hitter_player_box))
prev_box <- d3_hitting_box
if(!(is.null(box))){
box <- box %>%
filter(!str_detect(player,"Error : Document is empty|subscript out of bounds|Timeout was reached")) %>%
merge(scoreboard_d3, by = "game_id") %>%
mutate(season = 2024) %>%
select(player, pos, g, rbi, ab, r, h, x2b, x3b, tb, hr, ibb, bb, hbp, sf, sh, k, kl, dp, gdp, tp, sb, cs, picked, go, fo, team, opponent, game_id, game_date, season) %>%
mutate(across(3:26, as.numeric))
saveRDS(object = rbind(prev_box, box), file = "data/d3_hitting_box_scores_2024.RDS")
}
# Pitcher box scores
d1_pitching_box <- readRDS(url("https://github.com/sportsdataverse/softballR-data/raw/main/data/d1_pitching_box_scores_2024.RDS"))
d2_pitching_box <- readRDS(url("https://github.com/sportsdataverse/softballR-data/raw/main/data/d2_pitching_box_scores_2024.RDS"))
d3_pitching_box <- readRDS(url("https://github.com/sportsdataverse/softballR-data/raw/main/data/d3_pitching_box_scores_2024.RDS"))
scoreboard_d1$in_box <- scoreboard_d1$game_id %in% d1_pitching_box$game_id
scoreboard_d2$in_box <- scoreboard_d2$game_id %in% d2_pitching_box$game_id
scoreboard_d3$in_box <- scoreboard_d3$game_id %in% d3_pitching_box$game_id
game_ids_d1 <- scoreboard_d1 %>% filter(anydate(game_date) > most_recent_d1 | !in_box) %>% pull(game_id) %>% sort
game_ids_d2 <- scoreboard_d2 %>% filter(anydate(game_date) > most_recent_d2 | !in_box) %>% pull(game_id) %>% sort
game_ids_d3 <- scoreboard_d3 %>% filter(anydate(game_date) > most_recent_d3 | !in_box) %>% pull(game_id) %>% sort
i <- 0
prev_box <- d1_pitching_box
box <- do.call(rbind, lapply(X = game_ids_d1, FUN = get_ncaa_pitcher_player_box))
if(!(is.null(box))){
box <- box %>%
filter(!str_detect(player,"Error : Document is empty|subscript out of bounds|Timeout was reached")) %>%
merge(scoreboard_d1, by = "game_id") %>%
mutate(season = 2024) %>%
select(game_id, team, opponent, player, ip, ha, er, bb, hb, so, bf, hr_a, go, fo, season) %>%
mutate(across(5:14, as.numeric))
saveRDS(object = rbind(prev_box, box), file = "data/d1_pitching_box_scores_2024.RDS")
}
i <- 0
prev_box <- d1_pitching_box
box <- do.call(rbind, lapply(X = game_ids_d2, FUN = get_ncaa_pitcher_player_box))
if(!(is.null(box))){
box <- box %>%
filter(!str_detect(player,"Error : Document is empty|subscript out of bounds|Timeout was reached")) %>%
merge(scoreboard_d2, by = "game_id") %>%
mutate(season = 2024) %>%
select(game_id, team, opponent, player, ip, ha, er, bb, hb, so, bf, hr_a, go, fo, season) %>%
mutate(across(5:14, as.numeric))
saveRDS(object = rbind(prev_box, box), file = "data/d2_pitching_box_scores_2024.RDS")
}
i <- 0
prev_box <- d1_pitching_box
box <- do.call(rbind, lapply(X = game_ids_d3, FUN = get_ncaa_pitcher_player_box))
if(!(is.null(box))){
box <- box %>%
filter(!str_detect(player,"Error : Document is empty|subscript out of bounds|Timeout was reached")) %>%
merge(scoreboard_d3, by = "game_id") %>%
mutate(season = 2024) %>%
select(game_id, team, opponent, player, ip, ha, er, bb, hb, so, bf, hr_a, go, fo, season) %>%
mutate(across(5:14, as.numeric))
saveRDS(object = rbind(prev_box, box), file = "data/d3_pitching_box_scores_2024.RDS")
}