Added support for comics

Signed-off-by: Marcus Noble <github@marcusnoble.co.uk>
This commit is contained in:
2025-05-03 08:19:44 +01:00
parent 6a16f4285b
commit ba58d6b435

View File

@@ -3,6 +3,7 @@ package storygraph
import ( import (
"fmt" "fmt"
"iter" "iter"
"maps"
"net/http" "net/http"
"os" "os"
"slices" "slices"
@@ -110,6 +111,7 @@ func GetLatestBooks() (map[string]*Book, error) {
"Business": nextByTag(links, "Business"), "Business": nextByTag(links, "Business"),
"Technology": nextByTag(links, "Technology"), "Technology": nextByTag(links, "Technology"),
"Sci-Fi": nextByTag(links, "Sci-Fi"), "Sci-Fi": nextByTag(links, "Sci-Fi"),
"Comics": nextByTag(links, "Comics"),
}, nil }, nil
} }
@@ -180,7 +182,8 @@ func getImage(decs iter.Seq[*html.Node]) string {
} }
func getTags(decs iter.Seq[*html.Node]) []string { func getTags(decs iter.Seq[*html.Node]) []string {
tags := []string{} tags := map[string]bool{}
for n := range decs { for n := range decs {
if n.Type == html.ElementNode && n.DataAtom == atom.Div { if n.Type == html.ElementNode && n.DataAtom == atom.Div {
for _, a := range n.Attr { for _, a := range n.Attr {
@@ -191,25 +194,27 @@ func getTags(decs iter.Seq[*html.Node]) []string {
if b.Type == html.TextNode { if b.Type == html.TextNode {
switch b.Data { switch b.Data {
case "fiction": case "fiction":
tags = append(tags, "Fiction") tags["Fiction"] = true
case "nonfiction": case "nonfiction":
tags = append(tags, "Non-Fiction") tags["Non-Fiction"] = true
case "psychology": case "psychology":
fallthrough fallthrough
case "self help": case "self help":
fallthrough fallthrough
case "health": case "health":
tags = append(tags, "Health") tags["Health"] = true
case "art": case "art":
tags = append(tags, "Art") tags["Art"] = true
case "business": case "business":
tags = append(tags, "Business") tags["Business"] = true
case "technology": case "technology":
fallthrough fallthrough
case "computer science": case "computer science":
tags = append(tags, "Technology") tags["Technology"] = true
case "science fiction": case "science fiction":
tags = append(tags, "Sci-Fi") tags["Sci-Fi"] = true
case "comics":
tags["Comics"] = true
} }
} }
} }
@@ -219,7 +224,13 @@ func getTags(decs iter.Seq[*html.Node]) []string {
} }
} }
} }
return tags
// Only include comics in the "comics" category
if tags["Comics"] {
tags = map[string]bool{"Comics": true}
}
return slices.Collect(maps.Keys(tags))
} }
func bookContains(links []Book, bookID string) bool { func bookContains(links []Book, bookID string) bool {