R/get_caesars_data.R

Defines functions get_caesars_data

Documented in get_caesars_data

#' get data from csr api
#'
#' @param sport chr
#' @param save_path chr
#' @param sleep_time num
#' @return list
#' @export
get_caesars_data <- function(sport, save_path = NULL, sleep_time = 0) {

  # set the sport_id and competition_id
  if (sport == 'nba') {
    sport_id <- 'basketball'
    competition_id <- '5806c896-4eec-4de1-874f-afed93114b8c'
  }
  if (sport == 'nfl') {
    sport_id <- 'americanfootball'
    competition_id <- '007d7c61-07a7-4e18-bb40-15104b6eac92'
  }

  # build the main query
  main_URI <- 'https://api.americanwagering.com/regions/us/locations/il/brands/czr/sb/v3/events/highlights/'
  # set query params and headers
  qry <- list(
    'competitionId'=competition_id
  )
  hdrs <- c(
    'User-Agent'='Chrome',
    'Accept'='*/*'
  )
  # GET some data
  main_ret <- httr::GET(main_URI,
                        query=qry,
                        httr::add_headers(.headers = hdrs))
  main_content <- jsonlite::fromJSON(httr::content(main_ret, 'text', encoding = 'UTF-8'))

  # extract all the competitions from that main content
  all_competitions <- dplyr::bind_rows(main_content$competitions)
  # filter to the correct competition id, the grab the event ids
  selected_competitions <- all_competitions %>%
    dplyr::filter(id == competition_id)
  events_list <- as.list(selected_competitions$events)
  event_ids <- unique(unlist(lapply(events_list, function(x) x[['id']])))

  # loop through the event ids and save all the lists
  event_list <- list()
  for (e in event_ids) {
    # sleep to be nice
    Sys.sleep(sleep_time)
    # make the json string using the event_id, then grab the json
    event_URI <- paste0('https://api.americanwagering.com/regions/us/locations/il/brands/czr/sb/v3/events/', e)
    event_ret <- httr::GET(event_URI,
                           httr::add_headers(.headers = hdrs))
    event_content <- jsonlite::fromJSON(httr::content(event_ret, 'text', encoding = 'UTF-8'))
    event_list[[as.character(e)]] <- event_content
  }

  if (!is.null(save_path)) {
    fn <- paste0(sport, '_caesars_', e, '_', as.numeric(Sys.time()), '.json')
    jsonlite::write_json(event_list, file.path(save_path, fn))
    R.utils::gzip(file.path(save_path, fn), ext='gz')
  }

  # return as a list of lists (yikes!)
  return(event_list)
}
jimtheflash/betfinder documentation built on Dec. 6, 2023, 5:58 a.m.