AnthoLume/metadata/epub.go

40 lines
792 B
Go
Raw Permalink Normal View History

package metadata
import (
"strings"
"github.com/PuerkitoBio/goquery"
2023-10-24 00:18:16 +00:00
"github.com/taylorskalyo/goreader/epub"
)
func getEPUBMetadata(filepath string) (*MetadataInfo, error) {
rc, err := epub.OpenReader(filepath)
if err != nil {
return nil, err
}
rf := rc.Rootfiles[0]
return &MetadataInfo{
Title: &rf.Title,
Author: &rf.Creator,
Description: &rf.Description,
}, nil
}
2023-10-24 00:18:16 +00:00
func countEPUBWords(filepath string) (int64, error) {
rc, err := epub.OpenReader(filepath)
if err != nil {
2023-10-24 00:18:16 +00:00
return 0, err
}
2023-10-24 00:18:16 +00:00
rf := rc.Rootfiles[0]
var completeCount int64
for _, item := range rf.Spine.Itemrefs {
f, _ := item.Open()
doc, _ := goquery.NewDocumentFromReader(f)
completeCount = completeCount + int64(len(strings.Fields(doc.Text())))
}
2023-10-24 00:18:16 +00:00
return completeCount, nil
}