Skip to content

Commit

Permalink
Implements a cache method so that a file is not downloaded if it alre…
Browse files Browse the repository at this point in the history
…ady exists in tempdir
  • Loading branch information
hmeleiro committed May 30, 2024
1 parent 52407c3 commit 4aaecd4
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 16 deletions.
4 changes: 3 additions & 1 deletion R/candidatos_nosenado.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ candidatos_nosenado <- function(tipo, anno, mes) {
url <- paste0(urlbase, tipo, anno, mes, "_MUNI", ".zip")
### Descargo el fichero zip en un directorio temporal y lo descomprimo
tempd <- tempdir(check = TRUE)
temp <- tempfile(tmpdir = tempd, fileext = ".zip")
filename <- gsub(".+/", "", url)
temp <- file.path(tempd, filename)
tempd <- file.path(tempd, gsub(".zip", "", filename))
download_bin(url, temp)
unzip(temp, overwrite = TRUE, exdir = tempd)

Expand Down
2 changes: 1 addition & 1 deletion R/cleanup.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
#' @keywords internal
#'
cleanup <- function(files, dirs) {
file.remove(files)
# file.remove(files)
unlink(dirs, recursive=TRUE)
}
13 changes: 10 additions & 3 deletions R/download_bin.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ download_bin <- function(url, tempfile) {
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:98.0)",
"Gecko/20100101 Firefox/98.0"
)
message("Downloading ", url)
res <- GET(url, add_headers(`User-Agent` = UA, Connection = "keep-alive"))
writeBin(res$content, tempfile)

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


}
4 changes: 2 additions & 2 deletions R/mesas.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ mesas <- function(tipo_eleccion, anno, mes) {
### Descargo el fichero zip en un directorio temporal y lo descomprimo
tempd <- tempdir(check = TRUE)
filename <- gsub(".+/", "", url)
temp <- file.path(tempd, filename, fsep = "\\")
tempd <- paste0(tempd, "\\", gsub(".zip", "", filename))
temp <- file.path(tempd, filename)
tempd <- file.path(tempd, gsub(".zip", "", filename))
download_bin(url, temp)
unzip(temp, overwrite = TRUE, exdir = tempd)

Expand Down
5 changes: 2 additions & 3 deletions R/muni.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ municipios <- function(tipo_eleccion, anno, mes, distritos = FALSE) {
### Descargo el fichero zip en un directorio temporal y lo descomprimo
tempd <- tempdir(check = TRUE)
filename <- gsub(".+/", "", url)
temp <- file.path(tempd, filename, fsep = "\\")
tempd <- paste0(tempd, "\\", gsub(".zip", "", filename))
temp <- file.path(tempd, filename)
tempd <- file.path(tempd, gsub(".zip", "", filename))
download_bin(url, temp)

unzip(temp, overwrite = TRUE, exdir = tempd)

### Construyo las rutas a los ficheros DAT necesarios
Expand Down
4 changes: 2 additions & 2 deletions R/provincias.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ provincias <- function(tipo_eleccion, anno, mes) {
### Descargo el fichero zip en un directorio temporal y lo descomprimo
tempd <- tempdir(check = TRUE)
filename <- gsub(".+/", "", url)
temp <- file.path(tempd, filename, fsep = "\\")
tempd <- paste0(tempd, "\\", gsub(".zip", "", filename))
temp <- file.path(tempd, filename)
tempd <- file.path(tempd, gsub(".zip", "", filename))
download_bin(url, temp)
unzip(temp, overwrite = TRUE, exdir = tempd)

Expand Down
4 changes: 2 additions & 2 deletions R/senado_mesas.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ senado_mesas <- function(anno, mes) {
### Descargo el fichero zip en un directorio temporal y lo descomprimo
tempd <- tempdir(check = TRUE)
filename <- gsub(".+/", "", url)
temp <- file.path(tempd, filename, fsep = "\\")
tempd <- paste0(tempd, "\\", gsub(".zip", "", filename))
temp <- file.path(tempd, filename)
tempd <- file.path(tempd, gsub(".zip", "", filename))
download_bin(url, temp)
unzip(temp, overwrite = TRUE, exdir = tempd)

Expand Down
4 changes: 2 additions & 2 deletions R/senado_municipios.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ senado_municipios <- function(anno, mes) {
### Descargo el fichero zip en un directorio temporal y lo descomprimo
tempd <- tempdir(check = TRUE)
filename <- gsub(".+/", "", url)
temp <- file.path(tempd, filename, fsep = "\\")
tempd <- paste0(tempd, "\\", gsub(".zip", "", filename))
temp <- file.path(tempd, filename)
tempd <- file.path(tempd, gsub(".zip", "", filename))
download_bin(url, temp)
unzip(temp, overwrite = TRUE, exdir = tempd)

Expand Down

0 comments on commit 4aaecd4

Please sign in to comment.