R/main.R

Defines functions submit_tips get_games

Documented in get_games submit_tips

#' Get Games
#'
#' get_current_games provides an API to return the current rounds matches in a data frame.
#'
#'
#' @param user monash username, in text
#' @param pass monash password, in text
#' @param comp comp type, should be one of "normal", "gauss" or "info"
#' @param round (optional),  round number to return. If not provided, will try find the current round.
#'
#' @return data.frame of the matches from the currently available tipping round
#' @export
#'
#' @examples
#' \dontrun{
#' get_games(user, pass, comp)
#' }
get_games <- function(user, pass, comp, round = NULL) {
  if (is.null(round)) get_current_round(user, pass)
  # make request
  requ <- make_request(user = user, pass = pass, comp = comp, round = round)
  # get games
  get_games_tbl(requ)
}


#' Submit Tips
#'
#' @param games a table of games, ideally returned from `get_games`
#' @param user monash username, in text
#' @param pass monash password, in text
#' @param comp comp type, should be one of "normal", "gauss" or "info"
#' @param round (optional),  round number to return. If not provided, will try find the current round.
#'
#' @export
#'
#' @examples
#' \dontrun{
#' submit_tips(games, user, pass, comp)
#' }
submit_tips <- function(games, user, pass, comp, round = NULL) {
  if (is.null(round)) round <- get_current_round(user, pass)

  # make request
  sess <- create_session()
  requ <- make_request(user, pass, comp, round = round)
  form_unfilled <- get_form(requ)
  form_unfilled$action <- paste0("http://probabilistic-footy.monash.edu",
                                 form_unfilled$action)
  # add new fields
  form_filled <- convert_tips_to_form(games, form_unfilled, comp)

  # submit
  result <- rvest::session_submit(sess, form_filled)
  result$response %>%
    httr::content() %>%
    rvest::html_node("h1+ table") %>%
    rvest::html_table()
}
jimmyday12/monash_tipr documentation built on June 10, 2021, 8:41 p.m.