commit cf4f4a4d15050b31a76ce1250f8bfd87a038b5ed
parent f2e2653181588d92b0e25e3b2e633bb33b888269
Author: Anders Damsgaard <anders@adamsgaard.dk>
Date: Wed, 10 Apr 2019 11:29:59 +0200
Add script to get DOI(s) from search terms
Diffstat:
A | bin/getdoi | | | 90 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 90 insertions(+), 0 deletions(-)
diff --git a/bin/getdoi b/bin/getdoi
@@ -0,0 +1,90 @@
+#!/usr/bin/env bash
+
+version=1.0
+host="http://api.crossref.org/works"
+
+function show_help {
+ echo "usage: ${0##*/} [OPTIONS] QUERY"
+ echo "will attempt to get the DOI from $host"
+ echo "where QUERY can consist of publication title, author, DOI, ORCID id."
+ echo "If no QUERY is specified, this program will expect a QUERY as stdin."
+ echo
+ echo "OPTIONS are one or more of the following:"
+ echo " -h, --help show this message"
+ echo " -v, --version show version and license information"
+ echo " -V, --verbose show verbose information"
+ echo " -t, --tor-socks use torsocks for HTTP requests"
+ echo " -n, --number=NUM return NUM results (default 1)"
+ echo " -- do not consider any following args as options"
+}
+
+function show_version {
+ echo "${0##*/} version $version"
+ echo "Licensed under the GNU Public License, v3+"
+ echo "written by Anders Damsgaard, anders@adamsgaard.dk"
+ echo "https://gitlab.com/admesg/dotfiles"
+}
+
+function die {
+ printf '%s\n' "$1" >&2
+ exit 1
+}
+
+function extract_dois {
+ tr ',' '\n' | grep DOI | \
+ sed 's/.*DOI":"//' | sed 's/"}.*//' | sed 's|\\\/|/|g'
+}
+
+function get_doi {
+ query="$(echo "$@" | sed 's/ /+/g')"
+ url="$host?rows=$number&select=DOI&query=$query"
+ [ "$verbose" = 1 ] && echo "connecting to $url"
+ result=$($prefix curl --header "Accept: application/json" \
+ --header "Content-Type: application/json" \
+ --silent --show-error \
+ --request GET "$url")
+ echo "$result" | extract_dois
+}
+
+verbose=0
+number=1
+prefix=""
+while :; do
+ case "$1" in
+ -h|-\?|--help)
+ show_help
+ exit 0
+ ;;
+ -v|--version)
+ show_version
+ exit 0
+ ;;
+ -V|--verbose)
+ verbose=1
+ ;;
+ -t|--tor-socks)
+ prefix="torsocks"
+ ;;
+ -n|--number)
+ number="$2"
+ shift
+ ;;
+ --) # end all options
+ shift
+ break
+ ;;
+ -?*)
+ die 'Error: Unknown option specified'
+ ;;
+ *) # No more options
+ break
+ esac
+ shift
+done
+
+if [ $# -lt 1 ]; then
+ doi="$(cat)"
+ get_doi "$doi"
+else
+ get_doi "$@"
+fi