R/helper-aflcoaches.R

Defines functions scrape_coaches_votes

#' Scrape Coaches Votes
#'
#' Scrapes the AFLCA website for coaches votes for a particular season, round and competition.
#'
#' @param season Season in YYYY format. This can be an array of seasons. Defaults to null in which case season
#' 2021 is used.
#' @param round_number Round number. For finals this is the number of H&A rounds plus the Finals week. Defaults to null
#' in which case round 1 is used.
#' @param comp One of "AFLM" (default) or "AFLW"
#'
#' @keywords internal
#' @noRd
scrape_coaches_votes <- function(season = NULL,
                                 round_number = NULL,
                                 comp = "AFLM",
                                 finals) {
  
  # error checking
  check_comp(comp)
  if (is.null(round_number)) round_number <- 1
  season <- check_season(season)
  
  # AFLW 2023 Correction
  if(comp=="AFLW" & season==2022){
    season_type <- readline("Multiple AFLW 2022 Seasons. Type 'S6' (Jan-Apr) or 'S7' (Aug-Nov): ")
    if(season_type=="S6") season <- 2022
    if(season_type=="S7") season <- "season-7"
    if(!season_type%in%c("S6","S7")) stop("Invalid input. Please enter one of 'S6' or 'S7'.")
  }

  # awards are different depending on finals
  link_base <- ifelse(comp == "AFLW",
    "https://aflcoaches.com.au/awards/aflw-champion-player-of-the-year-award/leaderboard/",
    ifelse(finals,
      "https://aflcoaches.com.au/awards/gary-ayres-award-best-finals-player/leaderboard/",
      "https://aflcoaches.com.au/awards/the-aflca-champion-player-of-the-year-award/leaderboard/"
    )
  )

  # finish the link depending on the round and season
  link <- paste0(
    link_base,
    season,
    "/",
    dplyr::case_when(season=="season-7" ~ 2023,
                     season>=2023 ~ season+1,
                     T ~ season),
    ifelse(comp == "AFLW", "02", "01"),
    sprintf("%02d", round_number)
  )

  # read the link
  html <- rvest::read_html(link)

  # closeAllConnections()

  # extract each piece of information from the link
  home.teams <- rvest::html_elements(html, ".pr-md-3.votes-by-match .club_logo") %>%
    rvest::html_attr("title") %>%
    .[seq(1, length(.), 2)]
  away.teams <- rvest::html_elements(html, ".pr-md-3.votes-by-match .club_logo") %>%
    rvest::html_attr("title") %>%
    .[seq(2, length(.), 2)]
  votes <- rvest::html_elements(html, ".pr-md-3.votes-by-match .col-2") %>%
    rvest::html_text() %>%
    stringr::str_remove_all("\n") %>%
    stringr::str_remove_all("\t")
  players <- rvest::html_elements(html, ".pr-md-3.votes-by-match .col-10") %>%
    rvest::html_text() %>%
    stringr::str_remove_all("\n") %>%
    stringr::str_remove_all("\t")

  # arrange the info into a data frame
  df <- data.frame(
    Season = season, Round = round_number,
    Home.Team = NA, Away.Team = NA, Player.Name = players, Coaches.Votes = votes
  ) %>%
    # split the data frame into matches
    dplyr::mutate(Match.Id = cumsum(.data$Coaches.Votes == "Votes" &
      .data$Player.Name == "Player (Club)")) %>%
    # assign home and away teams to each match
    dplyr::mutate(
      Home.Team = home.teams[.data$Match.Id],
      Away.Team = away.teams[.data$Match.Id]
    ) %>%
    # remove unnecessary rows/columns
    dplyr::filter(!(.data$Coaches.Votes == "Votes" & .data$Player.Name == "Player (Club)")) %>%
    dplyr::select(-"Match.Id")

  return(df)
}
#' @rdname fetch_coaches_votes
#' @export

Try the fitzRoy package in your browser

Any scripts or data that you put into this service are public.

fitzRoy documentation built on Sept. 11, 2024, 6:52 p.m.