www.byjp.me/layouts/partials/openlibrary.obj
2024-10-10 23:31:21 +01:00

88 lines
No EOL
3.4 KiB
Text

{{/* Return empty object while OpenLibrary is down. I need something more robust here! */}}
{{ return dict }}
{{/*
{{- $path := .path -}}
{{- $isbn := "" -}}
{{- $key := "" -}}
{{- $showCover := ne .showcover false -}}
{{- $book := dict -}}
{{- with .id -}}
{{- if eq (len .) 13 -}}
{{- $isbn = . -}}
{{- else -}}
{{- $key = . -}}
{{- end -}}
{{- end -}}
{{- if not $key -}}
{{- $query := (dict "type" "/type/edition" "isbn_13" $isbn) | jsonify -}}
{{- with resources.GetRemote (printf "https://openlibrary.org/api/things?query=%s" ($query)) -}}
{{- with .Err -}}
{{- errorf "Error grabbing Thing from OpenLibrary: %v" . -}}
{{- else -}}
{{- $thing := .Content | transform.Unmarshal -}}
{{- if and (eq $thing.status "ok") (ge (len $thing.result) 1) -}}
{{- $key = index $thing.result 0 -}}
{{- end -}}
{{- end -}}
{{- else -}}
{{- errorf "Failed to fetch ISBN data from OpenLibrary: %s" $isbn -}}
{{- end -}}
{{- end -}}
{{- if $key -}}
{{- with resources.GetRemote (printf "https://openlibrary.org/api/get?key=%s" $key) -}}
{{- with .Err -}}
{{- errorf "Error grabbing Book data from OpenLibrary: %v" . -}}
{{- else -}}
{{- $bookObj := .Content | transform.Unmarshal -}}
{{- if eq $bookObj.status "ok" -}}
{{- $isbn = index $bookObj.result.isbn_13 0 -}}
{{- $authors := slice -}}
{{- range $bookObj.result.authors -}}
{{- with resources.GetRemote (printf "https://openlibrary.org/api/get?key=%s" .key) -}}
{{- with .Err -}}
{{- errorf "Error grabbing Author data from OpenLibrary: %v" . -}}
{{- else -}}
{{- $authorObj := .Content | transform.Unmarshal -}}
{{- if eq $authorObj.status "ok" -}}
{{- $authors = $authors | append (dict "name" $authorObj.result.name "url" (printf "https://openlibrary.org%s" $authorObj.result.key)) -}}
{{- else -}}
{{- errorf "OpenLibrary response was %s while fetching author data: %s" $authorObj.status .key -}}
{{- end -}}
{{- end -}}
{{- else -}}
{{- warnf "Failed to fetch Author data from OpenLibrary: %s" .key -}}
{{- end -}}
{{- end -}}
{{- $cover := "" -}}
{{- if $showCover -}}
{{- $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 -}}
{{- $bookFilename := replaceRE `[^a-z]+` "-" (lower $bookObj.result.title) -}}
{{- $coverPath := replaceRE `(\.md)?$` (printf "/%s.jpg" $bookFilename) $path -}}
{{- with resources.GetRemote $coverURL -}}
{{- with .Err -}}
{{- else -}}
{{- $cover = .Content | resources.FromString $coverPath -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- $book = dict "title" $bookObj.result.title "authors" $authors "url" (printf "https://openlibrary.org%s" $key) "cover" $cover -}}
{{- else -}}
{{- errorf "OpenLibrary response was %s while fetching: %s" $bookObj $key -}}
{{- end -}}
{{- end -}}
{{- else -}}
{{- errorf "Failed to fetch Book data data from OpenLibrary: %s" $key -}}
{{- end -}}
{{- end -}}
{{- return $book -}}
*/}}