#' Get NFL odds for a specific game from ESPN's API
#'
#' @param game_id character
#'
#' @return Returns a tibble
#' @export
#' @import tidyr dplyr
#' @importFrom dplyr %>%
#' @importFrom httr GET content stop_for_status
#' @importFrom glue glue
#' @examples
#' # Get odds from a specific game
#' get_nfl_odds(game_id = "400791550")
get_nfl_odds <- function(game_id) {
game_url <- glue::glue("http://site.api.espn.com/apis/site/v2/sports/football/nfl/summary")
raw_get <- httr::GET(game_url, query = list(event = game_id, enable = "ranks,odds,linescores,logos"))
httr::stop_for_status(raw_get)
raw_json <- httr::content(raw_get)
team_df <- raw_json[["boxscore"]][["teams"]] %>%
tibble(data = .) %>%
unnest_wider(data) %>%
unnest_wider(team) %>%
select(name:displayName, logo) %>%
mutate(location = c("away", "home")) %>%
rename(
team_name = name, team_full = displayName,
team_abb = abbreviation, team_logo = logo
)
odds_df <- raw_json[["pickcenter"]] %>%
tibble(data = .) %>%
unnest_wider(data) %>%
rename(over_under = overUnder, spread_team = details) %>%
hoist(
awayTeamOdds,
away_ml = "moneyLine",
away_odds = "spreadOdds"
) %>%
hoist(
homeTeamOdds,
home_ml = "moneyLine",
home_odds = "spreadOdds"
) %>%
hoist(
provider,
odds_source = "name",
odds_source_id = "id"
) %>%
select(!where(is.list)) %>%
pivot_longer(
cols = c(away_ml, away_odds, home_ml, home_odds),
names_to = "stat", values_to = "odds"
) %>%
separate(stat, into = c("location", "stat"), sep = "_") %>%
pivot_wider(names_from = stat, values_from = odds) %>%
left_join(
tibble(
location = c("home", "away"),
win_proj_fpi = c(
as.double(raw_json$predictor$homeTeam$gameProjection),
as.double(raw_json$predictor$awayTeam$gameProjection)
)
),
by = "location"
) %>%
left_join(team_df, by = "location") %>%
mutate(game_id = game_id, win_proj_fpi = as.double(win_proj_fpi))
odds_df
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.