tgetdoi - scholarref - tools for DOI and BiBTeX reference extraction, fetching, and parsing
 (HTM) git clone git://src.adamsgaard.dk/scholarref
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       tgetdoi (3421B)
       ---
            1 #!/bin/sh
            2 
            3 version=1.0
            4 host="http://api.crossref.org/works"
            5 
            6 show_help() {
            7         echo "usage: ${0##*/} [OPTIONS] QUERY"
            8         echo "returns the publication DOI from $host"
            9         echo "where QUERY can consist of publication title, author, DOI, ORCID id,"
           10         echo "or a PDF file. In case a file is specified, ${0##*/} will attempt to"
           11         echo "extract the DOI from it."
           12         echo "If no QUERY is specified, this program will expect a QUERY as stdin."
           13         echo
           14         echo "OPTIONS are one or more of the following:"
           15         echo "   -h, --help        show this message"
           16         echo "   -v, --version     show version and license information"
           17         echo "   -V, --verbose     show verbose information"
           18         echo "   -t, --tor-socks   use torsocks for HTTP requests"
           19         echo "   -n, --notify      send result as desktop notification"
           20         echo "   -N, --number NUM  return NUM results (default 1)"
           21         echo "   -c, --clip        paste DOI to clipboard (requires xclip)"
           22         echo "   -o, --open        open DOI as url in browser"
           23         echo "   --                do not consider any following args as options"
           24 }
           25 
           26 show_version() {
           27         echo "${0##*/} version $version"
           28         echo "Licensed under the ISC License"
           29         echo "written by Anders Damsgaard, anders@adamsgaard.dk"
           30         echo "https://src.adamsgaard.dk/scholarref"
           31 }
           32 
           33 die() {
           34         printf '%s\n' "$1" >&2
           35         exit 1
           36 }
           37 
           38 extract_dois() {
           39         tr ',' '\n' | grep DOI | \
           40                 sed 's/.*DOI":"//' | sed 's/"}.*//' | sed 's|\\\/|/|g'
           41 }
           42 
           43 browser_open() {
           44         if command -v xdg-open >/dev/null 2>&1; then
           45                 [ "$verbose" = 1 ] && echo "launching default browser"
           46                 xdg-open "$1"
           47         elif command -v open >/dev/null 2>&1; then
           48                 [ "$verbose" = 1 ] && echo "launching default browser"
           49                 open "$1"
           50         else
           51                 die 'Error: could not open a browser'
           52         fi
           53 }
           54 
           55 get_doi_from_crossref() {
           56         query="$(echo "$@" | sed 's/ /+/g')"
           57         url="$host?rows=$number&select=DOI&query=$query"
           58         [ "$verbose" = 1 ] && echo "connecting to $url"
           59         result=$($prefix curl --header "Accept: application/json" \
           60                 --header "Content-Type: application/json" \
           61                 --silent --show-error \
           62                 --request GET "$url")
           63         echo "$result" | extract_dois
           64 }
           65 
           66 get_doi_from_file() {
           67         doi=$(pdfinfo "$1" | grep -io "doi.*" -m 1) ||
           68         doi=$(pdftotext "$1" 2>/dev/null - | grep -io "doi.*" -m 1) ||
           69         die "Error: Could not extract DOI from file $1"
           70         doi=$(echo "$doi" | sed 's/[A-Za-z\.\/:]*//;s/[\.,]$//' |\
           71                   sed 's/.*\(10\.\)/\1/' | cut -d' ' -f1 | sed 's/,//g')
           72         echo "$doi"
           73 }
           74 
           75 get_doi() {
           76         if [ -e "$1" ]; then
           77                 doi=$(get_doi_from_file "$1")
           78         else
           79                 if [ $(expr "$1" : '^10\.[0-9]\+\/.*') -gt 0 ]; then
           80                         doi="$1"
           81                 else
           82                         doi=$(get_doi_from_crossref "$@")
           83                 fi
           84         fi
           85         echo "$doi"
           86         [ "$clip" = 1 ] && printf '%s' "${doi}" | xclip
           87         [ "$clip" = 1 ] && printf 'https://doi.org/%s' "${doi}" | \
           88                 xclip -selection clipboard
           89         [ "$notify" = 1 ] && notify-send "$doi"
           90         [ "$open" = 1 ] && browser_open "https://doi.org/${doi}"
           91 }
           92 
           93 verbose=0
           94 number=1
           95 clip=0
           96 open=0
           97 notify=0
           98 prefix=""
           99 while :; do
          100         case "$1" in
          101                 -h|-\?|--help)
          102                         show_help
          103                         exit 0
          104                         ;;
          105                 -v|--version)
          106                         show_version
          107                         exit 0
          108                         ;;
          109                 -V|--verbose)
          110                         verbose=1
          111                         ;;
          112                 -t|--tor-socks)
          113                         prefix="torsocks"
          114                         ;;
          115                 -N|--number)
          116                         number="$2"
          117                         shift
          118                         ;;
          119                 -n|--notify)
          120                         notify=1
          121                         ;;
          122                 -c|--clip)
          123                         clip=1
          124                         ;;
          125                 -o|--open)
          126                         open=1
          127                         ;;
          128                 --) # end all options
          129                         shift
          130                         break
          131                         ;;
          132                 -?*)
          133                         die 'Error: Unknown option specified'
          134                         ;;
          135                 *)  # No more options
          136                         break
          137         esac
          138         shift
          139 done
          140 
          141 if [ $# -lt 1 ]; then
          142         query="$(cat)"
          143         get_doi "$query"
          144 else
          145         get_doi "$@"
          146 fi