#################### Teams API ####################
get_nhl_api_teams <- function(team_ids = NULL){
raw_teams <- NULL
iattempts <- 5
if(is.null(team_ids)){
teams_url <-
paste0("https://statsapi.web.nhl.com/api/v1/teams")
} else {
teams_url <-
paste0("https://statsapi.web.nhl.com/api/v1/teams/?teamId=", paste(team_ids, collapse = ","))
}
while(! is.character(raw_teams) & iattempts > 0){
raw_teams <-
tryCatch(
jsonlite::fromJSON(teams_url),
error = function(e) {NULL}
)
iattempts <- iattempts - 1
}
if(length(raw_teams) == 0){
warning("No Team Data Returned")
return(NULL)
} else {
flat_teams <-
raw_teams %>%
tibble::as_tibble() %>%
jsonlite::flatten() %>%
dplyr::select(
## Team Details
team_id = teams.id,
is_active = teams.active,
full_team_name = teams.name,
team_name = teams.teamName,
team_abbreviation = teams.abbreviation,
team_short_name = teams.shortName,
location = teams.locationName,
website_link = teams.officialSiteUrl,
api_link = teams.link,
## Venue Details
venue_id = teams.venue.id,
venue_name = teams.venue.name,
venue_city = teams.venue.city,
venue_timezone = teams.venue.timeZone.id,
venue_timezone_abbreviation = teams.venue.timeZone.tz,
venue_timezone_offset = teams.venue.timeZone.offset,
venue_api = teams.venue.link,
## Division Details
division_id = teams.division.id,
division_name = teams.division.name,
division_short_name = teams.division.nameShort,
division_abbreviation = teams.division.abbreviation,
division_api = teams.division.link,
## Conference Details
conference_id = teams.conference.id,
conference_name = teams.conference.name,
conference_api = teams.conference.link,
## Franchise Details
franchise_id = teams.franchise.franchiseId,
franchise_name = teams.franchise.teamName,
franchise_api = teams.franchise.link
)
return(flat_teams)
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.