R/get_fanduel_data.R

Defines functions get_fanduel_data

Documented in get_fanduel_data

#' get data from fd api
#'
#' @param sport chr
#' @param save_path chr
#' @param get_tabs logical
#' @param mlb_tabs chr
#' @param nba_tabs chr
#' @param nfl_tabs chr
#' @param nhl_tabs chr
#' @param sleep_time num
#' @return list
#' @export
get_fanduel_data <- function(sport, save_path = NULL,
                             get_tabs = FALSE,
                             mlb_tabs = c(),
                             nba_tabs = c('quick-bets'),
                             nfl_tabs = c(),
                             nhl_tabs = c(),
                             sleep_time = c(12, 15)) {

  # set the main uri
  main_URI <- 'https://sbapi.il.sportsbook.fanduel.com/api/content-managed-page'
  # set the query; the sport is actually in here not the main URI
  qry <- list('betexRegion'= 'GBR',
              'capiJurisdiction'='intl',
              'currencyCode'='USD',
              'exchangeLocale'='en_US',
              'language'='en',
              'regionCode'='NAMERICA',
              '_ak'='FhMFpcPWXMeyZxOx',
              'page'='CUSTOM',
              'customPageId'=sport)
  hdrs <- c(
    'User-Agent'='Chrome',
    'Accept'='*/*'
  )

  # GET THAT 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'))

  output_list <- list(
    main=main_content
  )
  if (get_tabs == TRUE) {

    # extract event ids, in this case the names of the events
    events <- main_content$attachments$events
    event_ids <- unique(names(events))
    event_ids <- event_ids[grepl('^32', as.character(event_ids))]

    # loop through the event_ids and get event-level (game-specific) jsons
    event_list <- list()
    for (e in event_ids) {
      # print(paste0('getting event ', e, ' at ', Sys.time()))
      # sleep to be nice
      Sys.sleep(stats::runif(1, sleep_time[[1]], sleep_time[[2]]))
      # make the json string using the event_id, then grab the json, and pull the right prop
      event_URI <- 'https://sbapi.il.sportsbook.fanduel.com/api/event-page'
      event_query <- list('betexRegion'= 'GBR',
                          'capiJurisdiction'='intl',
                          'currencyCode'='USD',
                          'exchangeLocale'='en_US',
                          'includePrices'='true',
                          'language'='en',
                          'priceHistory'='1',
                          'regionCode'='NAMERICA',
                          '_ak'='FhMFpcPWXMeyZxOx',
                          'eventId'=e)

      ## BUT WAIT THERES MORE - gotta grab each of the tabs for the specific props and stuff
      if (sport == 'mlb') tabs <- mlb_tabs
      if (sport == 'nba') tabs <- nba_tabs
      if (sport == 'nfl') tabs <- nfl_tabs
      if (sport == 'nhl') tabs <- nhl_tabs

      tab_list <- list()
      for (i in tabs) {
        Sys.sleep(stats::runif(1, sleep_time[[1]], sleep_time[[2]]))
        event_query[['tab']] <- list(i)
        tab_ret <- httr::GET(event_URI, query = event_query, httr::add_headers(.headers = hdrs))
        tab_content <- jsonlite::fromJSON(httr::content(tab_ret, 'text', encoding = 'UTF-8'))
        tab_list[[i]] <- tab_content
      }
      ### add the tabs to the event list
      event_list[[e]] <- tab_list
    }
    output_list[['events']] <- event_list

  }

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

  # return as a list of lists (yikes!)
  return(output_list)

}
jimtheflash/betfinder documentation built on Dec. 6, 2023, 5:58 a.m.