{{ $path := .Page.Path }} {{ $isbn := "" }} {{ $key := "" }} {{ with .Get 0 }} {{ if eq (len .) 13 }} {{ $isbn = . }} {{ else }} {{ $key = . }} {{ end }} {{ end }} {{ $isbn := .Get 0 }} {{ if not $key }} {{ $query := (dict "type" "/type/edition" "isbn_13" $isbn) | jsonify }} {{ $thing := getJSON (printf "https://openlibrary.org/api/things?query=%s" ($query)) }} {{ if and (eq $thing.status "ok") (ge (len $thing.result) 1) }} {{ $key = index $thing.result 0 }} {{ end }} {{ end}} {{ if $key }} {{ $bookObj := getJSON (printf "https://openlibrary.org/api/get?key=%s" $key) }} {{ if eq $bookObj.status "ok" }} {{ $isbn = index $bookObj.result.isbn_13 0 }} {{ $authors := slice }} {{ range $bookObj.result.authors }} {{ $authorObj := getJSON (printf "https://openlibrary.org/api/get?key=%s" .key) }} {{ if eq $authorObj.status "ok" }} {{ $authors = $authors | append (dict "name" $authorObj.result.name "url" (printf "https://openlibrary.org%s" $authorObj.result.key)) }} {{ end }} {{ end }} {{ $coverURL := (printf "https://covers.openlibrary.org/b/isbn/%s-M.jpg" $isbn)}} {{ if not $isbn }} {{ $coverURL = printf "https://covers.openlibrary.org/b/id/%.0f-M.jpg" (index $bookObj.result.covers 0) }} {{ end }} {{ $cover := "" }} {{ with resources.GetRemote $coverURL }} {{ with .Err }} {{ else }} {{ $cover = .Content | resources.FromString (replaceRE `\.md$` ".jpg" $path) }} {{ end }} {{ end }} {{ $book := dict "title" $bookObj.result.title "authors" $authors "url" (printf "https://openlibrary.org%s" $key) "cover" $cover }} {{ partial "book.html" $book }} {{ end }} {{ end }}