#' get data from pb api
#'
#' @param sport chr
#' @param save_path chr
#' @param sleep_time num
#' @return list
#' @export
get_pointsbet_data <- function(sport, save_path = NULL, sleep_time = 0) {
# set the sport id value
if (sport == 'mlb') val <- 14
if (sport == 'nba') val <- 5
if (sport == 'nfl') val <- 2
# build the main query
main_uri <- paste0('https://api.il.pointsbet.com/api/v2/competitions/', val, '/events/featured')
main_query <- list(
'includeLive'='false',
'page'='1'
)
# GET THAT DATA
main_ret <- httr::GET(main_uri,
query=main_query)
main_content <- jsonlite::fromJSON(httr::content(main_ret, 'text', encoding = 'UTF-8'))
events <- main_content$events
event_ids <- unique(events$key)
# loop through the event_ids and get event-level (game-specific) jsons
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, and pull the right prop
event_URI <- paste0('https://api.il.pointsbet.com/api/v2/events/', e)
event_ret <- httr::GET(
event_URI
)
event_content <- jsonlite::fromJSON(httr::content(event_ret, 'text', encoding = 'UTF-8'))
event_list[[e]] <- event_content
}
# save if called for
if (!is.null(save_path)) {
fn <- paste0(sport, '_pointsbet_', 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(event_list)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.