-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoi2bib
executable file
·75 lines (65 loc) · 2.03 KB
/
doi2bib
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/bash
#
# usage:
# doi2bib -d DOI | -i DOILISTFILE [-o OUTBIBFILE]
#
# arguments:
# -d DOI: doi URL reference e.g.,
# http://dx.doi.org/10.1002/2014GL062882 or simply
# 10.1002/2014GL062882
#
# -i DOILISTFILE: A text file containing doi lists of multiple references
# Note: Use either -d or -i option
#
# -o OUTBIBFILE (optional): Filename to write bibtex entries, default is
# screen-dump
#
# requires:
# cURL
#
# author: yaswant.pradhan 2015-08-26
# Reference: http://crosscite.org/cn/
# -----------------------------------------------------------------------------
# usage -----------------------------------------------------------------------
usage() {
cat <<EOF
Usage:
$(basename $0) -d DOI | -i DOILISTFILE [-o OUTBIBFILE]
EOF
}
# Parse command line arguments -----------------------------------------------------
while [ ! -z "$1" ]; do
if [ -z "$2" ]; then
echo "Input option ($1) requires a parameter."
exit 1
fi
case $1 in
"-d") DOI=$2; shift ;;
"-i") DOILISTFILE=$2; shift ;;
"-o") OUTBIBFILE=$2; shift ;;
*) echo "Did not recognise input ($1)."; exit 1;;
esac
shift
done
# Check arguments:
[ -z "$DOI" ] && [ -z "$DOILISTFILE" ] && { usage && exit ; }
# Check conflicting options:
[ ! -z "$DOI" ] && [ ! -z "$DOILISTFILE" ] && \
{ echo "Conflicting options [-d|-i]"; usage && exit ; }
# Construct doi array:
[ -f "$DOILISTFILE" ] && dois=( $(cat "$DOILISTFILE") ) || dois=$DOI
# Retrieve entries ------------------------------------------------------------
echo -e "Retrieving ${#dois[@]} entrie(s)..\n"
base_url='http://dx.doi.org'
cnt=1
for doi in ${dois[@]}; do
[[ $doi != $base_url* ]] && doi=$base_url/$doi
if [ -z "$OUTBIBFILE" ]; then
curl -LH "Accept: text/bibliography; style=bibtex" $doi
echo
else
[[ $cnt -eq 1 ]] && echo > "$OUTBIBFILE" || echo >> "$OUTBIBFILE"
curl -sLH "Accept: text/bibliography; style=bibtex" $doi >> "$OUTBIBFILE"
fi
cnt=$(( cnt+1 ))
done