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

@@ -37,7 +37,7 @@ func init() {
c = New(cookie)
}
func GetLatestBooks() (map[string]*Book, error) {
func GetLatestBooks() (map[string]*[]Book, error) {
fmt.Println("Fetching latest book recommendations...")
links := []Book{}
@@ -46,7 +46,7 @@ func GetLatestBooks() (map[string]*Book, error) {
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 {
fmt.Println("Error making request:", err)
return nil, err
@@ -94,7 +94,7 @@ func GetLatestBooks() (map[string]*Book, error) {
return links[i].Rating > links[j].Rating
})
return map[string]*Book{
return map[string]*[]Book{
"Fiction": nextByTag(links, "Fiction"),
"Non-Fiction": nextByTag(links, "Non-Fiction"),
"Health": nextByTag(links, "Health"),
@@ -230,11 +230,12 @@ func bookContains(links []Book, bookID string) bool {
return false
}
func nextByTag(links []Book, tag string) *Book {
func nextByTag(links []Book, tag string) *[]Book {
filtered := []Book{}
for _, b := range links {
if slices.Contains(b.Tags, tag) {
return &b
filtered = append(filtered, b)
}
}
return nil
return &filtered
}