R/get_odds.R

Defines functions get_odds

Documented in get_odds

#' Get NBA Gambling Odds from draftkings.com
#'
#' This Function web scrapes draftkings.com to return moneyline & spread odds for the next 24 hrs of NBA Games.
#' @return
#' @export
#'
#' @importFrom rvest read_html html_nodes html_table
#' @importFrom readr read_csv
#' @importFrom purrr pluck
#' @importFrom dplyr select filter mutate group_by ungroup left_join cur_group_id row_number
#' @importFrom tidyr separate
#' @importFrom stringr str_extract str_sub
#' @importFrom utils head
#' @import magrittr
#' @examples
#' my_gambling_odds <- get_odds()
#'
get_odds <- function(){
  oddsURL <- paste0("https://sportsbook.draftkings.com/leagues/basketball/103?category=game-lines&subcategory=game")
  oddspage <- read_html(oddsURL)

  dk <- read_csv('data/draftkings_acc.csv')

  bb9 <- oddspage %>%
    html_nodes(xpath = '//*[@id="root"]/section/section[2]/section/div[3]/div/div[3]/div/div/div[2]/div/div[2]') %>%
    html_table() %>%
    pluck(1)

  df_clean <- bb9 %>%
    select(team = 1, spread = `POINT SPREAD`, tot_pts = `TOTAL POINTS`, moneyline = MONEYLINE) %>%
    filter(!team == 'Tomorrow',
           !spread == 'POINT SPREAD') %>%
    head(4) %>%
    separate(team, c('a', 'b', 'c', 'd'), sep = ' ') %>%
    mutate(time = str_extract(a, "^.{4}")) %>%
    group_by(time) %>%
    mutate(game_id = cur_group_id(),
           process_id = row_number()) %>%
    ungroup() %>%
    filter(!process_id > 2) %>%
    select(team = b, spread:process_id) %>%
    left_join(dk) %>%
    group_by(game_id) %>%
    mutate(opp = rev(team_acc)) %>%
    ungroup() %>%
    separate(tot_pts, c('overunder', 'team_pts', 'c', 'd'), sep = '-') %>%
    mutate(overunder = str_extract(overunder, "^.{1}")) %>%
    mutate(overunder = replace(overunder, overunder == 'O', 'over'),
           overunder = replace(overunder, overunder == 'U', 'under'),
           spread = str_sub(spread, 1, nchar(spread) - 4),
           date = Sys.Date(),
           date = as.character(date)) %>%
    select(-c, -d, -time, -team, team = full_name, team_acc, opp, moneyline, spread, overunder, team_pts, game_id, date) %>%
    select(team, team_acc, opp, moneyline, spread, overunder, team_pts, game_id, date)

  print(paste0('Finished Odds Function at ', Sys.time(), ' with ', nrow(df_clean), ' Rows'))
  return(df_clean)

}
jyablonski/practice_package documentation built on Dec. 21, 2021, 5:14 a.m.