Refactor to collect all books in a category

Signed-off-by: Marcus Noble <github@marcusnoble.co.uk>
This commit is contained in:
2026-03-01 10:39:09 +00:00
parent 70d0e8d8dd
commit 2b7dd60e0d
3 changed files with 16 additions and 14 deletions

View File

@@ -27,7 +27,7 @@ func init() {
} }
func main() { func main() {
latestBooks := map[string]*storygraph.Book{} latestBooks := map[string]*[]storygraph.Book{}
refreshingBooks := true refreshingBooks := true
var lastUpdated *time.Time var lastUpdated *time.Time
@@ -41,9 +41,9 @@ func main() {
} }
// Update each category individually with books we have managed to find // Update each category individually with books we have managed to find
for cat, book := range newBookList { for cat, books := range newBookList {
if b, ok := newBookList[cat]; ok && b != nil { if b, ok := newBookList[cat]; ok && b != nil {
latestBooks[cat] = book latestBooks[cat] = books
} }
} }
now := time.Now() now := time.Now()
@@ -60,7 +60,7 @@ func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
type data struct { type data struct {
Books map[string]*storygraph.Book Books map[string]*[]storygraph.Book
LastUpdated string LastUpdated string
Refreshing bool Refreshing bool
} }

View File

@@ -37,7 +37,7 @@ func init() {
c = New(cookie) c = New(cookie)
} }
func GetLatestBooks() (map[string]*Book, error) { func GetLatestBooks() (map[string]*[]Book, error) {
fmt.Println("Fetching latest book recommendations...") fmt.Println("Fetching latest book recommendations...")
links := []Book{} links := []Book{}
@@ -46,7 +46,7 @@ func GetLatestBooks() (map[string]*Book, error) {
page++ page++
fmt.Println("Fetching page", page) fmt.Println("Fetching page", page)
resp, err := c.Get(fmt.Sprintf("https://app.thestorygraph.com/to-read/averagemarcus?page=%d", page)) resp, err := c.Get(fmt.Sprintf("https://app.thestorygraph.com/to-read/averagemarcus?per_page=100&page=%d", page))
if err != nil { if err != nil {
fmt.Println("Error making request:", err) fmt.Println("Error making request:", err)
return nil, err return nil, err
@@ -94,7 +94,7 @@ func GetLatestBooks() (map[string]*Book, error) {
return links[i].Rating > links[j].Rating return links[i].Rating > links[j].Rating
}) })
return map[string]*Book{ return map[string]*[]Book{
"Fiction": nextByTag(links, "Fiction"), "Fiction": nextByTag(links, "Fiction"),
"Non-Fiction": nextByTag(links, "Non-Fiction"), "Non-Fiction": nextByTag(links, "Non-Fiction"),
"Health": nextByTag(links, "Health"), "Health": nextByTag(links, "Health"),
@@ -230,11 +230,12 @@ func bookContains(links []Book, bookID string) bool {
return false return false
} }
func nextByTag(links []Book, tag string) *Book { func nextByTag(links []Book, tag string) *[]Book {
filtered := []Book{}
for _, b := range links { for _, b := range links {
if slices.Contains(b.Tags, tag) { if slices.Contains(b.Tags, tag) {
return &b filtered = append(filtered, b)
} }
} }
return nil return &filtered
} }

View File

@@ -78,7 +78,7 @@
{{ if index .Books "Fiction" }} {{ if index .Books "Fiction" }}
<main> <main>
{{ $cat := "Fiction" }} {{ $cat := "Fiction" }}
{{ $book := index .Books $cat }} {{ $book := index (index .Books $cat) 0 }}
<div class="category"> <div class="category">
<h3>{{ $cat }}</h3> <h3>{{ $cat }}</h3>
<a href="{{ $book.Link }}" target="_blank"> <a href="{{ $book.Link }}" target="_blank">
@@ -88,7 +88,7 @@
<div class="rating">⭐ {{ $book.Rating }}</div> <div class="rating">⭐ {{ $book.Rating }}</div>
</div> </div>
{{ $cat := "Non-Fiction" }} {{ $cat := "Non-Fiction" }}
{{ $book := index .Books $cat }} {{ $book := index (index .Books $cat) 0 }}
<div class="category"> <div class="category">
<h3>{{ $cat }}</h3> <h3>{{ $cat }}</h3>
<a href="{{ $book.Link }}" target="_blank"> <a href="{{ $book.Link }}" target="_blank">
@@ -98,7 +98,7 @@
<div class="rating">⭐ {{ $book.Rating }}</div> <div class="rating">⭐ {{ $book.Rating }}</div>
</div> </div>
{{ $cat := "Comics" }} {{ $cat := "Comics" }}
{{ $book := index .Books $cat }} {{ $book := index (index .Books $cat) 0 }}
<div class="category"> <div class="category">
<h3>{{ $cat }}</h3> <h3>{{ $cat }}</h3>
<a href="{{ $book.Link }}" target="_blank"> <a href="{{ $book.Link }}" target="_blank">
@@ -108,10 +108,11 @@
<div class="rating">⭐ {{ $book.Rating }}</div> <div class="rating">⭐ {{ $book.Rating }}</div>
</div> </div>
{{ range $cat, $book := .Books }} {{ range $cat, $Books := .Books }}
{{ if or (eq $cat "Fiction") (eq $cat "Non-Fiction") (eq $cat "Comics") }} {{ if or (eq $cat "Fiction") (eq $cat "Non-Fiction") (eq $cat "Comics") }}
{{ continue }} {{ continue }}
{{ end }} {{ end }}
{{ $book := index $Books 0 }}
<div class="category"> <div class="category">
<h3>{{ $cat }}</h3> <h3>{{ $cat }}</h3>
<a href="{{ $book.Link }}" target="_blank"> <a href="{{ $book.Link }}" target="_blank">