#' Get dates of fantasy weeks from Yahoo! Fantasy API.
#'
#' Want to see start and end dates for fantasy weeks of a particular league? Curious how long
#' long a fantasy week is? If yes, this is your function.
#'
#' @param game_key A game key or a vector of game keys. Can be found with y_games().
#' @param token_name Name used for assignment when creating token object with y_create_token().
#' @param debug Returns a list of data such as uri call and content. Useful for debugging.
#' @param quiet Print function activity.
#'
#' @return A tibble
#' @export
y_weeks <- memoise::memoise(
function(game_key = NULL, token_name = NULL, debug = FALSE, quiet = TRUE) {
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
## TOKEN ----
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
api_token <- token_name
.token_check(token_name, api_token, name = .GlobalEnv)
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
## ARGUMENTS ----
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
resource <- "games"
subresource <- "game_weeks"
uri_out <- "game_keys="
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
## CHECKS ----
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
stopifnot(!is.null(game_key))
stopifnot(!is.null(api_token))
# Check if keys are type league, remove FALSE and duplicates.
key <- .single_resource_key_check(game_key, .game_key_check)
if(!quiet){cat(crayon::cyan("Resource is", resource, "\n"), sep = "")}
if(!quiet){cat(crayon::cyan("Keys are...\n", stringr::str_flatten(key, collapse = "\n")), sep = "\n")}
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
## URI ----
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Initial uri components
uri_parsed <- structure(
list(
scheme = "https",
hostname = "fantasysports.yahooapis.com/fantasy/v2",
port = NULL,
path = resource,
query = list(format = "json"),
params = NULL
),
class = "url"
)
key_path <-
.uri_path_packer(key, 25)
uri_parsed$params <-
stringr::str_c(uri_out, key_path, "/", subresource, sep = "")
uri <- httr::build_url(uri_parsed)
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
## GET RESPONSE ----
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
r <-
purrr::map(uri, .y_get_response, api_token)
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
## CHECK RESPONSE FOR ERRORS ----
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if(sum(!purrr::map_lgl(r, httr::http_error)) <= 0){
stop(message(crayon::cyan("All requests returned errors. You may need a token refresh.")), call. = FALSE)
}
r <- r[!purrr::map_lgl(r, httr::http_error)]
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
## CONTENT ----
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
r_parsed <-
purrr::map(r, .y_parse_response, "fantasy_content", resource)
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
## PARSE CONTENT ----
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
## DF ----
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if(!debug){
# General list pre-processing
preprocess <-
r_parsed %>%
purrr::flatten() %>%
list_pre_process_fn()
# df
df <-
tryCatch(
expr =
preprocess %>%
purrr::map_df(
.game_resource_parse_fn,
pluck_args = list("game", 2, 1),
fn = function(x) purrr::map_df(x, purrr::flatten_df)
) %>%
dplyr::mutate(matchup_length = difftime(end, start)),
error = function(e) {
message(
crayon::cyan(
"Function failed while parsing games resource with .game_resource_parse_fn. Returning debug list."
)
)
}
)
if(tibble::is_tibble(df)) {return(df)}
}
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
## DEBUG RETURN ----
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
data_list <-
structure(
list(
uri = uri,
resource = resource,
response = r,
r_parsed = r_parsed
),
class = "yahoo_fantasy_api")
return(data_list)
}
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.