R/get_mgm_data.R

Defines functions get_mgm_data

Documented in get_mgm_data

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

  # set the main_URI and headers, and then update the qry by sport
  main_URI <- 'https://sports.il.betmgm.com/en/sports/api/widget/widgetdata'
  hdrs <- c(
    'User-Agent'='Chrome',
    'Accept'='*/*'
  )
  qry <- list(
    'layoutSize'='Small',
    'page'='CompetitionLobby',
    'regionId'='9',
    'widgetId'='/mobilesports-v1.0/layout/layout_us/modules/competition/matches',
    'shouldIncludePayload'='true'
  )
  if (sport == 'mlb') {
    qry['sportId'] <- '23'
    qry['competitionId'] <- '75'
  }
  if (sport == 'nba') {
    qry['sportId'] <- '7'
    qry['competitionId'] <- '6004'
  }
  if (sport == 'ncaaf') {
    qry['sportId'] <- '11'
    qry['competitionId'] <- '211'
  }
  if (sport == 'nfl') {
    qry['sportId'] <- '11'
    qry['competitionId'] <- '35'
  }

  # get the json from the widget hehe, then extract the fixture ids
  ret <- httr::GET(main_URI,
                   query = qry,
                   httr::add_headers(.headers = hdrs))
  parsed_ret <- jsonlite::fromJSON(httr::content(ret, 'text'))
  fixtures <- jsonlite::flatten(parsed_ret$widgets$payload$fixtures[[1]])
  fixture_ids <- unique(fixtures$id)

  # make the event query
  event_URI <- 'https://sports.il.betmgm.com/cds-api/bettingoffer/fixture-view'
  event_qry <- list(
    'x-bwin-accessid'='ZTg4YWEwMTgtZTlhYy00MWRkLWIzYWYtZjMzODI5ZDE0Mjc5',
    'lang'='en-us',
    'country'='US',
    'userCountry'='US',
    'subdivision'='US-Illinois',
    'offerMapping'='All',
    'scoreboardMode'='Full',
    'state'='Latest',
    'includePrecreatedBetBuilder'='true',
    'supportVirtual'='false',
    'useRegionalisedConfiguration'='true',
    'includeRelatedFixtures'='true',
    'statisticsModes'='All'
  )

  # loop through the fixture_ids and get the events
  event_list <- list()
  for (id in fixture_ids) {
    event_qry['fixtureIds'] <- id
    event_ret <- httr::GET(
      event_URI,
      query = event_qry,
      httr::add_headers(.headers = hdrs)
    )
    parsed_event_ret <- jsonlite::fromJSON(httr::content(event_ret, 'text'))
    event_list[[id]] <- parsed_event_ret
  }

  if (!is.null(save_path)) {
    fn <- paste0(sport, '_mgm_', 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 the whole dang list of lists
  return(event_list)
}
jimtheflash/betfinder documentation built on Dec. 6, 2023, 5:58 a.m.