kdb/citing/cite.go

62 lines
1.7 KiB
Go
Raw Normal View History

2019-11-12 00:41:42 +00:00
package citing
2019-11-12 03:06:15 +00:00
import (
"crypto/sha512"
"encoding/base64"
"encoding/hex"
"git.openprivacy.ca/openprivacy/libricochet-go/log"
"git.openprivacy.ca/sarah/kdb/fetch"
"github.com/nickng/bibtex"
"io/ioutil"
"os"
"os/exec"
"path"
"strings"
)
2019-11-12 03:07:25 +00:00
func ExtractPDF(extract string) (string, string, string, *bibtex.BibEntry) {
2019-11-12 03:06:15 +00:00
log.Infof("Extracting..." + extract)
2019-11-12 03:07:25 +00:00
parts := strings.Split(extract, ";")
2019-11-12 03:06:15 +00:00
if len(parts) >= 3 {
args := strings.Split(parts[1], ":")
src := strings.Join(parts[2:], ":")
id := sha512.Sum512([]byte(src))
fetch.Fetch(src, hex.EncodeToString(id[:]))
cachepath := path.Join("cache/", hex.EncodeToString(id[:]))
2019-11-12 03:07:25 +00:00
args = append(args, "-singlefile", "-png", cachepath, "tmp")
2019-11-12 03:06:15 +00:00
subProcess := exec.Command("pdftoppm", args...)
log.Infof("Running.. %v", subProcess.Args, subProcess.String())
err := subProcess.Run()
output, _ := subProcess.CombinedOutput()
log.Infof("Success: %v %v", output, err)
data, _ := ioutil.ReadFile("tmp.png")
link := hex.EncodeToString([]byte(extract))
log.Infof("Extracted; %v", link)
bt := FindReference(src)
title := "From " + src
if bt != nil {
2019-11-12 03:07:25 +00:00
title = bt.Fields["title"].String() + " by " + bt.Fields["author"].String() + " (" + bt.Fields["year"].String() + ")"
2019-11-12 03:06:15 +00:00
}
2019-11-12 03:07:25 +00:00
return title, link, "<img class=\"source\" id=\"" + link + "\" src=\"data:image/png;base64, " + base64.StdEncoding.EncodeToString(data) + "\"/>", bt
2019-11-12 03:06:15 +00:00
}
2019-11-12 03:07:25 +00:00
return "", "", "", nil
2019-11-12 03:06:15 +00:00
}
2019-11-12 03:07:25 +00:00
func FindReference(url string) *bibtex.BibEntry {
file, err := os.Open("./data/references.kdb")
2019-11-12 03:06:15 +00:00
if err == nil {
2019-11-12 03:07:25 +00:00
bt, err := bibtex.Parse(file)
2019-11-12 03:06:15 +00:00
if err != nil {
log.Errorf("%v", err)
}
2019-11-12 03:07:25 +00:00
for _, e := range bt.Entries {
2019-11-12 03:06:15 +00:00
if e.Fields["url"].String() == url {
return e
}
}
}
return nil
}