Last active
May 27, 2021 23:53
-
-
Save sbibauw/f453e6f9a27111abf44252abf65c3606 to your computer and use it in GitHub Desktop.
Rename PDF as AuthorYear.pdf from Crossref DOI data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Used as an embedded shell script in Hazel for files where a DOI is available | |
# | |
# Depedencies: | |
# - pdfgrep | |
# - curl | |
# - jq | |
newname=$(pdfgrep -oPm 1 --page-range=1-2 '\b10.\d{4,9}/[-._;()/:A-Za-z0-9]+\b' $1 | | |
xargs -I{} curl -sLH "Accept: application/json" http://dx.doi.org/{} | | |
jq '.author[0].family, .issued."date-parts"[0][0]' | | |
tr '\n' ' ' | | |
sed 's/[" ]//g') | |
# Check that $newname is not empty (in case there was an error) before renaming | |
if [[ ! -z "$newname" ]]; then | |
mv $1 "$newname.pdf" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment