R/tseDownloadFinalAccountability.R

tseDownloadFinalAccountability <- function(destFolder,
                                  dataUrl,
                                  filePattern = NULL,
                                  electionType,
                                  year) {


  # this is the default location
  if (is.null(dataUrl)){
    dataUrl <- "http://agencia.tse.jus.br/estatistica/sead/odsele/prestacao_contas/"
  }


  # this is the file name pattern we are looking for...
  if (is.null(filePattern)){
    fileName <- "prestacao_contas_final_%s.zip"
  }

  # regexp to identify correctly formed http urls
  httpRegexp <- "http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+"

  # check if the location is in fact a web location
  if (grepl(httpRegexp, dataUrl)){

    fullUrl <- sprintf(
      paste0(dataUrl,
             fileName), year)

    # if it is, check if it is valid
    if (httr::http_error(fullUrl)){
      # if not, stop and display error message
      stop(paste0(
        paste("\n[English]: Invalid location:", fullUrl),
        paste("\n[Portuguese]: Local invalido:", fullUrl)))
    } else{

      # Download data
      dataFile <- tempfile()

      if (is.null(destFolder)){
        dataFolder <- tempdir()
      }else{
        dataFolder <- destFolder
      }

      download.file(fullUrl, dataFile)

      unzip(dataFile, exdir = dataFolder)
      # unlink(dataFile)
    }
  }
  return(dataFolder)
}
brunomssmelo/TseWrangler documentation built on May 13, 2019, 8:07 a.m.