R/tidyup_fanduel_data.R

Defines functions tidyup_fanduel_data

tidyup_fanduel_data <- function(fanduel_data, sport, prop = FALSE, game_lines = FALSE,
                                key = get_key_path(sport = sport, prop = prop, game_lines = game_lines)) {

  if (game_lines == TRUE) {

    # fix the totals first
    totals <- fanduel_data[fanduel_data$Type == 'Total Points', ]
    totals_df <- data.frame(
      matchup = totals$matchup,
      tipoff = totals$tipoff,
      tidytype = 'Total',
      tidyteam = 'game',
      tidyline = as.numeric(totals$handicap),
      tidyou = trimws(tolower(totals$runnerName)),
      tidyamericanodds = as.numeric(totals$winRunnerOdds$americanDisplayOdds$americanOdds)
    )
    # moneylines
    mls <- fanduel_data[fanduel_data$Type == 'Moneyline', ]
    mls_df <- data.frame(
      matchup = mls$matchup,
      tipoff = mls$tipoff,
      tidytype = 'Moneyline',
      tidyteam = normalize_names(mls$runnerName, key = key),
      tidyamericanodds = as.numeric(mls$winRunnerOdds$americanDisplayOdds$americanOdds)
    )
    # spreads
    spreads <- fanduel_data[fanduel_data$Type == 'Spread Betting', ]
    spreads_df <- data.frame(
      matchup = spreads$matchup,
      tipoff = spreads$tipoff,
      tidytype = 'Spread',
      tidyteam = normalize_names(spreads$runnerName, key = key),
      tidyline = as.numeric(spreads$handicap),
      tidyamericanodds = as.numeric(spreads$winRunnerOdds$americanDisplayOdds$americanOdds)
    )
    output_df <- dplyr::bind_rows(totals_df, mls_df, spreads_df)
    # standardize
    output_df$tidygame_part <- output_df$game_part
  } else {
    output_df <- data.frame(
      matchup = fanduel_data$matchup,
      tipoff = fanduel_data$tipoff
    )

    # for each prop, append whatever tidy fields we can, which should make thte data useful across datasets
    if (prop %in% c('first player to score', 'fpts')) {
      # generate tidy names and odds
      hacky_tidyplayer <- hacky_tidyup_player_names(fanduel_data$runners$runnerName)
      output_df$tidyplayer <- normalize_names(hacky_tidyplayer, key = key)
      output_df$tidyamericanodds <- as.numeric(fanduel_data$runners$winRunnerOdds$americanDisplayOdds$americanOdds)
      output_df$prop <- 'first player to score'
    }
    if (prop %in% c('fpts by team')) {
      # generate tidy names and odds
      hacky_tidyplayer <- hacky_tidyup_player_names(fanduel_data$runners$runnerName)
      output_df$tidyplayer <- normalize_names(hacky_tidyplayer, key = key)
      output_df$tidyamericanodds <- as.numeric(fanduel_data$runners$winRunnerOdds$americanDisplayOdds$americanOdds)
      output_df$prop <- 'first player to score by team'
    }
    if (prop %in% c('fpts shot type')) {
      # split the participant into player and exact shot type
      split_names <- strsplit(fanduel_data$runners$runnerName, ' - ')
      hacky_tidyplayer <- hacky_tidyup_player_names(unlist(lapply(split_names, '[[', 1)))
      output_df$tidyplayer <- normalize_names(hacky_tidyplayer, key = key)
      output_df$tidyshot_type <- unlist(lapply(split_names, '[[', 2))

      # get the odds and label the prop
      output_df$tidyamericanodds <- as.numeric(fanduel_data$runners$winRunnerOdds$americanDisplayOdds$americanOdds)
      output_df$prop <- 'first player to score exact shot'
    }
    if (prop %in% c('player any td', 'player first td')) {
      # generate tidy names and odds
      hacky_tidyplayer <- hacky_tidyup_player_names(output_df$participant)
      output_df$tidyplayer <- normalize_names(hacky_tidyplayer, key = key)
      output_df$tidyamericanodds <- as.numeric(output_df$american_odds)
      output_df$prop <- prop
    }
  }
  ## if tidyline isn't set, set it
  if (!'tidyline' %in% names(output_df)) {
    output_df$tidyline <- output_df$handicap
  }
  ## if the ou column doesn't exist, make it exist but NA_character
  if (!'tidyou' %in% names(output_df)) {
    output_df$tidyou <- NA_character_
  }
  if (!'tidyamericanodds' %in% names(output_df)) {
    output_df$tidyamericanodds <- output_df$american_odds
  }
  if (!'tidyexact_shot' %in% names(output_df)) {
    output_df$tidyexact_shot <- NA_character_
  }

  # tidyup the matchup! use the team abbreviations from the lookup
  output_df$matchup <- gsub("\\s*\\([^\\)]+\\)","",as.character(output_df$matchup))
  matchup_list <- strsplit(output_df$matchup, ' @ ')
  if (sport != 'nhl') {
    output_df$tidyawayteam <- normalize_names(unlist(lapply(matchup_list, '[[', 1)), key = get_key_path(sport, 'team'))
    output_df$tidyhometeam <- normalize_names(unlist(lapply(matchup_list, '[[', 2)), key = get_key_path(sport, 'team'))
  } else {
    output_df$tidyawayteam <- unlist(lapply(matchup_list, '[[', 1))
    output_df$tidyhometeam <- unlist(lapply(matchup_list, '[[', 2))
  }
  # tidyup the date! make sure this is EST
  output_df$tidygamedatetime <- lubridate::as_datetime(output_df$tipoff) - lubridate::hours(4)
  output_df$tidygamedatetime <- lubridate::round_date(output_df$tidygamedatetime, "30 minutes")
  lubridate::tz(output_df$tidygamedatetime) <- 'EST'

  # filter out the cols we don't need, i.e. not tidy ones
  names_to_keep <- names(output_df)[grepl('tidy|prop', names(output_df))]
  output_df <- output_df[, names(output_df) %in% names_to_keep]

  # stamp it up
  output_df$site <- 'fanduel'
  output_df$sport <- sport
  if (!'prop' %in% names(output_df)) {
    output_df$prop <- prop
  }

  # deliver
  return(output_df)
}
jimtheflash/betfinder documentation built on Dec. 6, 2023, 5:58 a.m.