From ba58d6b435389cd015f99702f1b910c8a61467ab Mon Sep 17 00:00:00 2001 From: Marcus Noble Date: Sat, 3 May 2025 08:19:44 +0100 Subject: [PATCH] Added support for comics Signed-off-by: Marcus Noble --- pkg/storygraph/client.go | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/pkg/storygraph/client.go b/pkg/storygraph/client.go index f913d15..f1751b0 100644 --- a/pkg/storygraph/client.go +++ b/pkg/storygraph/client.go @@ -3,6 +3,7 @@ package storygraph import ( "fmt" "iter" + "maps" "net/http" "os" "slices" @@ -110,6 +111,7 @@ func GetLatestBooks() (map[string]*Book, error) { "Business": nextByTag(links, "Business"), "Technology": nextByTag(links, "Technology"), "Sci-Fi": nextByTag(links, "Sci-Fi"), + "Comics": nextByTag(links, "Comics"), }, nil } @@ -180,7 +182,8 @@ func getImage(decs iter.Seq[*html.Node]) string { } func getTags(decs iter.Seq[*html.Node]) []string { - tags := []string{} + tags := map[string]bool{} + for n := range decs { if n.Type == html.ElementNode && n.DataAtom == atom.Div { for _, a := range n.Attr { @@ -191,25 +194,27 @@ func getTags(decs iter.Seq[*html.Node]) []string { if b.Type == html.TextNode { switch b.Data { case "fiction": - tags = append(tags, "Fiction") + tags["Fiction"] = true case "nonfiction": - tags = append(tags, "Non-Fiction") + tags["Non-Fiction"] = true case "psychology": fallthrough case "self help": fallthrough case "health": - tags = append(tags, "Health") + tags["Health"] = true case "art": - tags = append(tags, "Art") + tags["Art"] = true case "business": - tags = append(tags, "Business") + tags["Business"] = true case "technology": fallthrough case "computer science": - tags = append(tags, "Technology") + tags["Technology"] = true 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 {