-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
196 lines (171 loc) · 3.86 KB
/
main.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"archive/zip"
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"math"
"os"
"path/filepath"
"sort"
"strings"
"github.com/juruen/rmapi/annotations"
"github.com/juruen/rmapi/archive"
)
func doJson(input, output, token string) error {
jsn, err := jsonannotations(input)
if err != nil {
return err
}
if token != "" {
SendRequest(jsn, token)
} else {
if output == "" {
nameOnly := strings.TrimSuffix(input, filepath.Ext(input))
output = nameOnly + ".json"
}
return ioutil.WriteFile(output, jsn, 0700)
}
return nil
}
func main() {
inputName := flag.String("i", "", "file to convert")
outputName := flag.String("o", "", "outpufilename")
extract := flag.String("e", "", "extract, a - annotations, p - pdf (default)")
format := flag.String("f", "json", "format (json, txt)")
token := flag.String("t", "", "readwise token")
flag.Parse()
var err error
switch *extract {
case "a":
if *format == "json" {
err = doJson(*inputName, *outputName, *token)
} else {
err = txtannotations(*inputName, *outputName)
}
case "", "p":
err = convert(*inputName, *outputName)
}
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func jsonannotations(inputName string) (output []byte, err error) {
file, err := os.Open(inputName)
if err != nil {
return
}
defer file.Close()
zip := archive.NewZip()
fi, err := file.Stat()
if err != nil {
return
}
err = zip.Read(file, fi.Size())
if err != nil {
return
}
rw := ReadWise{}
docId := zip.UUID
nameOnly := strings.TrimSuffix(inputName, filepath.Ext(inputName))
for index, p := range zip.Pages {
if p.Highlights == nil {
continue
}
high := p.Highlights.Highlights[0]
sort.Slice(high, func(i, j int) bool {
y1 := high[i].Rects[0].Y
y2 := high[j].Rects[0].Y
x1 := high[i].Rects[0].X
x2 := high[j].Rects[0].X
if math.Abs(y1-y2) < 5 {
return x1 < x2
}
return y1 < y2
})
for _, h := range high {
rw.Highlights = append(rw.Highlights,
Highlight{
DocumentID: docId,
Text: h.Text,
Title: nameOnly,
LocationType: "page",
Location: index + 1,
})
}
}
return json.Marshal(rw)
}
func txtannotations(inputName, outputName string) error {
if outputName == "" {
nameOnly := strings.TrimSuffix(inputName, filepath.Ext(inputName))
outputName = nameOnly + ".txt"
}
file, err := os.Open(inputName)
if err != nil {
return err
}
defer file.Close()
zip := archive.NewZip()
fi, err := file.Stat()
if err != nil {
return err
}
err = zip.Read(file, fi.Size())
if err != nil {
return err
}
f, err := os.Open(outputName)
if err != nil {
return err
}
defer f.Close()
for index, p := range zip.Pages {
if p.Highlights == nil {
continue
}
f.WriteString(fmt.Sprintf("Page %d\n", index))
high := p.Highlights.Highlights[0]
sort.Slice(high, func(i, j int) bool {
y1 := high[i].Rects[0].Y
y2 := high[j].Rects[0].Y
x1 := high[i].Rects[0].X
x2 := high[j].Rects[0].X
if math.Abs(y1-y2) < 5 {
return x1 < x2
}
return y1 < y2
})
for _, h := range high {
f.WriteString(fmt.Sprintf(" X:%d Y:%d\t %s\n", int(h.Rects[0].X), int(h.Rects[0].Y), h.Text))
}
}
return nil
}
func convert(inputName, outputName string) (err error) {
if inputName == "" {
return errors.New("missing input file")
}
if outputName == "" {
nameOnly := strings.TrimSuffix(inputName, filepath.Ext(inputName))
outputName = nameOnly + ".pdf"
}
outputFile, err := os.Create(outputName)
if err != nil {
return fmt.Errorf("can't create outputfile %w", err)
}
defer outputFile.Close()
reader, err := zip.OpenReader(inputName)
if err != nil {
return fmt.Errorf("can't open file %w", err)
}
defer reader.Close()
options := annotations.PdfGeneratorOptions{
AllPages: true,
}
gen := annotations.CreatePdfGenerator(inputName, outputName, options)
return gen.Generate()
}