-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdfbook-renamer.go
114 lines (101 loc) · 2.63 KB
/
pdfbook-renamer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/dmikoss/pdfbook-renamer/isbn"
"github.com/klippa-app/go-pdfium"
"github.com/klippa-app/go-pdfium/webassembly"
)
const pagesToScan = 15
func main() {
folderPath := flag.String("folder", "", "Path to the folder")
flag.Parse()
if *folderPath == "" {
log.Fatalf("Please provide a folder path with pdfs using the -folder flag")
}
absFolderPath, err := filepath.Abs(*folderPath)
if err != nil {
log.Fatalf("Error getting the absolute path of the folder:%v", err)
}
if err := run(absFolderPath); err != nil {
log.Fatalf("Fatal error %s", err.Error())
}
}
type pdfengine struct {
instance pdfium.Pdfium
pool pdfium.Pool
}
func NewPdfEngine() (*pdfengine, error) {
// initialize pdfium
pool, err := webassembly.Init(webassembly.Config{
MinIdle: 1, // Makes sure that at least x workers are always available
MaxIdle: 1, // Makes sure that at most x workers are ever available
MaxTotal: 1, // Maxium amount of workers in total, allows the amount of workers to grow when needed, items between total max and idle max are automatically cleaned up, while idle workers are kept alive so they can be used directly.
})
if err != nil {
return nil, err
}
instance, err := pool.GetInstance(time.Second * 30)
if err != nil {
return nil, err
}
return &pdfengine{instance, pool}, nil
}
func (p *pdfengine) Destroy() {
p.instance.Close()
p.pool.Close()
}
func run(folder string) error {
// initialize pdfium (with wasm runtime)
pdfengine, err := NewPdfEngine()
if err != nil {
return err
}
defer pdfengine.Destroy()
// get pdf file list
var pdflist []string
err = filepath.Walk(folder,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !strings.Contains(info.Name(), ".pdf") || info.IsDir() {
return nil
}
pdflist = append(pdflist, path)
return nil
})
if err != nil {
return err
}
// pdf processing
fetcher := isbn.NewProviderGoogleBooks(http.DefaultClient)
for _, path := range pdflist {
isbn, err := isbn.FindPdfISBN(path, pagesToScan, pdfengine.instance)
if err != nil || isbn == "" {
continue
} else {
info, err := fetcher.Fetch(context.Background(), isbn)
if err != nil {
continue
}
if info.Title != "" && len(info.Authors) > 0 {
dir := filepath.Dir(path)
renamed := fmt.Sprintf("%v - %v - %d.pdf", info.Title, info.Authors[0], info.YearOfPublish)
dstpath := filepath.Join(dir, renamed)
if err := os.Rename(path, dstpath); err != nil {
continue
}
}
fmt.Println(info)
}
}
return err
}