more parsing of zotero's rdf..

This commit is contained in:
Marcell Mars 2022-04-04 13:46:55 +02:00
parent fac5a2b9b1
commit d0763e99a7
1 changed files with 70 additions and 26 deletions

View File

@ -4,7 +4,6 @@ import (
"encoding/xml"
"fmt"
"log"
"os"
"accorder/pkg/calibre"
@ -13,16 +12,23 @@ import (
"github.com/spf13/viper"
)
type FileAttachment struct {
XMLName xml.Name `xml:"resource"`
Path string `xml:"resource,attr"`
// best zotero rdf documentation:
// https://github.com/zotero/translators/blob/master/Zotero%20RDF.js
//
type ZoteroItem struct {
XMLName xml.Name `xml:"zoteroItem"`
FilePath string `xml:"filePath"`
MimeType string `xml:"mimeType"`
Publisher string `xml:"publisher,omitempty"`
Authors []ZoteroAuthor `xml:"authors"`
Title string `xml:"title"`
Description string `xml:"description,omitempty"`
}
type ZoteroAttachment struct {
XMLName xml.Name `xml:"Attachment"`
ReferenceID string `xml:"about,attr"`
FileAttachment FileAttachment
FileType string `xml:"type"`
type ZoteroAuthor struct {
XMLName xml.Name `xml:"authors"`
Author string `xml:"author"`
}
var buildCmd = &cobra.Command{
@ -74,32 +80,70 @@ should be enough for the next successful build).`,
if err := doc.ReadFromFile(bibtexPath); err != nil {
panic(err)
}
var zoteroAttachment ZoteroAttachment
root := doc.SelectElement("rdf:RDF")
for _, z := range root.FindElements("[name()='link:type']") {
doc := etree.NewDocument()
doc.SetRoot(z.Parent().Copy())
if z.Text() != "application/pdf" {
for _, attachmentNode := range root.FindElements("[name()='link:type']") {
var zoteroItem ZoteroItem
if attachmentNode.Text() != "application/pdf" {
continue
}
doc.WriteTo(os.Stdout)
b, err := doc.WriteToBytes()
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()
publisherQuery := bibliographyNode.FindElement("[name()='foaf:name']")
if publisherQuery != nil {
publisherElement := zoteroUnion.CreateElement("publisher")
publisher := publisherQuery.Text()
publisherElement.CreateText(publisher)
}
authorsQuery := bibliographyNode.FindElements("[name()='foaf:Person']")
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()
}
surNameNode := authorNode.FindElement("[name()='foaf:surname']")
if surNameNode != nil {
surName = surNameNode.Text()
}
author.CreateText(fmt.Sprintf("%s %s", firstName, surName))
}
titleQuery := bibliographyNode.FindElement("[name()='dc:title']")
if titleQuery != nil {
titleNode := zoteroUnion.CreateElement("title")
title := titleQuery.Text()
titleNode.CreateText(title)
}
descriptionQuery := bibliographyNode.FindElement("[name()='dcterms:abstract']")
if descriptionQuery != nil {
descriptionNode := zoteroUnion.CreateElement("description")
description := descriptionQuery.Text()
descriptionNode.CreateText(description)
}
// newDoc.WriteTo(os.Stdout)
b, err := newDoc.WriteToBytes()
if err != nil {
log.Fatal(err)
}
if err := xml.Unmarshal(b, &zoteroAttachment); err != nil {
if err := xml.Unmarshal(b, &zoteroItem); err != nil {
log.Fatalln(err)
}
fmt.Printf("\nZOTEROATTACHMENT: %#v\n", zoteroAttachment)
fmt.Printf("\nZoteroItem: %#v\n", zoteroItem)
fmt.Println(z.Text())
fmt.Println("~ ~ ~ ~")
fmt.Println(doc.WriteTo(os.Stdout))
elm := root.FindElement(fmt.Sprintf("[@rdf:resource='%s']", z.Parent().SelectAttr("rdf:about").Value))
doc.SetRoot(elm.Parent().Copy())
fmt.Println("+++++")
fmt.Println(doc.WriteTo(os.Stdout))
fmt.Println("~ ~ ~ ~")
}
calibre.RenderStandaloneApp(calibrePath, librarianName, libraryUUID, librarySecret, jsonPath)
},