small pixel drawing of a pufferfish j3s.sh

openlibrary/openlibrary.go

package openlibrary

import (
	"encoding/json"
	"fmt"
	"io"
	"net/http"
)

type CurrentBooks struct {
	ReadingLogEntries []struct {
		Work struct {
			Title            string      `json:"title"`
			Key              string      `json:"key"`
			AuthorKeys       []string    `json:"author_keys"`
			AuthorNames      []string    `json:"author_names"`
			FirstPublishYear int         `json:"first_publish_year"`
			LendingEditionS  interface{} `json:"lending_edition_s"`
			EditionKey       []string    `json:"edition_key"`
			CoverID          int         `json:"cover_id"`
			CoverEditionKey  string      `json:"cover_edition_key"`
		} `json:"work"`
		LoggedEdition string `json:"logged_edition"`
		LoggedDate    string `json:"logged_date"`
	} `json:"reading_log_entries"`
}

func GetCurrentBooks() *CurrentBooks {
	cb := CurrentBooks{}
	resp, err := http.Get("https://openlibrary.org/people/j3sj3sj3s/books/currently-reading.json")
	if err != nil {
		fmt.Println(err)
	}
	defer resp.Body.Close()
	body, err := io.ReadAll(resp.Body)
	err = json.Unmarshal(body, &cb)
	if err != nil {
		fmt.Println(err)
	}
	return &cb
}