parse_mgm_data <- function(mgm_data, sport, prop = FALSE, game_lines = FALSE) {
# loop through mgm_data and extract the correct prop
output_list <- list()
for (e in 1:length(mgm_data)) {
# subset the game event
game_event <- mgm_data[[e]]$fixture
matchup <- paste(game_event$participants$name$value, collapse = ' @ ')
tipoff <- game_event$startDate
# get the game lines if you're trying to do that
if (game_lines == TRUE) {
gl_out <- parse_mgm_main(game_event = game_event, matchup = matchup, tipoff = tipoff)
output_list[[length(output_list) + 1]] <-
parse_mgm_main(game_event = game_event, matchup = matchup, tipoff = tipoff)
next
}
# extract correct props
if (prop %in% c('first team to score', 'ftts')) {
output_list[[length(output_list) + 1]] <-
parse_mgm_prop(game_event = game_event, 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_mgm_prop(game_event = game_event, prop_name = 'First Field Goal',
matchup = matchup, tipoff = tipoff)
}
if (prop %in% c('fpts by team')) {
output_list[[length(output_list) + 1]] <-
parse_mgm_prop(game_event = game_event,
prop_regex = 'first field goal', prop_not_regex = 'exact method|win$|goal$',
matchup = matchup, tipoff = tipoff)
}
if (prop %in% c('fpts shot points')) {
output_list[[length(output_list) + 1]] <-
parse_mgm_prop(game_event = game_event,
prop_regex = 'exact method',
matchup = matchup, tipoff = tipoff)
}
if (prop %in% c('win tipoff')) {
output_list[[length(output_list) + 1]] <- parse_mgm_prop(game_event = game_event, prop_regex = 'To win the Tip', matchup = matchup, tipoff = tipoff)
}
if (prop %in% c('first rebound')) {
output_list[[length(output_list) + 1]] <- parse_mgm_prop(game_event = game_event, prop_name = '1st Player to Record a Rebound', matchup = matchup, tipoff = tipoff)
}
if (prop %in% c('first assist')) {
output_list[[length(output_list) + 1]] <- parse_mgm_prop(game_event = game_event, prop_name = '1st Player to Record an Assist', matchup = matchup, tipoff = tipoff)
}
if (prop %in% c('first steal')) {
output_list[[length(output_list) + 1]] <- parse_mgm_prop(game_event = game_event, prop_name = '1st Player to Record a Steal', matchup = matchup, tipoff = tipoff)
}
if (prop %in% c('first block')) {
output_list[[length(output_list) + 1]] <- parse_mgm_prop(game_event = game_event, prop_name = '1st Player to record a Block', matchup = matchup, tipoff = tipoff)
}
if (prop %in% c('first turnover')) {
output_list[[length(output_list) + 1]] <- parse_mgm_prop(game_event = game_event, prop_name = '1st Player to Record a Turnover', 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 mgm ', prop, ' props returned')
if (length(output_list) == 0 & game_lines == TRUE) stop('no mgm game lines props returned')
output_df <- dplyr::bind_rows(output_list)
return(output_df)
}
parse_mgm_prop <- function(game_event, prop_name = FALSE, prop_regex = NULL, prop_not_regex = NULL, matchup, tipoff) {
# browser()
label_vec <- tolower(game_event$games$name$value)
games <- dplyr::bind_rows(game_event$games)
if (prop_name != FALSE && tolower(prop_name) %in% label_vec) {
prop_content <- games[tolower(games$name$value) == tolower(prop_name), ]
outcomes_df <- dplyr::bind_rows(prop_content$results)
}
if (!is.null(prop_regex) && any(grepl(tolower(prop_regex), label_vec))) {
prop_content <- games[grepl(tolower(prop_regex), tolower(games$name$value)), ]
if (!is.null(prop_not_regex)) prop_content <- prop_content[!grepl(tolower(prop_not_regex), tolower(prop_content$name$value)), ]
outcomes_df <- dplyr::bind_rows(prop_content$results)
}
if (!('outcomes_df' %in% ls())) return()
outcomes_df$matchup <- matchup
outcomes_df$tipoff <- tipoff
return(outcomes_df)
}
parse_mgm_main <- function(game_event, matchup, tipoff) {
ml_outputs <- list()
for (ml in c('Moneyline', 'Total Points', 'Point Spread')) {
df <- parse_mgm_prop(game_event, prop_name = ml, matchup = matchup, tipoff = tipoff)
if (length(df) == 0) return()
df$Type <- ml
ml_outputs[[ml]] <- df
}
output_df <- dplyr::bind_rows(ml_outputs)
return(output_df)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.