Skip to content

Commit

Permalink
Fix kindle after addition of hardback and paperback
Browse files Browse the repository at this point in the history
  • Loading branch information
ahobsonsayers committed Dec 27, 2024
1 parent 14c609a commit 2e4e5ad
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions kindle/book.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,30 +121,38 @@ func parseBookCoversAttrValue(coverSetAttrValue string) string {
return originalCoverUrl
}

// parseBookInfoNodeValue parses the value of the book info node
// Returns author, publisher and date published.
// parseBookInfoNodeValue parses the value of the book info node.
// Returns author and date published if found.
// See test for expected value format.
func parseBookInfoNodeValue(bookInfoNodeValue string) (string, *time.Time) {
// Book info parts are separated by | the of which is the author
// Book info parts are separated by a |
// Split the parts and sanitise them.
bookInfoParts := strings.Split(bookInfoNodeValue, "|")
bookAuthorPart := strings.TrimSpace(bookInfoParts[0])
publishDatePart := strings.TrimSpace(bookInfoParts[2])

// Strip out the "by" from the author part
bookAuthorFields := strings.Fields(bookAuthorPart)
if len(bookAuthorFields) > 1 && strings.EqualFold(bookAuthorFields[0], "by") {
bookAuthorFields = bookAuthorFields[1:]
for idx, part := range bookInfoParts {
bookInfoParts[idx] = strings.TrimSpace(part)
}

// Rejoin author fields
bookAuthor := strings.Join(bookAuthorFields, " ")

// Parse the date string according to the defined layout
// Get author and publish date from info parts.
var author string
var publishDate *time.Time
parsedPublishDate, err := time.Parse(publishDateLayout, publishDatePart)
if err == nil {
publishDate = &parsedPublishDate
for _, part := range bookInfoParts {
partFields := strings.Fields(part)

// Attempt to get author from part
// Author is in a part beginning with "by".
if len(partFields) > 1 && strings.EqualFold(partFields[0], "by") {
author = strings.Join(partFields[1:], " ")
continue
}

// Attempt to get publish date from part
// Publish date is in a part that can be parsed as a date
parsedPublishDate, err := time.Parse(publishDateLayout, part)
if err == nil {
publishDate = &parsedPublishDate
continue
}
}

return bookAuthor, publishDate
return author, publishDate
}

0 comments on commit 2e4e5ad

Please sign in to comment.