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 5c728f7d9d
commit 4743e6f143
2 changed files with 50 additions and 38 deletions

View File

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

View File

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