misc/scripts/ref
2023-06-07 11:33:01 +02:00

42 lines
1.3 KiB
Bash
Executable File

#!/bin/sh
# Generate a CSL bibliography entry from a DOI or ISBN
if test "${1#*10.}" != "$1"; then
# DOI
curl -sLH 'Accept: application/x-bibtex' "https://doi.org/$1" \
| pandoc -f bibtex -t markdown -s
else
# ISBN
oclc=$(curl -sI "https://www.worldcat.org/isbn/$1" \
| sed -nE '/^Location:/ {s@.*oclc/([0-9]+).*@\1@p; q}')
curl -s "https://openlibrary.org/api/volumes/brief/isbn/$1.json" \
| jq --arg oclc "$oclc" '
def splitname:
split(" ") | {family: .[-1], given: .[0:-1] | map (.[0:1] + ".")| join(" ")};
[.records | .[]] | .[0] as $reply
| ($reply.data + $reply.details) as $data
| [{author: $data.authors | map(.name | splitname),
isbn: $data.identifiers.isbn_10[0],
edition: $data.details | (.edition_name // "1") | split(" ") | .[0] | sub("[a-z]+"; ""),
issued: $data.publish_date,
publisher: $data.details.publishers[0],
"publisher-place": $data.details | (.publish_places[0] // "Nowhere"),
type: "book",
title: $data.title,
url: ("https://www.worldcat.org/oclc/" + $oclc)}]' \
| pandoc -f csljson -t markdown -s
fi | sed '
# delete start of header
1,3d
# insert the reference id
/^- / {i - id: CHANGE_ME
s/- / /
}
# unquote the url
/url:/ s/"//g
# delete end of header
/^---/d
$d'