Skip to content

Commit

Permalink
Merge pull request #301 from weecology/downloads
Browse files Browse the repository at this point in the history
Update zenodo downloads
  • Loading branch information
gmyenni authored Aug 5, 2024
2 parents 6620e89 + c666aa7 commit 76c2f9d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 31 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,4 @@ Suggests:
VignetteBuilder:
knitr
Encoding: UTF-8
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
60 changes: 31 additions & 29 deletions R/download_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#'
#' @param verbose \code{logical} whether to provide details of downloading.
#'
#' @param pause Positive \code{integer} or integer \code{numeric} seconds for pausing during steps around unzipping that require time delayment.
#' @param pause Positive \code{integer} or integer \code{numeric} seconds for pausing during steps around unzipping that require time delayment.
#'
#' @param timeout Positive \code{integer} or integer \code{numeric} seconds for timeout on downloads. Temporarily overrides the \code{"timeout"} option in \code{\link[base]{options}}.
#'
Expand All @@ -26,7 +26,7 @@
#' @export
#'
download_observations <- function (path = get_default_data_path(),
version = "latest",
version = "latest",
source = "github",
quiet = FALSE,
verbose = FALSE,
Expand All @@ -38,28 +38,30 @@ download_observations <- function (path = get_default_data_path(),

timeout_backup <- getOption("timeout")
on.exit(options(timeout = timeout_backup))
options(timeout = timeout)
options(timeout = timeout)

if (source == "zenodo") {

base_url <- "https://zenodo.org/api/records/"
base_url <- "https://zenodo.org/api/records/"

got <- GET(base_url, query = list(q = "conceptrecid:1215988",
size = 9999,
size = 9999,
all_versions = "true"))

stop_for_status(got, task = paste0("locate Zenodo concept record"))

contents <- content(got)
contents <- content(got)
hits=getElement(contents, name = "hits")
hits=getElement(hits, name = "hits")

metadata <- lapply(FUN = getElement,
X = contents,
metadata <- lapply(FUN = getElement,
X = hits,
name = "metadata")
versions <- sapply(FUN = getElement,
X = metadata,
versions <- sapply(FUN = getElement,
X = metadata,
name = "version")
pub_date <- sapply(FUN = getElement,
X = metadata,
pub_date <- sapply(FUN = getElement,
X = metadata,
name = "publication_date")

selected <- ifelse(version == "latest",
Expand All @@ -69,45 +71,45 @@ download_observations <- function (path = get_default_data_path(),
if (length(selected) == 0){

stop(paste0("Failed to locate version `", version, "`"))

}
zipball_url <- contents[[selected]]$files[[1]]$links$download
version <- ifelse(version == "latest",

zipball_url <- hits[[selected]]$files[[1]]$links$self
version <- ifelse(version == "latest",
metadata[[selected]]$version, version)

} else if (source == "github") {

base_url <- "https://api.github.com/repos/weecology/PortalData/releases/"
url <- ifelse(version == "latest",
base_url <- "https://api.github.com/repos/weecology/PortalData/releases/"
url <- ifelse(version == "latest",
paste0(base_url, "latest"),
paste0(base_url, "tags/", version))

got <- GET(url)

stop_for_status(got, task = paste0("locate version `", version, "`"))

zipball_url <- content(got)$zipball_url
zipball_url <- content(got)$zipball_url

version <- ifelse(version == "latest", content(got)$name, version)

} else {

stop("`source` must be either 'zenodo' or 'github'")

}


temp <- file.path(tempdir(), "PortalData.zip")
final <- file.path(path, "PortalData")
version_file <- file.path(final, "version.txt")

if (!force & file.exists(version_file)) {

existing_version <- scan(file = version_file,
what = character(),
existing_version <- scan(file = version_file,
what = character(),
quiet = TRUE)


if (existing_version == version) {

Expand All @@ -128,9 +130,9 @@ download_observations <- function (path = get_default_data_path(),


result <- tryCatch(
expr = download.file(url = zipball_url,
destfile = temp,
quiet = !verbose,
expr = download.file(url = zipball_url,
destfile = temp,
quiet = !verbose,
mode = "wb"),
error = function(x){NA})

Expand All @@ -152,7 +154,7 @@ download_observations <- function (path = get_default_data_path(),

file.remove(old_files)

unlink(x = final,
unlink(x = final,
recursive = TRUE)

}
Expand Down Expand Up @@ -192,7 +194,7 @@ check_for_newer_data <- function (path = get_default_data_path()) {
}


url <- "https://api.github.com/repos/weecology/PortalData/releases/latest"
url <- "https://api.github.com/repos/weecology/PortalData/releases/latest"
got <- tryCatch(GET(url),
error = function(e) NULL)
return_if_null(x = got, value = FALSE)
Expand Down
2 changes: 1 addition & 1 deletion man/portalr.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 76c2f9d

Please sign in to comment.