commit 2e02b0a59df72b6dd1cbbb8f8b6e1707e5c9a073
parent c9a04e32be9ad5ebc0570472b35e3f9b4b522c1c
Author: Anders Damsgaard <anders@adamsgaard.dk>
Date: Fri, 12 Apr 2019 11:48:26 +0200
Add option to open DOI as URL in browser
Diffstat:
1 file changed, 21 insertions(+), 2 deletions(-)
diff --git a/bin/getdoi b/bin/getdoi
@@ -17,6 +17,7 @@ function show_help {
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 " -o, --open open DOI as url in browser"
echo " -- do not consider any following args as options"
}
@@ -37,6 +38,18 @@ function extract_dois {
sed 's/.*DOI":"//' | sed 's/"}.*//' | sed 's|\\\/|/|g'
}
+function browser_open {
+ if command -v xdg-open >/dev/null 2>&1; then
+ [ "$verbose" = 1 ] && echo "launching default browser"
+ xdg-open "$1"
+ elif command -v open >/dev/null 2>&1; then
+ [ "$verbose" = 1 ] && echo "launching default browser"
+ open "$1"
+ else
+ die 'Error: could not open a browser'
+ fi
+}
+
function get_doi_from_crossref {
query="$(echo "$@" | sed 's/ /+/g')"
url="$host?rows=$number&select=DOI&query=$query"
@@ -58,14 +71,17 @@ function get_doi_from_file {
function get_doi {
if [ -e "$1" ]; then
- get_doi_from_file "$1"
+ doi=$(get_doi_from_file "$1")
else
- get_doi_from_crossref "$@"
+ doi=$(get_doi_from_crossref "$@")
fi
+ echo "$doi"
+ [ "$open" = 1 ] && browser_open "https://doi.org/${doi}"
}
verbose=0
number=1
+open=0
prefix=""
while :; do
case "$1" in
@@ -87,6 +103,9 @@ while :; do
number="$2"
shift
;;
+ -o|--open)
+ open=1
+ ;;
--) # end all options
shift
break