From 85b65fb2dac569bb2882dd449e61b256403f7fd4 Mon Sep 17 00:00:00 2001 From: Arran Hobson Sayers Date: Thu, 26 Dec 2024 19:09:34 +0000 Subject: [PATCH] Ad subtitle splitting --- goodreads/book.go | 19 ++++++++++++++++++- goodreads/goodreads.go | 2 +- server/book.go | 8 +++++++- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/goodreads/book.go b/goodreads/book.go index ae25da6..b1f3753 100644 --- a/goodreads/book.go +++ b/goodreads/book.go @@ -65,7 +65,7 @@ func (b *Book) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { } type Work struct { - Title string `xml:"original_title"` + FullTitle string `xml:"original_title"` MediaType string `xml:"media_type"` EditionsCount int `xml:"books_count"` @@ -81,6 +81,23 @@ type Work struct { RatingDistribution string `xml:"rating_dist"` } +// Title is the full title with any subtitle removed. +// A subtitle is anything after the first : in the full title +func (w Work) Title() string { + titleParts := strings.Split(w.FullTitle, ":") + return strings.TrimSpace(titleParts[0]) +} + +// Subtitle is the subtle part of the full title. +// A subtitle is anything after the first : in the full title +func (w Work) Subtitle() string { + colonIdx := strings.Index(w.FullTitle, ":") + if colonIdx == -1 { + return "" + } + return strings.TrimSpace(w.FullTitle[colonIdx+1:]) +} + func (w Work) AverageRating() float64 { averageRating := float64(w.RatingsSum) / float64(w.RatingsCount) return math.Round(averageRating*100) / 100 // Round to two decimal places diff --git a/goodreads/goodreads.go b/goodreads/goodreads.go index f9c061a..e21b54a 100644 --- a/goodreads/goodreads.go +++ b/goodreads/goodreads.go @@ -147,7 +147,7 @@ func (c *Client) GetBooksByIds(ctx context.Context, bookIds []string) ([]Book, e // Only return books whose work have a title validBooks := make([]Book, 0, len(books)) for _, book := range books { - if book.Work.Title != "" { + if book.Work.FullTitle != "" { validBooks = append(validBooks, book) } } diff --git a/server/book.go b/server/book.go index 4d2380f..db4c38a 100644 --- a/server/book.go +++ b/server/book.go @@ -50,6 +50,11 @@ func searchKindleBooks( } func goodreadsBookToBookMetadata(goodreadsBook goodreads.Book) BookMetadata { + var subtitle *string + if goodreadsBook.Work.Subtitle() != "" { + subtitle = lo.ToPtr(goodreadsBook.Work.Subtitle()) + } + var authorName *string if len(goodreadsBook.Authors) != 0 { authorName = &goodreadsBook.Authors[0].Name @@ -71,7 +76,8 @@ func goodreadsBookToBookMetadata(goodreadsBook goodreads.Book) BookMetadata { return BookMetadata{ // Work Fields - Title: goodreadsBook.Work.Title, + Title: goodreadsBook.Work.Title(), + Subtitle: subtitle, Author: authorName, PublishedYear: lo.ToPtr(strconv.Itoa(goodreadsBook.Work.PublicationYear)), // Edition Fields