Skip to content

Commit

Permalink
Ad subtitle splitting
Browse files Browse the repository at this point in the history
  • Loading branch information
ahobsonsayers committed Dec 26, 2024
1 parent fd08f71 commit 85b65fb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
19 changes: 18 additions & 1 deletion goodreads/book.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion goodreads/goodreads.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
8 changes: 7 additions & 1 deletion server/book.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 85b65fb

Please sign in to comment.