R/tidyup_caesars_data.R

Defines functions tidyup_caesars_data

tidyup_caesars_data <- function(caesars_data, sport, prop = FALSE, game_lines = FALSE,
                            key = get_key_path(sport = sport, prop = prop, game_lines = game_lines)) {

  if (nrow(caesars_data) < 1) stop('no caesars ', prop, ' available')

  # make the output from the input
  output_df <- caesars_data

  # game_lines
  if (game_lines == TRUE) {
    # # fix the totals first
    # totals <- output_df[output_df$Type == 'Total Points', ]
    # new_totals_list <- list()
    # for (m in unique(totals$matchup)) {
    #   mu <- totals[totals$matchup == m, ]
    #   teams <- unlist(strsplit(m, ' @ '))
    #   mu$participantName[[1]] <- teams[[1]]
    #   mu$participantName[[2]] <- teams[[2]]
    #   new_totals_list[[length(new_totals_list) + 1]] <- mu
    # }
    # new_totals <- dplyr::bind_rows(new_totals_list)
    # output_df <- dplyr::bind_rows(new_totals, output_df[output_df$Type != 'Total Points', ])
    # output_df$tidyteam <- normalize_names(as.character(output_df$participantName), key = key)
    # output_df$tidyplayer <- 'team'
    # output_df$tidytype <- gsub(' Points|Point ', '', as.character(output_df$Type))
    # output_df$tidyline <- as.numeric(output_df$line)
    # output_df$tidyou <- ifelse(output_df$tidytype == 'Total', tolower(output_df$label), NA_character_)
    # output_df$tidyamericanodds <- as.numeric(output_df$oddsAmerican)
  }


  # for each prop, append tidy team, tidy opponent, tidy odds (numeric american odds)
  if (prop %in% c('first shot points')) {
    # # generate tidy names and odds
    output_df$tidyteam <- 'game'
    output_df$tidyplayer <- 'game'
    output_df$tidyamericanodds <- as.numeric(output_df$price$a)
    output_df$tidyshot_points <- as.numeric(ifelse(grepl('2', output_df$name), 2, 3))
  }
  if (prop %in% c('first team to score', 'ftts')) {
    # # generate tidy names and odds
    output_df$tidyteam <- normalize_names(output_df$teamData$teamShortName, key = key)
    output_df$tidyplayer <- 'team'
    output_df$tidyamericanodds <- as.numeric(output_df$price$a)
    output_df$quarter <- '1Q'
    # # since prop arg is flexible, set it here for output
    output_df$prop <- 'first team to score'
  }
  if (prop %in% c('ftts shot points')) {
    # # generate tidy names and odds
    split_name <- strsplit(output_df$name, 'Made')
    team <- gsub('|', '', trimws(unlist(lapply(split_name, '[[', 1))), fixed = TRUE)
    output_df$tidyteam <- normalize_names(team, key = key)
    output_df$tidyplayer <- 'team'
    output_df$tidyamericanodds <- as.numeric(output_df$price$a)
    # extract the points now
    points <- as.numeric(gsub('|', '', trimws(unlist(lapply(split_name, '[[', 2))), fixed = TRUE))
    output_df$tidyshot_points <- points

  }
  if (prop %in% c('first player to score', 'fpts')) {
    hacky_player_names <- hacky_tidyup_player_names(output_df$name)
    output_df$tidyplayer <- normalize_names(hacky_player_names, key = key)
    output_df$tidyamericanodds <- as.numeric(output_df$price$a)
    output_df$prop <- 'first player to score'
  }
  if (prop %in% c('fpts shot points', 'fpts shot points by team')) {

    split_name <- strsplit(output_df$name, ' - ')
    hacky_player_names <- hacky_tidyup_player_names(gsub('|', '', trimws(unlist(lapply(split_name, '[[', 1))), fixed = TRUE))
    output_df$tidyplayer <- normalize_names(hacky_player_names, key = key)
    output_df$tidyamericanodds <- as.numeric(output_df$price$a)
    points <- as.numeric(substring(unlist(lapply(split_name, '[[', 2)), 1, 1))
    output_df$tidyshot_points <- points
    output_df$prop <- 'fpts shot points'
  }
  if (prop %in% c('fpts by team')) {
    hacky_player_names <- hacky_tidyup_player_names(output_df$name)
    output_df$tidyplayer <- normalize_names(hacky_player_names, key = key)
    output_df$tidyamericanodds <- as.numeric(output_df$price$a)
    output_df$prop <- 'fpts by team'
  }


  # if (prop %in% c('fpts by team')) {
  #   hacky_player_names <- hacky_tidyup_player_names(output_df$name.value)
  #   output_df$tidyplayer <- normalize_names(hacky_player_names, key = key)
  #   output_df$tidyamericanodds <- as.numeric(output_df$americanOdds)
  #   output_df$prop <- 'first player to score by team'
  # }
  # if (prop %in% c('fpts exact method')) {
  #   output_df$tidyplayer <- normalize_names(output_df$name.value, key = key)
  #   output_df$tidyamericanodds <- as.numeric(output_df$americanOdds)
  #   output_df$prop <- 'first player to score exact method'
  # }
  #
  # if (prop %in% c('player first td', 'player any td')) {
  #   hacky_tidyplayer <- hacky_tidyup_player_names(as.character(output_df$name))
  #   output_df$tidyplayer <- normalize_names(hacky_tidyplayer, key = key)
  #   output_df$tidyamericanodds <- ifelse(as.numeric(output_df$price) - 1 < 1,
  #                                        -100 / (as.numeric(output_df$price) - 1),
  #                                        (as.numeric(output_df$price) - 1) * 100)
  #   # since prop arg is flexible, set it here for output
  #   output_df$prop <- prop
  # }
  #
  # if (grepl('alt$| ou$|tiers$|points|rebounds|assists|three| 3pts| pts| rebs| asts|hit|double|pass|rush|rec', tolower(prop))) {
  #   # handle special cases by prop type
  #   ## alt lines can be over or under, but need to extract direction and line from names
  #   if (grepl('alt$', tolower(prop))) {
  #     ## set the over/under column value, which is always an over
  #     output_df$tidyou <- 'over'
  #     ## get the name AND line out of the name; split everything first to make this easier
  #     split_string <- gsub(' To Get | To Make | To Record ', 'XX', as.character(output_df$name))
  #     splitted <- strsplit(split_string, 'XX')
  #     splitted_name <- sapply(splitted, '[[', 1)
  #     splitted_name <- hacky_tidyup_player_names(splitted_name)
  #     splitted_line <- gsub('[A-Za-z |\\+]', '', sapply(splitted, '[[', 2))
  #     output_df$tidyplayer <- normalize_names(splitted_name, key = key)
  #     # the lines here are for "N+ made 3s" so adjust for that here by subtracting half a point
  #     output_df$tidyline <- as.numeric(splitted_line) - .5
  #   }
  #   if (grepl('ou$', tolower(prop))) {
  #     ## set the over/under column values
  #     output_df$tidyou <- ifelse(grepl('Over', as.character(output_df$name)), 'over', 'under')
  #     ## get the name AND line out of the name; split everything first to make this easier
  #     split_string <- gsub(' Over | Under | over | under ', 'XX', as.character(output_df$name))
  #     splitted <- strsplit(split_string, 'XX')
  #
  #     splitted_name <- sapply(splitted, '[[', 1)
  #     splitted_name <- hacky_tidyup_player_names(splitted_name)
  #     output_df$tidyplayer <- normalize_names(splitted_name, key = key)
  #
  #     splitted_line <- sapply(splitted, '[[', 2)
  #     splitted_line <- gsub('[A-Za-z| |+]', '', splitted_line)
  #     output_df$tidyline <- as.numeric(splitted_line)
  #   }
  #   ## tiers are always overs, but the lines are in the prop_details, not the handicap
  #   if (grepl('tiers|double', tolower(prop))) {
  #     output_df$tidyou <- 'over'
  #     ## get the name AND line out of the name; split everything first to make this easier
  #     split_string <- gsub(' To Make | To Get | To Get [Aa]', 'XX', output_df$name)
  #     splitted <- strsplit(split_string, 'XX')
  #     splitted_name <- sapply(splitted, '[[', 1)
  #     splitted_name <- hacky_tidyup_player_names(splitted_name)
  #     splitted_line <- sapply(splitted, '[[', 2)
  #     output_df$tidyplayer <- normalize_names(splitted_name, key = key)
  #     # as kyle pointed out, tiers are "score at least lines" so need to cut half a point from them
  #     output_df$tidyline <- as.numeric(gsub('[^0-9]', '', splitted_line)) - .5
  #   }
  #
  #   # set the odds
  #   output_df$tidyamericanodds <- ifelse(as.numeric(output_df$price) - 1 < 1,
  #                                        -100 / (as.numeric(output_df$price) - 1),
  #                                        (as.numeric(output_df$price) - 1) * 100)
  #
  #   # handle any tidy values that weren't already handled
  #   ## if tidyplayer isn't set, set it
  #   if (!'tidyplayer' %in% names(output_df)) {
  #     hacky_tidyplayer <- hacky_tidyup_player_names(unlist(output_df$name))
  #     output_df$tidyplayer <- normalize_names(hacky_tidyplayer, key = key)
  #   }
  #   ## if tidyline isn't set, set it
  #   if (!'tidyline' %in% names(output_df) && 'currenthandicap' %in% names(output_df)) {
  #     output_df$tidyline <- unlist(output_df$currenthandicap)
  #   }
  #   ## if the ou column doesn't exist, make it exist but NA_character
  #   if (!'tidyou' %in% names(output_df)) {
  #     output_df$tidyou <- NA_character_
  #   }
  # }

  # tidyup the matchup! use the team abbreviations from the lookup
  no_pipes <- gsub('\\|', '', output_df$matchup)
  matchup_list <- strsplit(no_pipes, ' at ')
  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'))

  # 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'

  # keep the tidy columns
  names_to_keep <- names(output_df)[grepl('tidy|prop|quarter', names(output_df))]
  output_df <- output_df[, names(output_df) %in% names_to_keep]

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

  # guess sometimes there are no odds, filter those out they do no good
  output_df <- output_df[!is.na(output_df$tidyamericanodds), ]

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