Skip to content

Instantly share code, notes, and snippets.

@jsta
Created March 11, 2019 00:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsta/c50a1d11ce77dba9430b77d5771525b9 to your computer and use it in GitHub Desktop.
Save jsta/c50a1d11ce77dba9430b77d5771525b9 to your computer and use it in GitHub Desktop.
Download files from Zenodo record id
#' Download files from Zenodo record id
#'
#' @param record_id record id
#' @param dest_folder destination folder for file downloads
#' @param token API key
#'
#' @details https://github.com/zenodo/zenodo/issues/1629#issuecomment-435062462
#'
#' @importFrom httr GET content
#' @importFrom jsonlite fromJSON
#' @export
#'
#' @examples \dontrun{
#' download_zenodo(record_id = "2025865")
#' }
download_zenodo <- function(record_id, dest_folder = "",
token = Sys.getenv("ZENODO_PAT")){
qry <- paste0("https://zenodo.org/api/records/", record_id)
api_response <- httr::GET(qry, query = list(access_token = token))
api_response <- jsonlite::fromJSON(httr::content(api_response, as = "text"))
file_urls <- api_response$files$links$download
norm_path <- function(fldr, x){
if(nchar(fldr) > 0){
file.path(fldr, x)
}else{
x
}
}
invisible(lapply(file_urls, function(x)
download.file(x,
norm_path(dest_folder, basename(x)))))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment