parse_fanduel_data <- function(fanduel_data, sport, prop = FALSE, game_lines = FALSE, exclude_live = TRUE, exclude_alts = FALSE, game_part = "full") {
# extract the events as a data.frame for merging later
events <- dplyr::bind_rows(fanduel_data$main$attachments$events) %>%
dplyr::select(eventId, name, openDate) %>%
dplyr::rename(
matchup = name,
tipoff = openDate
)
# if there's only one element in fanduel_data and its name is main, then do things differently
if (length(fanduel_data) == 1 && names(fanduel_data) == 'main') {
# extract the markets as a data.frame
markets <- dplyr::bind_rows(fanduel_data$main$attachments$markets)
# subset selected markets based on game lines or prop
if (game_lines == TRUE) {
market_names <- c("Total Points", "Spread Betting", "Moneyline")
}
else {
return()
}
selected_markets <- markets[markets$marketName %in% market_names, ]
# merge the events and markets
output_df <- selected_markets %>%
dplyr::inner_join(events, by='eventId')
return(output_df)
}
# if theres more than one element in fanduel_data, that means there are individual events to parse. nbd, just a little extra effort
if (length(fanduel_data) > 1) {
# loop through the events element and get what is needed
output_list <- list()
for (e in names(fanduel_data$events)) {
# extract the tabs for that event
game_event <- fanduel_data$events[[e]]
# parse based on the prop
if (prop == 'fpts by team') market_names <- "First Team Basket Scorer"
else if (prop == 'fpts shot type') market_names <- "Method of First Basket"
else if (prop == 'fpts') market_names <- "First Basket"
# loop through the tabs in the event and extract the correct markets
tab_list <- list()
for (tab in names(game_event)) {
tab_data <- game_event[[tab]]
markets_df <- dplyr::bind_rows(tab_data$attachments$markets)
if (!market_names %in% markets_df$marketName) next
selected_markets <- markets_df[markets_df$marketName %in% market_names, ]
if (nrow(selected_markets) == 0 || length(selected_markets) == 0) next
tab_list[[length(tab_list) + 1]] <- selected_markets
}
# turn the tabs into a df and merged into the big output list
tab_output_df <- dplyr::bind_rows(tab_list)
output_list[[length(output_list) + 1]] <- tab_output_df
}
# stick all the elements into a df and merged with events, then distinctify
output_df <- dplyr::bind_rows(output_list) %>%
dplyr::inner_join(events, by='eventId') %>%
dplyr::distinct()
return(output_df)
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.