Skip to content

Commit

Permalink
#31 listFiles / downloadFiles
Browse files Browse the repository at this point in the history
  • Loading branch information
eblondel committed Aug 6, 2020
1 parent 8c654f1 commit 84dcb9f
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 6 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: zen4R
Version: 0.4
Date: 2020-08-05
Date: 2020-08-06
Title: Interface to 'Zenodo' REST API
Authors@R: c(
person("Emmanuel", "Blondel", role = c("aut", "cre"), email = "emmanuel.blondel1@gmail.com", comment = c(ORCID = "0000-0002-5870-5762")),
Expand Down
10 changes: 10 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## **_0.4_**

* [#26](https://github.com/eblondel/zen4R/issues/26) addCreator: allow to specify name, rather than firstname and lastname
* [#27](https://github.com/eblondel/zen4R/issues/27) Fix bugs in addCommunity
* [#28](https://github.com/eblondel/zen4R/issues/28) Fix search by concept DOI
* [#30](https://github.com/eblondel/zen4R/issues/30) simple way to supply token from envvar with ``zenodo_pat()``
* [#31]https://github.com/eblondel/zen4R/issues/31) Methods to list/download files from Zenodo record
* [#32](https://github.com/eblondel/zen4R/issues/32) Remove dependency with rvest package (not stricly needed)
* [#33](https://github.com/eblondel/zen4R/issues/33) Secure API token with 'keyring'

## **_0.3_**

**New features**
Expand Down
79 changes: 79 additions & 0 deletions R/ZenodoRecord.R
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,23 @@
#' Export metadata as all Zenodo supported metadata formats. THis function will
#' create one file per Zenodo metadata formats.
#' }
#' \item{\code{listFiles(pretty)}}{
#' List files attached to the record. By default \code{pretty} is TRUE and the output
#' will be a \code{data.frame}, otherwise a \code{list} will be returned.
#' }
#' \item{\code{downloadFiles(path, parallel, parallel_handler, cl, ...)}}{
#' Download files attached to the record. The \code{path} can be specified as target
#' download directory (by default it will be the current working directory).
#'
#' The argument \code{parallel} (default is \code{FALSE}) can be used to parallelize
#' the files download. If set to \code{TRUE}, files will be downloaded in parallel
#' using default \code{mclapply} interface from \pkg{parallel} package. To use a different
#' parallel handler (such as eg \code{parLapply} or \code{parSapply}), specify its function
#' in \code{parallel_handler} argument. For cluster-based parallel download, this is the
#' way to proceed. In that case, the cluster should be created earlier by the user with
#' \code{makeCluster} and passed as \code{cl} argument. After downloading all files, the cluster
#' will be stopped automatically.
#' }
#' }
#'
#' @author Emmanuel Blondel <emmanuel.blondel1@@gmail.com>
Expand Down Expand Up @@ -1106,6 +1123,68 @@ ZenodoRecord <- R6Class("ZenodoRecord",
exportAsAllFormats = function(filename){
formats <- c("BibTeX","CSL","DataCite","DublinCore","DCAT","JSON","JSON-LD","GeoJSON","MARCXML")
invisible(lapply(formats, self$exportAs, filename))
},

#listFiles
listFiles = function(pretty = TRUE){
if(!pretty) return(self$files)
outdf <- do.call("rbind", lapply(self$files, function(x){
return(
data.frame(
id = x$id,
checksum = x$checksum,
filename = x$filename,
download = x$links$download,
self = x$links$self,
stringsAsFactors = FALSE
)
)
}))
return(outdf)
},

#downloadFiles
downloadFiles = function(path = ".", parallel = FALSE, parallel_handler = NULL, cl = NULL, ...){
if(length(self$files)==0){
self$WARN(sprintf("No files to download for record '%s' (doi: '%s')",
self$id, self$doi))
}else{
files_summary <- sprintf("Download %s file%s from record '%s' (doi: '%s') - total size: %s",
length(self$files), ifelse(length(self$files)>1,"s",""), self$id,
self$doi, sum(sapply(self$files, function(x){x$filesize})))

download_file <- function(file){
cat(sprintf("Downloading file '%s' from record '%s' (doi: '%s') - size: %s\n",
file$filename, self$id, self$doi, file$filesize))
download.file(url = file$links$download, destfile = file.path(path, file$filename))
cat(sprintf("File '%s' successfully downloaded at '%s'\n",
file$filename, file.path(path, file$filename)))
}

if(parallel){
self$INFO("Download in parallel mode")
if(is.null(parallel_handler)){
self$INFO("Using default parallel 'mclapply' handler")
self$INFO(files_summary)
invisible(mclapply(self$files, download_file, ...))
}else{
self$INFO("Using cluster-based parallel handler")
if(is.null(cl)){
errMsg <- "No cluster object defined as 'cl' argument. Aborting file download..."
self$ERROR(errMsg)
stop(errMsg)
}
self$INFO(files_summary)
invisible(parallel_handler(cl, self$files, download_file, ...))
try(stopCluster(cl))
}
}else{
self$INFO("Download in sequential mode")
self$INFO(files_summary)
invisible(lapply(self$files, download_file))
}
self$INFO("End of download")
}
}

)
Expand Down
4 changes: 2 additions & 2 deletions R/zen4R.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#' \tabular{ll}{
#' Package: \tab zen4R\cr
#' Type: \tab Package\cr
#' Version: \tab 0.3-1\cr
#' Date: \tab 2020-07-20\cr
#' Version: \tab 0.4\cr
#' Date: \tab 2020-08-06\cr
#' License: \tab MIT\cr
#' LazyLoad: \tab yes\cr
#' }
Expand Down
17 changes: 17 additions & 0 deletions man/ZenodoRecord.Rd

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

4 changes: 2 additions & 2 deletions man/zen4R.Rd

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

2 changes: 1 addition & 1 deletion tests/test-all.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ library(zen4R)

#test environment
zenodo_url <- "https://sandbox.zenodo.org/api"
zenodo_token <- "mytoken"
zenodo_token <- "v1inBpipYx9aXF47ymeNowa8zQBLUqpprrmRMd8EfR3RKdRHyU5pEeSE7b8x"
zenodo_logger <- "DEBUG"
ZENODO <- try(ZenodoManager$new(url = zenodo_url, token = zenodo_token, logger = zenodo_logger))

Expand Down

0 comments on commit 84dcb9f

Please sign in to comment.