R/download_bin.R

Defines functions download_bin

Documented in download_bin

#' @title Downloads the elections zip file
#'
#' @param url The URL of the elections zip to download
#' @param tempfile The path to save the downloaded file
#'
#' @return NULL
#'
#' @importFrom httr GET
#' @importFrom httr add_headers
#'
#' @keywords internal
#'
download_bin <- function(url, tempfile) {
  UA <- paste(
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:98.0)",
    "Gecko/20100101 Firefox/98.0"
  )

  if (file.exists(tempfile)) {
    message(
      "File already exists, skipping download. Reading existing file ",
      gsub("..+/", "", tempfile)
    )
  } else {
    message("Downloading ", url)
    res <- GET(url, add_headers(`User-Agent` = UA, Connection = "keep-alive"))
    writeBin(res$content, tempfile)
  }
}

Try the infoelectoral package in your browser

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

infoelectoral documentation built on June 22, 2024, 6:59 p.m.