/
mlb_game_content.R
66 lines (63 loc) · 2.02 KB
/
mlb_game_content.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
#' @rdname mlb_game_content
#' @title **Retrieve additional game content for major and minor league games**
#' @param game_pk The unique game_pk identifier for the game
#' @return Returns a tibble of game content data with the following columns:
#'
#' |col_name |types |
#' |:-------------------|:---------|
#' |title |character |
#' |epg_id |integer |
#' |content_id |character |
#' |media_id |character |
#' |media_state |character |
#' |media_feed_type |character |
#' |media_feed_sub_type |character |
#' |call_letters |character |
#' |fox_auth_required |logical |
#' |tbs_auth_required |logical |
#' |espn_auth_required |logical |
#' |fs1auth_required |logical |
#' |mlbn_auth_required |logical |
#' |free_game |logical |
#' |type |character |
#' |description |character |
#' |rendition_name |character |
#' |language |character |
#'
#' @importFrom jsonlite fromJSON
#' @importFrom janitor clean_names
#' @importFrom dplyr rename
#' @importFrom glue glue
#' @importFrom rlang .data
#' @importFrom tidyr unnest
#' @importFrom tibble tibble
#' @import rvest
#' @export
#' @examples \donttest{
#' try(mlb_game_content(game_pk = 566001))
#' }
mlb_game_content <- function(game_pk) {
mlb_endpoint <- mlb_stats_endpoint(glue::glue("v1/game/{game_pk}/content"))
query_params <- list()
mlb_endpoint <- httr::modify_url(mlb_endpoint, query = query_params)
tryCatch(
expr = {
resp <- mlb_endpoint %>%
mlb_api_call()
media <- resp$media
epg <- media$epg %>%
tidyr::unnest("items") %>%
as.data.frame() %>%
janitor::clean_names() %>%
dplyr::rename(
"epg_id" = "id") %>%
make_baseballr_data("MLB Game Content data from MLB.com",Sys.time())
},
error = function(e) {
message(glue::glue("{Sys.time()}: Invalid arguments provided"))
},
finally = {
}
)
return(epg)
}