R/parse_betrivers_data.R

Defines functions parse_br_game_lines parse_br_prop parse_betrivers_data

parse_betrivers_data <- function(betrivers_data, sport, prop = FALSE, game_lines = FALSE, exclude_live = TRUE, exclude_alts = FALSE) {

  # loop through betrivers_data and extract the correct prop
  output_list <- list()
  for (e in names(betrivers_data)) {

    # subset the game event
    game_event <- betrivers_data[[e]]

    # nuke live games if specified, which is the default
    if (exclude_live) {
      if (game_event$state == "STARTED") next
    }

    # get the matchup and the tipoff
    matchup <- game_event$name
    tipoff <- game_event$start

    if (game_lines == TRUE) {
      gl_out <- parse_br_game_lines(game_event = game_event, matchup = matchup, tipoff = tipoff)
      output_list[[length(output_list) + 1]] <- gl_out
      next
    }

    # extract correct props

    # nba props ---------------------------------------------------------------
    if (prop %in% c('first team to score', 'ftts')) {
      output_list[[length(output_list) + 1]] <- parse_br_prop(game_event = game_event, category_name = 'Game', prop_name = "Next Team to Score - at Score 0-0", matchup = matchup, tipoff = tipoff)
    }
    if (prop %in% c('first player to score', 'fpts')) {
      output_list[[length(output_list) + 1]] <- parse_br_prop(game_event = game_event, category_name = 'First Field Goal Scorer', prop_name = 'Player to Score the First Field Goal of the Game', matchup = matchup, tipoff = tipoff)
    }

    # nfl props ---------------------------------------------------------------
    if (prop %in% c('player anytime td')) {
      output_list[[length(output_list) + 1]] <- parse_br_prop(game_event = game_event, category_name = 'Touchdown Scorer', prop_name = 'TD Scorer', matchup = matchup, tipoff = tipoff)
    }

  }

  # if output_list is empty, error, else return as a data.frame
  if (length(output_list) == 0 & game_lines == FALSE) stop('no betrivers ', prop, ' props returned')
  if (length(output_list) == 0 & game_lines == TRUE) stop('no betrivers game lines props returned')

  # stick it all together and return
  output_df <- dplyr::bind_rows(output_list)
  return(output_df)
}

parse_br_prop <- function(game_event, category_name, prop_name = NULL, prop_regex = NULL, matchup, tipoff) {

  offering_groups <- game_event$offeringGroups
  ge_cat_names <- unique(offering_groups$categoryName)
  if (!category_name %in% ge_cat_names) return()
  category_content <- offering_groups[offering_groups$categoryName == category_name, ]
  # extract attachments
  if (!'criterionGroups' %in% names(category_content)) return()
  criterion_groups <- as.data.frame(category_content$criterionGroups)
  criterion_names <- unique(criterion_groups$criterionName)
  if (!is.null(prop_name)) {
    if (!prop_name %in% criterion_names) return()
    bet_offers <- as.data.frame(criterion_groups[criterion_groups$criterionName == prop_name, ]$betOffers)
    outcome_df <- dplyr::bind_rows(bet_offers$outcomes)
    outcome_df$matchup <- matchup
    outcome_df$tipoff <- tipoff
    return(outcome_df)
  }
}

parse_br_game_lines <- function(game_event, exclude_alts, matchup, tipoff) {
  gl_outputs <- list()
  for (i in c('Moneyline', 'Total Points', 'Point Spread')) {
    df <- parse_br_prop(game_event, category_name = 'Most Popular', prop_name = i, matchup = matchup, tipoff = tipoff)
    if (length(df) == 0) next
    df$Type <- i
    gl_outputs[[i]] <- df
  }
  output_df <- dplyr::bind_rows(gl_outputs)
  return(output_df)
}
jimtheflash/betfinder documentation built on Dec. 6, 2023, 5:58 a.m.