Skip to content

Commit

Permalink
Merge pull request #95 from cboettig/patch/new_zenodo
Browse files Browse the repository at this point in the history
Patch/new zenodo
  • Loading branch information
cboettig authored Oct 20, 2023
2 parents b90eb88 + 42c337f commit 6a5d9f0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: contentid
Version: 0.0.17
Version: 0.0.18
Title: An Interface for Content-Based Identifiers
Description: An interface for creating, registering, and resolving content-based
identifiers for data management. Content-based identifiers rely on
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# contentid 0.0.18

* Patch parsing for new Zenodo API

# contentid 0.0.17

- bugfix for Zenodo resolver to search all versions
Expand Down
21 changes: 14 additions & 7 deletions R/zenodo_registry.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# md5 only for now
# id <- "hash://md5/eb5e8f37583644943b86d1d9ebd4ded5"
sources_zenodo <- function(id, host = "https://zenodo.org"){
query <- "/api/records/?q=_files.checksum:"
query <- "/api/records?q=files.entries.checksum:"
hash <- strip_prefix(id)
algo <- extract_algo(id)

Expand All @@ -11,11 +11,12 @@ sources_zenodo <- function(id, host = "https://zenodo.org"){
return(null_query())
}

checksum <- paste0('"', algo, ":", hash, '"')
url <- paste0(host, query, checksum, '&all_versions=1')
checksum <- curl::curl_escape(paste0('"', algo, ":", hash, '"'))
url <- paste0(host, query, checksum, '&allversions=true')

sources <- tryCatch({
resp <- httr::GET(url)
httr::stop_for_status(resp)
sources <- httr::content(resp)
},
error = function(e){
Expand All @@ -28,15 +29,21 @@ sources_zenodo <- function(id, host = "https://zenodo.org"){
if(length(sources) == 0){
return(null_query())
}
sources <- sources[[1]]

if(sources$hits$total == 0){
return(null_query())
}

matches <- sources$hits$hits[[1]]

## The associated record may also have other files, match by id:
ids <- vapply(sources$files, `[[`, character(1L), "checksum")
file <- sources$files[ids == hash][[1]]
ids <- vapply(matches$files, `[[`, character(1L), "checksum")
item <- matches$files[ids == hash]
file <- item[[1]]

download_url <- file$links$download
size <- file$filesize
date <- sources$created
date <- matches$created
out <- registry_entry(id, source = download_url, size =size, date = date)
out
}
Expand Down

0 comments on commit 6a5d9f0

Please sign in to comment.