accorder/cmd/build.go

127 lines
3.9 KiB
Go
Raw Normal View History

2023-07-30 15:39:17 +00:00
package cmd
import (
"encoding/xml"
2023-07-30 15:39:17 +00:00
"fmt"
"log"
"os"
2023-07-30 15:39:17 +00:00
"accorder/pkg/calibre"
"github.com/beevik/etree"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
type FileAttachment struct {
XMLName xml.Name `xml:"resource"`
Path string `xml:"resource,attr"`
}
type ZoteroAttachment struct {
XMLName xml.Name `xml:"Attachment"`
ReferenceID string `xml:"about,attr"`
FileAttachment FileAttachment
FileType string `xml:"type"`
}
2023-07-30 15:39:17 +00:00
var buildCmd = &cobra.Command{
Use: "build",
Short: "Build standalone, portable webapp from Calibre library.",
Long: `Build searchable, standalone, portable webapp against the local Calibre
library including all the metadata needed. It creates BROWSE_LIBRARY.html in
root directory of the local Calibre library. For search (authors, titles,
tags...) it uses rendered metadata from static/data{1-8}.js files.
Every time the directory path and/or librarian is provided it is saved in
the configuration file for the future use (therefore: 'accorder build SESSION'
should be enough for the next successful build).`,
Args: cobra.ExactArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
session := args[0]
for vipFlag, cliFlag := range map[string]string{
"librarian_name": "librarian",
"local_upload": "directory",
"library_uuid": "library-uuid",
"library_secret": "library-secret",
"server_upload": "server",
"bucket_upload": "bucket",
} {
viper.BindPFlag(fmt.Sprintf("%s.%s", session, vipFlag), cmd.Flags().Lookup(cliFlag))
}
},
Run: func(cmd *cobra.Command, args []string) {
session := args[0]
MissingRequiredFlags(
map[string]string{
"librarian_name": "librarian",
"local_upload": "directory",
},
session,
cmd,
)
libraryUUID := ViperValue(session, "library_uuid")
librarySecret := ViperValue(session, "library_secret")
calibrePath := ViperValue(session, "local_upload")
librarianName := ViperValue(session, "librarian_name")
jsonPath := CliFlagValue(cmd, "jsonpath")
bibtexPath := CliFlagValue(cmd, "import-bibtex")
doc := etree.NewDocument()
if err := doc.ReadFromFile(bibtexPath); err != nil {
panic(err)
}
var zoteroAttachment ZoteroAttachment
2023-07-30 15:39:17 +00:00
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" {
continue
}
doc.WriteTo(os.Stdout)
b, err := doc.WriteToBytes()
if err != nil {
log.Fatal(err)
}
if err := xml.Unmarshal(b, &zoteroAttachment); err != nil {
log.Fatalln(err)
}
fmt.Printf("\nZOTEROATTACHMENT: %#v\n", zoteroAttachment)
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("~ ~ ~ ~")
2023-07-30 15:39:17 +00:00
}
calibre.RenderStandaloneApp(calibrePath, librarianName, libraryUUID, librarySecret, jsonPath)
},
}
func init() {
rootCmd.AddCommand(buildCmd)
buildCmd.Flags().StringP("directory", "d", "", "A local Calibre directory.")
buildCmd.Flags().StringP("librarian", "l", "", "Librarian's name.")
buildCmd.Flags().StringP("export-json", "e", "", "Path where to render all metadata into JSON.")
buildCmd.Flags().StringP("import-bibtex", "i", "", "Import books from BibTex file into Calibre.")
buildCmd.Flags().StringP("library-uuid", "u", "", "A library's UUID used if part of MotW.")
buildCmd.Flags().StringP("library-secret", "p", "", "A password used if part of MotW.")
buildCmd.Flags().StringP("server", "s", "minio.memoryoftheworld.org", "Server.")
buildCmd.Flags().StringP("bucket", "b", "", "Server.")
CustomHelpOutput(buildCmd)
buildCmd.Flags().MarkHidden("library-uuid")
buildCmd.Flags().MarkHidden("library-secret")
buildCmd.Flags().MarkHidden("server")
buildCmd.Flags().MarkHidden("bucket")
}