Refactor to collect all books in a category
Signed-off-by: Marcus Noble <github@marcusnoble.co.uk>
This commit is contained in:
8
main.go
8
main.go
@@ -27,7 +27,7 @@ func init() {
|
||||
}
|
||||
|
||||
func main() {
|
||||
latestBooks := map[string]*storygraph.Book{}
|
||||
latestBooks := map[string]*[]storygraph.Book{}
|
||||
refreshingBooks := true
|
||||
var lastUpdated *time.Time
|
||||
|
||||
@@ -41,9 +41,9 @@ func main() {
|
||||
}
|
||||
|
||||
// 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 {
|
||||
latestBooks[cat] = book
|
||||
latestBooks[cat] = books
|
||||
}
|
||||
}
|
||||
now := time.Now()
|
||||
@@ -60,7 +60,7 @@ func main() {
|
||||
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
type data struct {
|
||||
Books map[string]*storygraph.Book
|
||||
Books map[string]*[]storygraph.Book
|
||||
LastUpdated string
|
||||
Refreshing bool
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@
|
||||
{{ if index .Books "Fiction" }}
|
||||
<main>
|
||||
{{ $cat := "Fiction" }}
|
||||
{{ $book := index .Books $cat }}
|
||||
{{ $book := index (index .Books $cat) 0 }}
|
||||
<div class="category">
|
||||
<h3>{{ $cat }}</h3>
|
||||
<a href="{{ $book.Link }}" target="_blank">
|
||||
@@ -88,7 +88,7 @@
|
||||
<div class="rating">⭐ {{ $book.Rating }}</div>
|
||||
</div>
|
||||
{{ $cat := "Non-Fiction" }}
|
||||
{{ $book := index .Books $cat }}
|
||||
{{ $book := index (index .Books $cat) 0 }}
|
||||
<div class="category">
|
||||
<h3>{{ $cat }}</h3>
|
||||
<a href="{{ $book.Link }}" target="_blank">
|
||||
@@ -98,7 +98,7 @@
|
||||
<div class="rating">⭐ {{ $book.Rating }}</div>
|
||||
</div>
|
||||
{{ $cat := "Comics" }}
|
||||
{{ $book := index .Books $cat }}
|
||||
{{ $book := index (index .Books $cat) 0 }}
|
||||
<div class="category">
|
||||
<h3>{{ $cat }}</h3>
|
||||
<a href="{{ $book.Link }}" target="_blank">
|
||||
@@ -108,10 +108,11 @@
|
||||
<div class="rating">⭐ {{ $book.Rating }}</div>
|
||||
</div>
|
||||
|
||||
{{ range $cat, $book := .Books }}
|
||||
{{ range $cat, $Books := .Books }}
|
||||
{{ if or (eq $cat "Fiction") (eq $cat "Non-Fiction") (eq $cat "Comics") }}
|
||||
{{ continue }}
|
||||
{{ end }}
|
||||
{{ $book := index $Books 0 }}
|
||||
<div class="category">
|
||||
<h3>{{ $cat }}</h3>
|
||||
<a href="{{ $book.Link }}" target="_blank">
|
||||
|
||||
Reference in New Issue
Block a user