-
Notifications
You must be signed in to change notification settings - Fork 8
/
cfbd_teams.R
637 lines (579 loc) · 22.1 KB
/
cfbd_teams.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
#' @name cfbd_teams
#' @aliases cfbd_team_info cfbd_team_matchup_records cfbd_team_matchup cfbd_team_roster cfbd_team_talent
#' @title CFBD Teams Endpoint
#' @description
#' \describe{
#' \item{`cfbd_team_info()`: Team Info Lookup}{.}
#' \item{`cfbd_team_matchup_records()`: Get matchup history records between two teams.}{.}
#' \item{`cfbd_team_matchup()`: Get matchup history between two teams.}{.}
#' \item{`cfbd_team_roster()`: Get a team's full roster by year.}{.}
#' \item{`cfbd_team_talent()`: Get composite team talent rankings for all teams in a given year}{.}
#' }
#' ## Team Info Lookup
#' Lists all teams in conference or all D-I teams if conference is left NULL
#' Currently, support is only provided for D-I
#' @param conference (\emph{String} optional): Conference abbreviation - Select a valid FBS conference\cr
#' Conference abbreviations P5: ACC, B12, B1G, SEC, PAC,\cr
#' Conference abbreviations G5 and FBS Independents: CUSA, MAC, MWC, Ind, SBC, AAC\cr
#' @param only_fbs (\emph{Logical} default TRUE): Filter for only returning FBS teams for a given year.\cr
#' If year is left blank while only_fbs is TRUE, then will return values for most current year
#' @param year (\emph{Integer} optional): Year, 4 digit format (\emph{YYYY}). Filter for getting a list of major division team for a given year
#' @param verbose Logical parameter (TRUE/FALSE, default: FALSE) to return warnings and messages from function
#' @return [cfbd_team_info()] - A data frame with 12 variables:
#' \describe{
#' \item{`team_id`: integer.}{Referencing team id.}
#' \item{`school`: character.}{Team name.}
#' \item{`mascot`: character.}{Team mascot.}
#' \item{`abbreviation`: character.}{Team abbreviations.}
#' \item{`alt_name1`: character.}{Team alternate name 1 (as it appears in `play_text`).}
#' \item{`alt_name2`: character.}{Team alternate name 2 (as it appears in `play_text`).}
#' \item{`alt_name3`: character.}{Team alternate name 3 (as it appears in `play_text`).}
#' \item{`conference`: character.}{Conference of team.}
#' \item{`division`: character.}{Division of team within the conference.}
#' \item{`color`: character.}{Team color (primary).}
#' \item{`alt_color`: character.}{Team color (alternate).}
#' \item{`logos`: character.}{Team logos.}
#' \item{`venue_id`: character.}{Referencing venue id.}
#' \item{`venue_name`: character.}{Stadium name.}
#' \item{`city`: character.}{Team/venue city.}
#' \item{`state`: character.}{Team/venue state.}
#' \item{`zip`: character.}{Team/venue zip code (someone double check Miami (FL) on if they're in the same zip code).}
#' \item{`country_code`: character.}{Team/venue country code.}
#' \item{`timezone`: character.}{Team/venue timezone.}
#' \item{`latitude`: character.}{Venue latitude.}
#' \item{`longitude`: character.}{Venue longitude.}
#' \item{`elevation`: character.}{Venue elevation.}
#' \item{`capacity`: character.}{Venue capacity.}
#' \item{`year_constructed`: character.}{Year the venue was constructed.}
#' \item{`grass`: character.}{TRUE/FALSE response on whether the field is grass or not (oh, and there are so many others).}
#' \item{`dome`: character.}{TRUE/FALSE flag for if the venue is a domed stadium.}
#' }
#' @source \url{https://api.collegefootballdata.com/teams}
#' @keywords Teams
#' @importFrom jsonlite fromJSON
#' @importFrom httr GET
#' @importFrom utils URLencode
#' @importFrom assertthat assert_that
#' @importFrom dplyr rename
#' @export
#' @examples
#' \donttest{
#' cfbd_team_info(conference = "SEC")
#'
#' cfbd_team_info(conference = "Ind")
#'
#' cfbd_team_info(year = 2019)
#' }
cfbd_team_info <- function(conference = NULL, only_fbs = TRUE, year = NULL,
verbose = FALSE) {
if (!is.null(conference)) {
# # Check conference parameter in conference abbreviations, if not NULL
# assertthat::assert_that(conference %in% cfbfastR::cfbd_conf_types_df$abbreviation,
# msg = "Incorrect conference abbreviation, potential misspelling.\nConference abbreviations P5: ACC, B12, B1G, SEC, PAC\nConference abbreviations G5 and Independents: CUSA, MAC, MWC, Ind, SBC, AAC")
# Encode conference parameter for URL, if not NULL
conference <- utils::URLencode(conference, reserved = TRUE)
base_url <- "https://api.collegefootballdata.com/teams?"
full_url <- paste0(
base_url,
"conference=", conference
)
# Check for CFBD API key
if (!has_cfbd_key()) stop("CollegeFootballData.com now requires an API key.", "\n See ?register_cfbd for details.", call. = FALSE)
# Create the GET request and set response as res
res <- httr::RETRY(
"GET", full_url,
httr::add_headers(Authorization = paste("Bearer", cfbd_key()))
)
# Check the result
check_status(res)
# Get the content and return it as data.frame
df <- res %>%
httr::content(as = "text", encoding = "UTF-8") %>%
jsonlite::fromJSON()
locs <- df$location
locs <- locs %>%
jsonlite::flatten()
df <- df %>% select(-.data$location)
# suppressWarnings(
# logos_list <- df %>%
# dplyr::group_by(.data$id) %>%
# tidyr::separate(.data$logos, c("logo_1","logo_2"), sep = ',') %>%
# dplyr::mutate(
# logo_1 = stringr::str_remove(.data$logo_1, "c\\("),
# logo_1 = ifelse(.data$logo_1 == 'NULL', NA_character_, .data$logo_1),
# logo_2 = stringr::str_remove(.data$logo_2,"\\)"),
# logo_2 = ifelse(.data$logo_2 == 'NULL', NA_character_, .data$logo_2),
# )
#
# )
df <- dplyr::bind_cols(df, locs) %>%
dplyr::rename(
team_id = .data$id,
venue_name = .data$name) %>%
as.data.frame()
return(df)
} else {
if (!is.null(year)) {
# Check if year is numeric, if not NULL
assertthat::assert_that(is.numeric(year) & nchar(year) == 4,
msg = "Enter valid year as a number (YYYY)"
)
}
base_url <- "https://api.collegefootballdata.com/teams/fbs?"
# if they want all fbs
full_url <- paste0(
base_url,
"year=", year
)
# Check for CFBD API key
if (!has_cfbd_key()) stop("CollegeFootballData.com now requires an API key.", "\n See ?register_cfbd for details.", call. = FALSE)
# Create the GET request and set response as res
res <- httr::RETRY(
"GET", full_url,
httr::add_headers(Authorization = paste("Bearer", cfbd_key()))
)
# Check the result
check_status(res)
# Get the content and return it as data.frame
df <- res %>%
httr::content(as = "text", encoding = "UTF-8") %>%
jsonlite::fromJSON()
locs <- df$location
locs <- locs %>%
jsonlite::flatten()
df <- df %>% select(-.data$location)
# suppressWarnings(
# logos_list <- df %>%
# dplyr::group_by(.data$id) %>%
# tidyr::separate(.data$logos, c("logo_1","logo_2"), sep = ',') %>%
# dplyr::mutate(
# logo_1 = stringr::str_remove(.data$logo_1, "c\\("),
# logo_1 = ifelse(.data$logo_1 == 'NULL', NA_character_, .data$logo_1),
# logo_2 = stringr::str_remove(.data$logo_2,"\\)"),
# logo_2 = ifelse(.data$logo_2 == 'NULL', NA_character_, .data$logo_2),
# )
#
# )
df <- dplyr::bind_cols(df, locs) %>%
dplyr::rename(
team_id = .data$id,
venue_name = .data$name) %>%
as.data.frame()
return(df)
}
}
#' @rdname cfbd_teams
#' @title Get matchup history records between two teams.
#' @param team1 (\emph{String} required): D-I Team 1
#' @param team2 (\emph{String} required): D-I Team 2
#' @param min_year (\emph{Integer} optional): Minimum of year range, 4 digit format (\emph{YYYY})
#' @param max_year (\emph{Integer} optional): Maximum of year range, 4 digit format (\emph{YYYY})
#' @param verbose Logical parameter (TRUE/FALSE, default: FALSE) to return warnings and messages from function
#'
#' @return [cfbd_team_matchup_records()] - A data frame with 7 variables:
#' \describe{
#' \item{`start_year`: character.}{Span starting year.}
#' \item{`end_year`: character.}{Span ending year.}
#' \item{`team1`: character.}{First team selected in query.}
#' \item{`team1_wins`: character.}{First team wins in series against `team2`.}
#' \item{`team2`: character.}{Second team selected in query.}
#' \item{`team2_wins`: character.}{Second team wins in series against `team1`.}
#' \item{`ties`: character.}{Number of ties in the series.}
#' }
#' @source \url{https://api.collegefootballdata.com/teams/matchup}
#' @keywords Team Matchup Records
#' @importFrom jsonlite fromJSON
#' @importFrom httr GET
#' @importFrom utils URLencode
#' @importFrom assertthat assert_that
#' @importFrom glue glue
#' @importFrom tibble enframe
#' @importFrom dplyr rename mutate select
#' @importFrom tidyr pivot_wider
#' @export
#' @examples
#' \donttest{
#' cfbd_team_matchup_records("Texas", "Oklahoma")
#'
#' cfbd_team_matchup_records("Texas A&M", "TCU", min_year = 1975)
#' }
#'
cfbd_team_matchup_records <- function(team1, team2, min_year = NULL, max_year = NULL,
verbose = FALSE) {
if (!is.null(min_year)) {
# Check if min_year is numeric, if not NULL
assertthat::assert_that(is.numeric(min_year) & nchar(min_year) == 4,
msg = "Enter valid min_year as a number (YYYY)"
)
}
if (!is.null(max_year)) {
# Check if max_year is numeric, if not NULL
assert_that(is.numeric(max_year) & nchar(max_year) == 4,
msg = "Enter valid max_year as a number (YYYY)"
)
}
if (!is.null(team1)) {
if (team1 == "San Jose State") {
team1 <- utils::URLencode(paste0("San Jos", "\u00e9", " State"), reserved = TRUE)
} else {
# Encode team1 parameter for URL if not NULL
team1 <- utils::URLencode(team1, reserved = TRUE)
}
}
if (!is.null(team1)) {
if (team2 == "San Jose State") {
team2 <- utils::URLencode(paste0("San Jos", "\u00e9", " State"), reserved = TRUE)
} else {
# Encode team2 parameter for URL if not NULL
team2 <- utils::URLencode(team2, reserved = TRUE)
}
}
base_url <- "https://api.collegefootballdata.com/teams/matchup?"
full_url <- paste0(
base_url,
"team1=", team1,
"&team2=", team2,
"&minYear=", min_year,
"&maxYear=", max_year
)
# Check for CFBD API key
if (!has_cfbd_key()) stop("CollegeFootballData.com now requires an API key.", "\n See ?register_cfbd for details.", call. = FALSE)
# Create the GET request and set response as res
res <- httr::RETRY(
"GET", full_url,
httr::add_headers(Authorization = paste("Bearer", cfbd_key()))
)
# Check the result
check_status(res)
df <- data.frame()
tryCatch(
expr = {
# Get the content and return it as data.frame
df <- res %>%
httr::content(as = "text", encoding = "UTF-8") %>%
jsonlite::fromJSON()
df1 <- tibble::enframe(unlist(df, use.names = TRUE))[1:7, ]
df <- tidyr::pivot_wider(df1,
names_from = .data$name,
values_from = .data$value
) %>%
dplyr::rename(
start_year = .data$startYear,
end_year = .data$endYear,
team1_wins = .data$team1Wins,
team2_wins = .data$team2Wins
) %>%
dplyr::select(
.data$start_year, .data$end_year,
.data$team1, .data$team1_wins,
.data$team2, .data$team2_wins,
.data$ties
)
df <- as.data.frame(df)
if(verbose){
message(glue::glue("{Sys.time()}: Scraping team matchup records..."))
}
},
error = function(e) {
message(glue::glue("{Sys.time()}:Invalid arguments or no team matchup records data available!"))
},
warning = function(w) {
},
finally = {
}
)
return(df)
}
#' @rdname cfbd_teams
#' @title Get matchup history between two teams.
#' @param team1 (\emph{String} required): D-I Team 1
#' @param team2 (\emph{String} required): D-I Team 2
#' @param min_year (\emph{Integer} optional): Minimum of year range, 4 digit format (\emph{YYYY})
#' @param max_year (\emph{Integer} optional): Maximum of year range, 4 digit format (\emph{YYYY})
#' @param verbose Logical parameter (TRUE/FALSE, default: FALSE) to return warnings and messages from function
#' @return [cfbd_team_matchup] - A data frame with 11 variables:
#' \describe{
#' \item{`season`: integer.}{Season the game took place.}
#' \item{`week`: integer.}{Game week of the season.}
#' \item{`season_type`: character.}{Season type of the game.}
#' \item{`date`: character.}{Game date.}
#' \item{`neutral_site`: logical.}{TRUE/FALSE flag for if the game took place at a neutral site.}
#' \item{`venue`: character.}{Stadium name.}
#' \item{`home_team`: character.}{Home team of the game.}
#' \item{`home_score`: integer.}{Home score in the game.}
#' \item{`away_team`: character.}{Away team of the game.}
#' \item{`away_score`: integer.}{Away score in the game.}
#' \item{`winner`: character.}{Winner of the matchup.}
#' }
#' @source \url{https://api.collegefootballdata.com/teams/matchup}
#' @keywords Team Matchup
#' @importFrom jsonlite fromJSON
#' @importFrom httr GET
#' @importFrom utils URLencode
#' @importFrom assertthat assert_that
#' @importFrom janitor clean_names
#' @importFrom glue glue
#' @export
#' @examples
#' \donttest{
#' cfbd_team_matchup("Texas", "Oklahoma")
#'
#' cfbd_team_matchup("Texas A&M", "TCU")
#'
#' cfbd_team_matchup("Texas A&M", "TCU", min_year = 1975)
#'
#' cfbd_team_matchup("Florida State", "Florida", min_year = 1975)
#' }
#'
cfbd_team_matchup <- function(team1, team2, min_year = NULL, max_year = NULL,
verbose = FALSE) {
if (!is.null(min_year)) {
# Check if min_year is numeric, if not NULL
assertthat::assert_that(is.numeric(min_year) & nchar(min_year) == 4,
msg = "Enter valid min_year as a number (YYYY)"
)
}
if (!is.null(max_year)) {
# Check if max_year is numeric, if not NULL
assertthat::assert_that(is.numeric(max_year) & nchar(max_year) == 4,
msg = "Enter valid max_year as a number (YYYY)"
)
}
if (!is.null(team1)) {
if (team1 == "San Jose State") {
team1 <- utils::URLencode(paste0("San Jos", "\u00e9", " State"), reserved = TRUE)
} else {
# Encode team1 parameter for URL if not NULL
team1 <- utils::URLencode(team1, reserved = TRUE)
}
}
if (!is.null(team1)) {
if (team2 == "San Jose State") {
team2 <- utils::URLencode(paste0("San Jos", "\u00e9", " State"), reserved = TRUE)
} else {
# Encode team2 parameter for URL if not NULL
team2 <- utils::URLencode(team2, reserved = TRUE)
}
}
base_url <- "https://api.collegefootballdata.com/teams/matchup?"
full_url <- paste0(
base_url,
"team1=", team1,
"&team2=", team2,
"&minYear=", min_year,
"&maxYear=", max_year
)
# Check for CFBD API key
if (!has_cfbd_key()) stop("CollegeFootballData.com now requires an API key.", "\n See ?register_cfbd for details.", call. = FALSE)
# Create the GET request and set response as res
res <- httr::RETRY(
"GET", full_url,
httr::add_headers(Authorization = paste("Bearer", cfbd_key()))
)
# Check the result
check_status(res)
df <- data.frame()
tryCatch(
expr = {
# Get the content and return it as data.frame
df <- res %>%
httr::content(as = "text", encoding = "UTF-8") %>%
jsonlite::fromJSON()$games
if (nrow(df) == 0) {
if(verbose){
warning("The data pulled from the API was empty.")
}
return(NULL)
}
df <- df %>%
janitor::clean_names() %>%
as.data.frame()
if(verbose){
message(glue::glue("{Sys.time()}: Scraping team matchup..."))
}
},
error = function(e) {
message(glue::glue("{Sys.time()}:Invalid arguments or no team matchup data available!"))
},
warning = function(w) {
},
finally = {
}
)
return(df)
}
#' @rdname cfbd_teams
#' @title Team Roster
#' Get a teams full roster by year. If team is not selected, API returns rosters for every team from the selected year.
#'
#' @param year (\emph{Integer} required): Year, 4 digit format (\emph{YYYY})
#' @param team (\emph{String} optional): Team, select a valid team in D-I football
#' @param verbose Logical parameter (TRUE/FALSE, default: FALSE) to return warnings and messages from function
#'
#' @return [cfbd_team_roster()] - A data frame with 12 variables:
#' \describe{
#' \item{`athlete_id`: character.}{Referencing athlete id.}
#' \item{`first_name`: character.}{Athlete first name.}
#' \item{`last_name`: character.}{Athlete last name.}
#' \item{`team`: character.}{Team name.}
#' \item{`weight`: integer.}{Athlete weight.}
#' \item{`height`: integer.}{Athlete height.}
#' \item{`jersey`: integer.}{Athlete jersey number.}
#' \item{`year`: integer.}{Athlete year.}
#' \item{`position`: character.}{Athlete position.}
#' \item{`home_city`: character.}{Hometown of the athlete.}
#' \item{`home_state`: character.}{Hometown state of the athlete.}
#' \item{`home_country`: character.}{Hometown country of the athlete.}
#' \item{`home_latitude`: numeric.}{Hometown latitude.}
#' \item{`home_longitude`: number.}{Hometown longitude.}
#' \item{`home_county_fips`: integer.}{Hometown FIPS code.}
#' }
#' @source \url{https://api.collegefootballdata.com/roster}
#' @keywords Team Roster
#' @importFrom dplyr rename mutate
#' @importFrom jsonlite fromJSON
#' @importFrom httr GET
#' @importFrom utils URLencode
#' @importFrom assertthat assert_that
#' @importFrom glue glue
#' @export
#' @examples
#' \donttest{
#' cfbd_team_roster(year = 2013, team = "Florida State")
#' }
#'
cfbd_team_roster <- function(year, team = NULL,
verbose = FALSE) {
team2 <- team
# check if year is numeric
assert_that(is.numeric(year) & nchar(year) == 4,
msg = "Enter valid year as a number (YYYY)"
)
if (!is.null(team)) {
if (team == "San Jose State") {
team <- utils::URLencode(paste0("San Jos", "\u00e9", " State"), reserved = TRUE)
} else {
# Encode team1 parameter for URL if not NULL
team <- utils::URLencode(team, reserved = TRUE)
}
}
base_url <- "https://api.collegefootballdata.com/roster?"
if (is.null(team)) {
full_url <- paste0(
base_url,
"year=", year
)
} else {
full_url <- paste0(
base_url, "team=", team,
"&year=", year
)
}
# Check for CFBD API key
if (!has_cfbd_key()) stop("CollegeFootballData.com now requires an API key.", "\n See ?register_cfbd for details.", call. = FALSE)
# Create the GET request and set response as res
res <- httr::RETRY(
"GET", full_url,
httr::add_headers(Authorization = paste("Bearer", cfbd_key()))
)
# Check the result
check_status(res)
df <- data.frame()
tryCatch(
expr = {
# Get the content and return it as data.frame
df <- res %>%
httr::content(as = "text", encoding = "UTF-8") %>%
jsonlite::fromJSON() %>%
dplyr::rename(athlete_id = .data$id) %>%
# Is this okay to just comment out?
# Changing to team = NULL deleted the column
# dplyr::mutate(team = team2) %>%
as.data.frame()
if(verbose){
message(glue::glue("{Sys.time()}: Scraping team roster..."))
}
},
error = function(e) {
message(glue::glue("{Sys.time()}:Invalid arguments or no team roster data available!"))
},
warning = function(w) {
},
finally = {
}
)
return(df)
}
#' Get composite team talent rankings for all teams in a given year
#' Extracts team talent composite as sourced from 247 rankings
#' @rdname cfbd_teams
#'
#' @param year (\emph{Integer} optional): Year 4 digit format (\emph{YYYY})
#' @param verbose Logical parameter (TRUE/FALSE, default: FALSE) to return warnings and messages from function
#'
#' @return [cfbd_team_talent()] - A data frame with 3 variables:
#' \describe{
#' \item{`year`: integer.}{Season for the talent rating.}
#' \item{`school`: character.}{Team name.}
#' \item{`talent`: double.}{Overall roster talent points (as determined by 247Sports).}
#' }
#' @source \url{https://api.collegefootballdata.com/talent}
#' @keywords Team talent
#' @importFrom jsonlite fromJSON
#' @importFrom httr GET
#' @importFrom utils URLencode
#' @importFrom assertthat assert_that
#' @importFrom glue glue
#' @export
#' @examples
#' \donttest{
#' cfbd_team_talent()
#'
#' cfbd_team_talent(year = 2018)
#' }
#'
cfbd_team_talent <- function(year = NULL,
verbose = FALSE) {
if (!is.null(year)) {
## check if year is numeric
assert_that(is.numeric(year) & nchar(year) == 4,
msg = "Enter valid year as a number (YYYY)"
)
}
base_url <- "https://api.collegefootballdata.com/talent?"
full_url <- paste0(
base_url,
"year=", year
)
# Check for CFBD API key
if (!has_cfbd_key()) stop("CollegeFootballData.com now requires an API key.", "\n See ?register_cfbd for details.", call. = FALSE)
# Create the GET request and set response as res
res <- httr::RETRY(
"GET", full_url,
httr::add_headers(Authorization = paste("Bearer", cfbd_key()))
)
# Check the result
check_status(res)
df <- data.frame()
tryCatch(
expr = {
# Get the content and return it as data.frame
df <- res %>%
httr::content(as = "text", encoding = "UTF-8") %>%
jsonlite::fromJSON() %>%
as.data.frame() %>%
mutate(talent = as.numeric(.data$talent))
if(verbose){
message(glue::glue("{Sys.time()}: Scraping team talent..."))
}
},
error = function(e) {
message(glue::glue("{Sys.time()}:Invalid arguments or no team talent data available!"))
},
warning = function(w) {
},
finally = {
}
)
return(df)
}