Skip to content

Instantly share code, notes, and snippets.

@jperryhouts
Created September 6, 2018 00:18
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 jperryhouts/353b705707c9dce6f7fcef007ef5af78 to your computer and use it in GitHub Desktop.
Save jperryhouts/353b705707c9dce6f7fcef007ef5af78 to your computer and use it in GitHub Desktop.
Check zotero library against crossref.org
#!/bin/bash
##
## Requires: sqlite3, jq, wdiff, and curl
##
## It is strongly recommended to use the -m flag with your email address,
## as this will put your crossref reuqests in a priority queue, and help
## the crossref folks keep their service running smoothly.
printhelp() { echo 'Usage: checkzoterodb.sh [-m|--mailto <email>] <database>' ; }
mailto=""
DB="$HOME/Zotero/zotero.sqlite"
while [ "$1" ]; do
case "$1" in
'-h'|'--help')
printhelp
exit 0
;;
'-m'|'--mailto')
mailto=$2
shift
;;
*)
if [ -f $1 ]; then
DB="$1"
else
printhelp
exit 1
fi
;;
esac
shift
done
mkdir -p /tmp/crossref
doiID=$(sqlite3 "${DB}" 'SELECT fieldID FROM fields WHERE fieldName="DOI";')
dateID=$(sqlite3 "${DB}" 'SELECT fieldID FROM fields WHERE fieldName="date";')
titleID=$(sqlite3 "${DB}" 'SELECT fieldID FROM fields WHERE fieldName="title";')
for itemID in $(sqlite3 "${DB}" 'SELECT itemID FROM items;'); do
[ "$itemID" == "" ] && continue
idateID=$(sqlite3 "${DB}" 'SELECT valueID FROM itemData WHERE fieldID="'${dateID}'" and itemID="'${itemID}'"')
idoiID=$(sqlite3 "${DB}" 'SELECT valueID FROM itemData WHERE fieldID="'${doiID}'" and itemID="'${itemID}'"')
[ "${idoiID}" == "" ] && continue
ititleID=$(sqlite3 "${DB}" 'SELECT valueID FROM itemData WHERE fieldID="'${titleID}'" and itemID="'${itemID}'"')
[ "${ititleID}" == "" ] && continue
title=$(sqlite3 "${DB}" 'SELECT value FROM itemDataValues WHERE valueID="'${ititleID}'"')
doi=$(sqlite3 "${DB}" 'SELECT value FROM itemDataValues WHERE valueID="'${idoiID}'"')
[ "${doi}" == "" ] && echo " - No DOI value on item: $title" && continue
year=$(sqlite3 "${DB}" 'SELECT value FROM itemDataValues WHERE valueID="'${idateID}'"' | cut -d '-' -f 1)
## Get crossref data (first check for cached version)
refdata=/tmp/crossref/$(echo "$doi" | md5sum | cut -d ' ' -f 1)
if [ ! -f $refdata ]; then
if [ "$mailto" ]; then
curl -s https://api.crossref.org/works/${doi}?mailto=${mailto} > "${refdata}" 2>/dev/null
else
curl -s https://api.crossref.org/works/${doi} > "${refdata}" 2>/dev/null
fi
fi
## Compare year
crossrefyear="null"
[ "$crossrefyear" == "null" ] && crossrefyear=$(jq '.message.issued."date-parts"[0][0]' "${refdata}")
[ "$crossrefyear" == "null" ] && crossrefyear=$(jq '.message."journal-issue"."published-print"."date-parts"[0][0]' "${refdata}")
[ "$crossrefyear" == "null" ] && crossrefyear=$(jq '.message."published-print"."date-parts"[0][0]' "${refdata}")
if [ ! "$year" == "$crossrefyear" ]; then
echo "$crossrefyear != $year :: $title :: $doi"
fi
## Compare title
crossreftitle=$(jq '.message.title[0]' "${refdata}")
diff -i -q <(echo "$crossreftitle") <(echo "\"$title\"") >/dev/null
if [ ! $? -eq 0 ]; then
echo -n "Title does not match (DOI: $doi): "
wdiff -i -w "$(tput bold;tput setaf 1)" -x "$(tput sgr0)" \
-y "$(tput bold;tput setaf 2)" -z "$(tput sgr0)" \
<(echo "$crossreftitle") <(echo "\"$title\"")
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment