if there's more than one attachment add it to the item...

This commit is contained in:
Marcell Mars 2022-04-21 02:14:07 +02:00
parent 103292cd38
commit 3a505ce022
2 changed files with 50 additions and 38 deletions

View File

@ -7,6 +7,7 @@ import (
"log" "log"
"os" "os"
"strconv" "strconv"
"strings"
"github.com/araddon/dateparse" "github.com/araddon/dateparse"
"github.com/beevik/etree" "github.com/beevik/etree"
@ -19,18 +20,20 @@ import (
// //
type ZoteroItem struct { type ZoteroItem struct {
XMLName xml.Name `xml:"zoteroItem"` XMLName xml.Name `xml:"zoteroItem"`
FilePath string `xml:"filePath"` Attachments struct {
MimeType string `xml:"mimeType"` Attachment []struct {
Publisher string `xml:"publisher,omitempty"` Path string `xml:"path"`
Authors []ZoteroAuthor `xml:"authors"` MimeType string `xml:"mimeType"`
Title string `xml:"title"` } `xml:"attachment"`
Description string `xml:"description,omitempty"` } `xml:"attachments"`
} Date string `xml:"date"`
Authors struct {
type ZoteroAuthor struct { Author []string `xml:"author"`
XMLName xml.Name `xml:"authors"` } `xml:"authors"`
Author string `xml:"author"` Title string `xml:"title"`
Description string `xml:"description"`
Publisher string `xml:"publisher"`
} }
var buildCmd = &cobra.Command{ var buildCmd = &cobra.Command{
@ -83,27 +86,35 @@ should be enough for the next successful build).`,
panic(err) panic(err)
} }
root := doc.SelectElement("rdf:RDF") root := doc.SelectElement("rdf:RDF")
attachmentsIDs := map[string]bool{}
for _, attachmentNode := range root.FindElements("[name()='link:type']") { for _, attachmentNode := range root.FindElements("[name()='link:type']") {
var zoteroItem ZoteroItem
var bookOpf calibre.BookOpfW var bookOpf calibre.BookOpfW
var zoteroItem ZoteroItem
if attachmentNode.Text() != "application/pdf" { attachmentID := attachmentNode.Parent().SelectAttr("rdf:about").Value
if attachmentNode.Text() == "text/html" || attachmentsIDs[attachmentID] {
continue continue
} }
bibliographyNode := root.FindElement(fmt.Sprintf("[@rdf:resource='%s']", attachmentID)).Parent().Copy()
newDoc := etree.NewDocument() newDoc := etree.NewDocument()
zoteroUnion := newDoc.CreateElement("zoteroItem") zoteroUnion := newDoc.CreateElement("zoteroItem")
filePathElement := zoteroUnion.CreateElement("filePath")
filePathQuery := attachmentNode.Parent().FindElement("[name()='rdf:resource']").SelectAttr("rdf:resource").Value
filePathElement.CreateText(filePathQuery)
mimeTypeElement := zoteroUnion.CreateElement("mimeType") attachmentsQuery := bibliographyNode.FindElements("[name()='link:link']")
mimeType := attachmentNode.Text() itemAttachments := zoteroUnion.CreateElement("attachments")
mimeTypeElement.CreateText(mimeType) for _, attachment := range attachmentsQuery {
attachmentID := attachment.SelectAttr("rdf:resource").Value
bibliographyNode := root.FindElement(fmt.Sprintf("[@rdf:resource='%s']", attachmentNode.Parent().SelectAttr("rdf:about").Value)).Parent().Copy() zAttachment := root.FindElement(fmt.Sprintf("[@rdf:about='%s']", attachmentID))
mimeType := zAttachment.FindElement("[name()='link:type']").Text()
// newDoc.AddChild(bibliographyNode) if mimeType != "text/html" {
// newDoc.WriteTo(os.Stdout) itemAttachment := itemAttachments.CreateElement("attachment")
attachmentsIDs[attachmentID] = true
filePath := zAttachment.FindElement("[name()='rdf:resource']").SelectAttr("rdf:resource").Value
itemAttachment.CreateElement("path").SetText(filePath)
itemAttachment.CreateElement("mimeType").SetText(mimeType)
}
}
dateQuery := bibliographyNode.FindElement("[name()='dc:date']") dateQuery := bibliographyNode.FindElement("[name()='dc:date']")
if dateQuery != nil { if dateQuery != nil {
@ -149,18 +160,17 @@ should be enough for the next successful build).`,
authors := zoteroUnion.CreateElement("authors") authors := zoteroUnion.CreateElement("authors")
for _, authorNode := range authorsQuery { for _, authorNode := range authorsQuery {
var firstName, surName string var firstName, surName string
author := authors.CreateElement("author")
firstNameNode := authorNode.FindElement("[name()='foaf:givenName']") firstNameNode := authorNode.FindElement("[name()='foaf:givenName']")
if firstNameNode != nil { if firstNameNode != nil {
firstName = firstNameNode.Text() firstName = firstNameNode.Text() + " "
} }
surNameNode := authorNode.FindElement("[name()='foaf:surname']") surNameNode := authorNode.FindElement("[name()='foaf:surname']")
if surNameNode != nil { if surNameNode != nil {
surName = surNameNode.Text() surName = surNameNode.Text() + " "
} }
fullName := fmt.Sprintf("%s %s", firstName, surName) fullName := strings.TrimSuffix(fmt.Sprintf("%s%s", firstName, surName), " ")
author.CreateText(fullName) authors.CreateElement("author").SetText(fullName)
bookOpf.Metadata.Creators = append(bookOpf.Metadata.Creators, calibre.Creator{ bookOpf.Metadata.Creators = append(bookOpf.Metadata.Creators, calibre.Creator{
Role: "aut", Role: "aut",
@ -186,16 +196,18 @@ should be enough for the next successful build).`,
bookOpf.Metadata.Description = description bookOpf.Metadata.Description = description
} }
// newDoc.WriteTo(os.Stdout)
// fmt.Println("")
b, err := newDoc.WriteToBytes() b, err := newDoc.WriteToBytes()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
if err := xml.Unmarshal(b, &zoteroItem); err != nil { if err := xml.Unmarshal(b, &zoteroItem); err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
zi, _ := xml.MarshalIndent(zoteroItem, " ", " ")
os.Stdout.Write(zi)
fmt.Println("\n~ ~ ~ ~ ~ ~")
// fmt.Printf("\nZoteroItem: %#v\n", zoteroItem) // fmt.Printf("\nZoteroItem: %#v\n", zoteroItem)
bookOpf.Version = "2.0" bookOpf.Version = "2.0"
@ -214,9 +226,9 @@ should be enough for the next successful build).`,
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
// _ = bookOpfOutput _ = bookOpfOutput
os.Stdout.Write(bookOpfOutput) //os.Stdout.Write(bookOpfOutput)
fmt.Println("") //fmt.Println("\n ~ ~ ~ ~ ~")
} }
calibre.RenderStandaloneApp(calibrePath, librarianName, libraryUUID, librarySecret, jsonPath) calibre.RenderStandaloneApp(calibrePath, librarianName, libraryUUID, librarySecret, jsonPath)
}, },

View File

@ -110,8 +110,8 @@ type BookOpfW struct {
Name string `xml:",chardata"` Name string `xml:",chardata"`
} `xml:"dc:creator"` } `xml:"dc:creator"`
Published string `xml:"dc:date"` Published string `xml:"dc:date"`
Description string `xml:"dc:description"` Description string `xml:"dc:description,omitempty"`
Publisher string `xml:"dc:publisher"` Publisher string `xml:"dc:publisher,omitempty"`
Languages []struct { Languages []struct {
Language string `xml:",chardata"` Language string `xml:",chardata"`
} `xml:"dc:language"` } `xml:"dc:language"`