Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expanded shift data #26

Open
hswerdfe opened this issue Jun 29, 2022 · 1 comment
Open

expanded shift data #26

hswerdfe opened this issue Jun 29, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@hswerdfe
Copy link

The function nhl_game_shifts aggregates the data from https://api.nhle.com/stats/rest/en/shiftcharts?cayenneExp=gameId= via a group_by function. This makes it harder to determine who is on the ice during any given event, in the play by play data as the player_id column is not maintained.
I suggest making a new standalone function that returns the detailed game shift data nhl_game_shifts_detailed, this would make it easier to determine which players are on the ice for a given event.

@hswerdfe
Copy link
Author

something like this might fit with your code, and allow a users to find the player on the ice during any event.

nhl_game_shifts_by_player <- function (game_id) 
{
  base_url <- "https://api.nhle.com/stats/rest/en/shiftcharts?cayenneExp=gameId="
  full_url <- paste0(base_url, game_id)
  res <- httr::RETRY("GET", full_url)
  fastRhockey:::check_status(res)
  tryCatch(expr = {
    res %>% 
      httr::content(as = "text", encoding = "UTF-8") |> 
      jsonlite::fromJSON() |>
      magrittr::extract2('data') |>
      dplyr::tibble() |> janitor::clean_names() |>
      tidyr::unite("player_name", c(.data$first_name, 
                                    .data$last_name), sep = " ") |> 
      dplyr::select(.data$game_id, 
                    .data$player_id, .data$player_name, .data$team_abbrev, 
                    .data$team_id, .data$team_name, .data$period, .data$start_time, 
                    .data$end_time, .data$duration) %>% dplyr::filter(!is.na(.data$duration)) |> 
      dplyr::mutate(start_time_ms = lubridate::ms(.data$start_time), 
                    start_seconds = lubridate::period_to_seconds(.data$start_time_ms), 
                    start_game_seconds = .data$start_seconds + (1200 * (.data$period - 1)), end_time_ms = lubridate::ms(.data$end_time), 
                    end_seconds = lubridate::period_to_seconds(.data$end_time_ms), 
                    end_game_seconds = .data$end_seconds + (1200 * (.data$period - 1)), 
                    duration = lubridate::ms(.data$duration), 
                    duration_seconds = lubridate::period_to_seconds(.data$duration)) |>
      fastRhockey:::make_fastRhockey_data("detailed NHL Game Shifts Information from NHL.com", Sys.time())
  }, error = function(e) {
    message(glue::glue("{Sys.time()}: shift by player data for {game_id} generated an error, {e}."))
  }, warning = function(w) {
  }, finally = {
  })
}

@armstjc armstjc added the enhancement New feature or request label Jun 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants