R/parse_caesars_data.R

Defines functions parse_caesars_prop parse_caesars_data

parse_caesars_data <- function(caesars_data, sport, prop = FALSE, game_lines = FALSE) {

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

    # skip the tmnt bets for now
    if (caesars_data[[e]]$type == 'TMNT') next

    # subset the game event
    game_event <- caesars_data[[e]]
    matchup <- game_event$name
    tipoff <- game_event$startTime

    # TODO: GET GAME LINES BACK IN HERE
    # extract correct props
    # nba ---------------------------------------------------------------------
    if (prop %in% c('first shot points')) {
      output_list[[length(output_list) + 1]] <- parse_caesars_prop(game_event = game_event, prop_name = '|First Field Goal Type|', matchup = matchup, tipoff = tipoff)
    }
    if (prop %in% c('first team to score', 'ftts')) {
      output_list[[length(output_list) + 1]] <- parse_caesars_prop(game_event = game_event, prop_name = '|First Field Goal Team|', matchup = matchup, tipoff = tipoff)
    }
    if (prop %in% c('ftts shot points')) {
      output_list[[length(output_list) + 1]] <- parse_caesars_prop(game_event = game_event, prop_name = '|First Field Goal Exact|', matchup = matchup, tipoff = tipoff)
    }
    if (prop %in% c('first player to score', 'fpts')) {
      output_list[[length(output_list) + 1]] <- parse_caesars_prop(game_event = game_event, prop_name = '|First Scorer|', matchup = matchup, tipoff = tipoff)
    }
    if (prop %in% c('fpts by team')) {
      output_list[[length(output_list) + 1]] <- parse_caesars_prop(game_event = game_event, prop_regex = 'Field Goal Scorer|', prop_not_regex = '3pt', matchup = matchup, tipoff = tipoff)
    }
    if (prop %in% c('fpts shot points')) {
      output_list[[length(output_list) + 1]] <- parse_caesars_prop(game_event = game_event, prop_name = '|First Field Goal Scorer Exact|', matchup = matchup, tipoff = tipoff)
    }
    if (prop %in% c('fpts shot points by team')) {
      output_list[[length(output_list) + 1]] <- parse_caesars_prop(game_event = game_event, prop_regex = 'Field Goal Scorer Exact|', prop_not_regex = 'First Field', matchup = matchup, tipoff = tipoff)
    }

    # nfl ---------------------------------------------------------------------
    if (prop %in% c('player anytime td')) {
      output_list[[length(output_list) + 1]] <- parse_caesars_prop(game_event = game_event, prop_name = "|Player To Score a Touchdown|", 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 caesars ', prop, ' props returned')
  if (length(output_list) == 0 & game_lines == TRUE) stop('no caesars game lines props returned')
  output_df <- dplyr::bind_rows(output_list)

  return(output_df)
}

parse_caesars_prop <- function(game_event, prop_name = FALSE, prop_regex = FALSE, prop_not_regex = NULL, matchup, tipoff) {
  markets <- game_event$markets
  markets <- markets[markets$active==TRUE, ]
  if (length(markets) == 0) return()
  label_vec <- markets$name
  if (prop_name != FALSE & prop_name %in% label_vec) {
    target_market <- markets[markets$name == prop_name, ]
    selections <- as.data.frame(dplyr::bind_rows(target_market$selections))
    if (length(selections) == 0 | nrow(selections) == 0 | is.null(nrow(selections))) return()
    outcomes_df <- as.data.frame(selections)
  }
  else if (prop_regex != FALSE) {
    target_markets <- markets[grepl(prop_regex, markets$name, fixed = TRUE), ]
    target_markets <- target_markets[!grepl(prop_not_regex, target_markets$name), ]
    selections <- as.data.frame(dplyr::bind_rows(target_markets$selections))
    if (length(selections) == 0 | nrow(selections) == 0 | is.null(nrow(selections))) return()
    outcomes_df <- as.data.frame(selections)
  }

  # TODO: add logic for prop_regex


  if (!('outcomes_df' %in% ls())) return()
  if (length(outcomes_df) == 0) return()
  if (nrow(outcomes_df) == 0) return()
  outcomes_df$matchup <- matchup
  outcomes_df$tipoff <- tipoff
  return(outcomes_df)
}
jimtheflash/betfinder documentation built on Dec. 6, 2023, 5:58 a.m.