From c66a6c84997505b1cc1574e0039d3b29040ab49a Mon Sep 17 00:00:00 2001 From: Evan Reichard Date: Sat, 30 Dec 2023 10:18:43 -0500 Subject: [PATCH] [add] parse local isbn metadata --- metadata/epub.go | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/metadata/epub.go b/metadata/epub.go index b518ba8..b6e6a9d 100644 --- a/metadata/epub.go +++ b/metadata/epub.go @@ -1,6 +1,7 @@ package metadata import ( + "regexp" "strings" "github.com/PuerkitoBio/goquery" @@ -14,11 +15,33 @@ func getEPUBMetadata(filepath string) (*MetadataInfo, error) { } rf := rc.Rootfiles[0] - return &MetadataInfo{ + parsedMetadata := &MetadataInfo{ Title: &rf.Title, Author: &rf.Creator, Description: &rf.Description, - }, nil + } + + // Parse Possible ISBN + if rf.Source != "" { + replaceRE := regexp.MustCompile(`[-\s]`) + possibleISBN := replaceRE.ReplaceAllString(rf.Source, "") + + // ISBN Matches + isbn13RE := regexp.MustCompile(`(?P\d{13})`) + isbn10RE := regexp.MustCompile(`(?P\d{10})`) + isbn13Matches := isbn13RE.FindStringSubmatch(possibleISBN) + isbn10Matches := isbn10RE.FindStringSubmatch(possibleISBN) + + if len(isbn13Matches) > 0 { + isbnIndex := isbn13RE.SubexpIndex("ISBN") + parsedMetadata.ISBN13 = &isbn13Matches[isbnIndex] + } else if len(isbn10Matches) > 0 { + isbnIndex := isbn10RE.SubexpIndex("ISBN") + parsedMetadata.ISBN10 = &isbn10Matches[isbnIndex] + } + } + + return parsedMetadata, nil } func countEPUBWords(filepath string) (int64, error) {