Skip to content

Instantly share code, notes, and snippets.

@njbart
Created April 14, 2015 10:41
Show Gist options
  • Save njbart/3f0b75e2574f221f3a0c to your computer and use it in GitHub Desktop.
Save njbart/3f0b75e2574f221f3a0c to your computer and use it in GitHub Desktop.
Replace DOIs by shortDOIs in CSL JSON
#! /usr/bin/env Rscript
library("jsonlite")
filename <- commandArgs(TRUE)[1]
bibdata <- fromJSON(filename, simplifyVector = FALSE, simplifyDataFrame = FALSE, simplifyMatrix = FALSE)
for (n in seq_along(bibdata)) {
if (exists('DOI', where=bibdata[[n]]))
{
getshortdoi <- paste0("curl -sS http://shortdoi.org/", bibdata[[n]]$DOI, "?format=json | grep ShortDOI | cut -f 4 -d '\"'")
shortdoi <- try(system(getshortdoi, intern = TRUE))
if (shortdoi=="") {} else {bibdata[[n]]$DOI <- shortdoi}
}
# else {}
}
write(toJSON(bibdata, auto_unbox = TRUE), file = filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment