Skip to content

Commit 4aaecd4

Browse files
committed
Implements a cache method so that a file is not downloaded if it already exists in tempdir
1 parent 52407c3 commit 4aaecd4

8 files changed

+24
-16
lines changed

R/candidatos_nosenado.R

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ candidatos_nosenado <- function(tipo, anno, mes) {
2424
url <- paste0(urlbase, tipo, anno, mes, "_MUNI", ".zip")
2525
### Descargo el fichero zip en un directorio temporal y lo descomprimo
2626
tempd <- tempdir(check = TRUE)
27-
temp <- tempfile(tmpdir = tempd, fileext = ".zip")
27+
filename <- gsub(".+/", "", url)
28+
temp <- file.path(tempd, filename)
29+
tempd <- file.path(tempd, gsub(".zip", "", filename))
2830
download_bin(url, temp)
2931
unzip(temp, overwrite = TRUE, exdir = tempd)
3032

R/cleanup.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
#' @keywords internal
99
#'
1010
cleanup <- function(files, dirs) {
11-
file.remove(files)
11+
# file.remove(files)
1212
unlink(dirs, recursive=TRUE)
1313
}

R/download_bin.R

+10-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@ download_bin <- function(url, tempfile) {
1515
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:98.0)",
1616
"Gecko/20100101 Firefox/98.0"
1717
)
18-
message("Downloading ", url)
19-
res <- GET(url, add_headers(`User-Agent` = UA, Connection = "keep-alive"))
20-
writeBin(res$content, tempfile)
18+
19+
if(file.exists(tempfile)) {
20+
message("File already exists, skipping download")
21+
} else {
22+
message("Downloading ", url)
23+
res <- GET(url, add_headers(`User-Agent` = UA, Connection = "keep-alive"))
24+
writeBin(res$content, tempfile)
25+
}
26+
27+
2128
}

R/mesas.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ mesas <- function(tipo_eleccion, anno, mes) {
2727
### Descargo el fichero zip en un directorio temporal y lo descomprimo
2828
tempd <- tempdir(check = TRUE)
2929
filename <- gsub(".+/", "", url)
30-
temp <- file.path(tempd, filename, fsep = "\\")
31-
tempd <- paste0(tempd, "\\", gsub(".zip", "", filename))
30+
temp <- file.path(tempd, filename)
31+
tempd <- file.path(tempd, gsub(".zip", "", filename))
3232
download_bin(url, temp)
3333
unzip(temp, overwrite = TRUE, exdir = tempd)
3434

R/muni.R

+2-3
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ municipios <- function(tipo_eleccion, anno, mes, distritos = FALSE) {
2828
### Descargo el fichero zip en un directorio temporal y lo descomprimo
2929
tempd <- tempdir(check = TRUE)
3030
filename <- gsub(".+/", "", url)
31-
temp <- file.path(tempd, filename, fsep = "\\")
32-
tempd <- paste0(tempd, "\\", gsub(".zip", "", filename))
31+
temp <- file.path(tempd, filename)
32+
tempd <- file.path(tempd, gsub(".zip", "", filename))
3333
download_bin(url, temp)
34-
3534
unzip(temp, overwrite = TRUE, exdir = tempd)
3635

3736
### Construyo las rutas a los ficheros DAT necesarios

R/provincias.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ provincias <- function(tipo_eleccion, anno, mes) {
2727
### Descargo el fichero zip en un directorio temporal y lo descomprimo
2828
tempd <- tempdir(check = TRUE)
2929
filename <- gsub(".+/", "", url)
30-
temp <- file.path(tempd, filename, fsep = "\\")
31-
tempd <- paste0(tempd, "\\", gsub(".zip", "", filename))
30+
temp <- file.path(tempd, filename)
31+
tempd <- file.path(tempd, gsub(".zip", "", filename))
3232
download_bin(url, temp)
3333
unzip(temp, overwrite = TRUE, exdir = tempd)
3434

R/senado_mesas.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ senado_mesas <- function(anno, mes) {
2626
### Descargo el fichero zip en un directorio temporal y lo descomprimo
2727
tempd <- tempdir(check = TRUE)
2828
filename <- gsub(".+/", "", url)
29-
temp <- file.path(tempd, filename, fsep = "\\")
30-
tempd <- paste0(tempd, "\\", gsub(".zip", "", filename))
29+
temp <- file.path(tempd, filename)
30+
tempd <- file.path(tempd, gsub(".zip", "", filename))
3131
download_bin(url, temp)
3232
unzip(temp, overwrite = TRUE, exdir = tempd)
3333

R/senado_municipios.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ senado_municipios <- function(anno, mes) {
2626
### Descargo el fichero zip en un directorio temporal y lo descomprimo
2727
tempd <- tempdir(check = TRUE)
2828
filename <- gsub(".+/", "", url)
29-
temp <- file.path(tempd, filename, fsep = "\\")
30-
tempd <- paste0(tempd, "\\", gsub(".zip", "", filename))
29+
temp <- file.path(tempd, filename)
30+
tempd <- file.path(tempd, gsub(".zip", "", filename))
3131
download_bin(url, temp)
3232
unzip(temp, overwrite = TRUE, exdir = tempd)
3333

0 commit comments

Comments
 (0)