Repository navigation
Expand file tree
/
Copy pathdoris.sh
More file actions
executable file
·167 lines (145 loc) · 3.95 KB
/
Copy pathdoris.sh
File metadata and controls
executable file
·167 lines (145 loc) · 3.95 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env bash
# Doris – DOI to RIS Tool 🦉
# Author: RubiaLab
# Email: rubialab@rubialab.de
# Version 1.0
#
# Fetches bibliographic data from a DOI and saves in RIS (default),
# optionally BibTeX and CSL-JSON formats.
set -euo pipefail
show_help() {
cat <<EOF
Usage: doris <DOI> [options]
Options:
--bib Also save BibTeX file
--json Also save CSL-JSON file
--no-browser Do not open the DOI URL automatically in the browser
--outdir DIR Output directory (default: current directory)
--help Show this help message and exit
Examples:
doris 10.1038/s41586-020-2649-2
doris 10.1038/s41586-020-2649-2 --bib --json --outdir ./library
EOF
}
if [ $# -eq 0 ]; then
show_help
exit 1
fi
DOI=""
FETCH_BIB=false
FETCH_JSON=false
OPEN_BROWSER=true
OUTDIR="."
# Parse arguments
while [[ $# -gt 0 ]]; do
case "$1" in
--bib)
FETCH_BIB=true
shift
;;
--json)
FETCH_JSON=true
shift
;;
--no-browser)
OPEN_BROWSER=false
shift
;;
--outdir)
if [ -z "${2:-}" ]; then
echo "❌ Error: --outdir requires a directory argument."
exit 1
fi
OUTDIR="$2"
shift 2
;;
--help)
show_help
exit 0
;;
-*)
echo "❌ Error: Unknown option $1"
show_help
exit 1
;;
*)
if [ -z "$DOI" ]; then
DOI="$1"
shift
else
echo "❌ Error: Multiple DOIs or unknown arguments provided."
show_help
exit 1
fi
;;
esac
done
if [ -z "$DOI" ]; then
echo "❌ Error: No DOI provided."
exit 1
fi
# Check if OUTDIR exists or create it
if [ ! -d "$OUTDIR" ]; then
mkdir -p "$OUTDIR" || {
echo "❌ Error: Could not create directory $OUTDIR."
exit 1
}
fi
URL="https://doi.org/$DOI"
# Fetch RIS data (default)
RIS=$(curl -fsSLH "Accept: application/x-research-info-systems" "$URL") || {
echo "❌ Error: Could not connect to $URL."
exit 1
}
if [ -z "$RIS" ]; then
echo "❌ Error: Could not retrieve RIS data."
exit 1
fi
# Extract first author (AU line)
AUTHOR=$(echo "$RIS" | grep '^AU' | head -n1 | cut -d'-' -f2 | sed 's/,.*//' | tr -d '[:space:]' | tr '[:upper:]' '[:lower:]')
[ -z "$AUTHOR" ] && AUTHOR="unknown"
# Extract year (PY line)
YEAR=$(echo "$RIS" | grep '^PY' | grep -o '[0-9]\{4\}' | head -n1)
[ -z "$YEAR" ] && YEAR="nodate"
# Sanitize DOI for filename (letters, digits, underscore only)
SAFE_DOI=$(echo "$DOI" | tr '/' '_' | tr -cd '[:alnum:]_')
# Base filename
BASENAME="${AUTHOR}${YEAR}"
# Save RIS file
RIS_PATH="${OUTDIR}/${BASENAME}.ris"
echo "$RIS" > "$RIS_PATH"
echo "✅ RIS saved: $RIS_PATH"
# Optionally save BibTeX
if [ "$FETCH_BIB" = true ]; then
BIB=$(curl -fsSLH "Accept: application/x-bibtex" "$URL") || {
echo "⚠️ Warning: Could not retrieve BibTeX data."
BIB=""
}
if [ -n "$BIB" ]; then
BIB_PATH="${OUTDIR}/${BASENAME}.bib"
echo "$BIB" > "$BIB_PATH"
echo "✅ BibTeX saved: $BIB_PATH"
fi
fi
# Optionally save CSL-JSON
if [ "$FETCH_JSON" = true ]; then
CSL=$(curl -fsSLH "Accept: application/vnd.citationstyles.csl+json" "$URL") || {
echo "⚠️ Warning: Could not retrieve JSON data."
CSL=""
}
if [ -n "$CSL" ]; then
JSON_PATH="${OUTDIR}/${BASENAME}.json"
echo "$CSL" > "$JSON_PATH"
echo "✅ CSL-JSON saved: $JSON_PATH"
fi
fi
# Open URL in browser (optional)
if [ "$OPEN_BROWSER" = true ]; then
if command -v xdg-open >/dev/null; then
xdg-open "$URL" >/dev/null 2>&1 &
elif command -v open >/dev/null; then
open "$URL" >/dev/null 2>&1 &
else
echo "⚠️ Warning: No known browser opener found."
fi
fi