-
Notifications
You must be signed in to change notification settings - Fork 35
/
seriesmeta.go
403 lines (359 loc) · 13.3 KB
/
seriesmeta.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// Command seriesmeta updates series metadata for EPUB/KEPUB books in the Kobo database.
package main
import (
"archive/zip"
"bytes"
"database/sql"
"errors"
"fmt"
"html/template"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/beevik/etree"
_ "github.com/mattn/go-sqlite3"
"github.com/pgaskin/koboutils/v2/kobo"
"github.com/spf13/pflag"
)
var version = "dev"
func main() {
noPersist := pflag.BoolP("no-persist", "p", false, "Don't ensure metadata is always set (this will cause series metadata to be lost if opening a book after an import but before a reboot)")
noReplace := pflag.BoolP("no-replace", "n", false, "Don't replace existing series metadata (you probably don't want this option)")
uninstall := pflag.BoolP("uninstall", "u", false, "Uninstall seriesmeta table and hooks (imported series metadata will be left untouched)")
help := pflag.BoolP("help", "h", false, "Show this help message")
pflag.Parse()
if *help || pflag.NArg() > 1 {
fmt.Fprintf(os.Stderr, "Usage: seriesmeta [options] [kobo_path]\n\nVersion:\n seriesmeta %s\n\nOptions:\n", version)
pflag.PrintDefaults()
fmt.Fprintf(os.Stderr, "\nArguments:\n kobo_path is the path to the Kobo eReader. If not specified, seriesmeta will try to automatically detect the Kobo.\n")
if pflag.NArg() > 1 {
os.Exit(2)
} else {
os.Exit(0)
}
return
}
fmt.Println("Note: You might be interested in NickelSeries (https://pgaskin.net/kepubify/ns), which will automatically import series and subtitle metadata along with the book itself.")
fmt.Println("Finding kobo")
var kp string
if pflag.NArg() == 1 {
kp = pflag.Arg(0)
} else {
kobos, err := kobo.Find()
if err != nil {
fmt.Fprintf(os.Stderr, "Could not automatically detect a Kobo eReader: %v.\n", err)
os.Exit(1)
} else if len(kobos) == 0 {
fmt.Fprintf(os.Stderr, "Could not automatically detect a Kobo eReader.\n")
os.Exit(1)
}
kp = kobos[0]
}
fmt.Println("Opening kobo")
k, err := OpenKobo(kp)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not open Kobo eReader: %v.\n", err)
os.Exit(1)
return
}
fmt.Println("Setting up database")
if err := k.SeriesConfig(*noReplace, *noPersist, *uninstall); err != nil {
fmt.Fprintf(os.Stderr, "Could not set up database: %v.\n", err)
os.Exit(1)
return
}
if *uninstall {
os.Exit(0)
return
}
fmt.Println("Updating metadata")
var nt, nu, ne, nn int
if err := k.UpdateSeries(func(filename string, i, total int, series string, index float64, err error) {
fmt.Printf("[%3d/%3d] %-40s %s\n", i+1, total, fmt.Sprintf("(%-34s %3v)", series, index), filename)
if err != nil {
fmt.Printf("--------- Error: %v\n", err)
ne++
} else if series == "" {
nn++
} else {
nu++
}
nt = total
}); err != nil {
fmt.Fprintf(os.Stderr, "Could not update metadata: %v.\n", err)
k.Close()
os.Exit(1)
return
}
fmt.Printf("%d total: %d updated, %d errored, %d without metadata\n", nt, nu, ne, nn)
if ne > 0 {
k.Close()
os.Exit(1)
return
}
k.Close()
}
// Kobo is a Kobo eReader.
type Kobo struct {
Path string
DB *sql.DB
}
// OpenKobo opens a Kobo eReader device and the database.
func OpenKobo(path string) (*Kobo, error) {
if _, err := os.Stat(path); err != nil {
return nil, err
} else if _, err = os.Stat(filepath.Join(path, ".kobo")); err != nil {
return nil, fmt.Errorf("could not access .kobo directory, is this a Kobo eReader: %v", err)
}
path, err := filepath.Abs(path)
if err != nil {
return nil, err
}
db, err := sql.Open("sqlite3", filepath.Join(path, ".kobo", "KoboReader.sqlite"))
if err != nil {
return nil, fmt.Errorf("could not open KoboReader.sqlite: %w", err)
}
if err := db.Ping(); err != nil {
return nil, fmt.Errorf("could not open KoboReader.sqlite: %w", err)
}
return &Kobo{path, db}, nil
}
// Close closes the reader and the database.
func (k *Kobo) Close() error {
return k.DB.Close()
}
// SeriesConfig sets up the table and triggers for seriesmeta. noReplace prevents
// series metadata from seriesmeta from replacing existing metadata, and noPersist
// allows the series metadata to be changed by something else later. If uninstall
// is true, the table and triggers will be removed.
func (k *Kobo) SeriesConfig(noReplace, noPersist, uninstall bool) error {
buf := bytes.NewBuffer(nil)
template.Must(template.New("").Parse(`
{{if .Uninstall}}
DROP TABLE _seriesmeta;
{{else}}
CREATE TABLE IF NOT EXISTS _seriesmeta (
ImageId TEXT NOT NULL UNIQUE,
Series TEXT,
SeriesNumber TEXT,
PRIMARY KEY(ImageId)
);
{{end}}
/* Adding series metadata on import */
DROP TRIGGER IF EXISTS _seriesmeta_content_insert;
{{if not .Uninstall}}
CREATE TRIGGER _seriesmeta_content_insert
AFTER INSERT ON content WHEN
{{if .NoReplace}}(new.Series IS NULL) AND{{end}}
(new.ImageId LIKE "file____mnt_onboard_%") AND
(SELECT count() FROM _seriesmeta WHERE ImageId = new.ImageId)
BEGIN
UPDATE content
SET
Series = (SELECT Series FROM _seriesmeta WHERE ImageId = new.ImageId),
SeriesNumber = (SELECT SeriesNumber FROM _seriesmeta WHERE ImageId = new.ImageId),
/* Get the SeriesID from books from the Kobo Store (WorkId NOT NULL) where the series name matches, otherwise just use the series name as the SeriesID (https://www.mobileread.com/forums/showthread.php?p=3959768) */
SeriesID = coalesce((SELECT SeriesID FROM content WHERE Series = (SELECT Series FROM _seriesmeta WHERE ImageId = new.ImageId) AND WorkId NOT NULL AND SeriesID NOT NULL AND WorkId != "" AND SeriesID != "" LIMIT 1), (SELECT Series FROM _seriesmeta WHERE ImageId = new.ImageId))
WHERE ImageId = new.ImageId;
{{if .NoPersist}}DELETE FROM _seriesmeta WHERE ImageId = new.ImageId;{{end}}
END;
{{end}}
DROP TRIGGER IF EXISTS _seriesmeta_content_update;
{{if not .Uninstall}}
CREATE TRIGGER _seriesmeta_content_update
AFTER UPDATE ON content WHEN
{{if .NoReplace}}(new.Series IS NULL) AND{{end}}
(new.ImageId LIKE "file____mnt_onboard_%") AND
(SELECT count() FROM _seriesmeta WHERE ImageId = new.ImageId)
BEGIN
UPDATE content
SET
Series = (SELECT Series FROM _seriesmeta WHERE ImageId = new.ImageId),
SeriesNumber = (SELECT SeriesNumber FROM _seriesmeta WHERE ImageId = new.ImageId),
/* Get the SeriesID from books from the Kobo Store (WorkId NOT NULL) where the series name matches, otherwise just use the series name as the SeriesID (https://www.mobileread.com/forums/showthread.php?p=3959768) */
SeriesID = coalesce((SELECT SeriesID FROM content WHERE Series = (SELECT Series FROM _seriesmeta WHERE ImageId = new.ImageId) AND WorkId NOT NULL AND SeriesID NOT NULL AND WorkId != "" AND SeriesID != "" LIMIT 1), (SELECT Series FROM _seriesmeta WHERE ImageId = new.ImageId))
WHERE ImageId = new.ImageId;
{{if .NoPersist}}DELETE FROM _seriesmeta WHERE ImageId = new.ImageId;{{end}}
END;
{{end}}
DROP TRIGGER IF EXISTS _seriesmeta_content_delete;
{{if not .Uninstall}}
CREATE TRIGGER _seriesmeta_content_delete
AFTER DELETE ON content
BEGIN
DELETE FROM _seriesmeta WHERE ImageId = old.ImageId;
END;
{{end}}
/* Adding series metadata directly when already imported */
{{if not .Uninstall}}
DROP TRIGGER IF EXISTS _seriesmeta_seriesmeta_insert;
CREATE TRIGGER _seriesmeta_seriesmeta_insert
AFTER INSERT ON _seriesmeta WHEN
(SELECT count() FROM content WHERE ImageId = new.ImageId)
{{if .NoReplace}}AND ((SELECT Series FROM content WHERE ImageId = new.ImageId) IS NULL){{end}}
BEGIN
UPDATE content
SET
Series = new.Series,
SeriesNumber = new.SeriesNumber,
/* Get the SeriesID from books from the Kobo Store (WorkId NOT NULL) where the series name matches, otherwise just use the series name as the SeriesID (https://www.mobileread.com/forums/showthread.php?p=3959768) */
SeriesID = coalesce((SELECT SeriesID FROM content WHERE Series = new.Series AND WorkId NOT NULL AND SeriesID NOT NULL AND WorkId != "" AND SeriesID != "" LIMIT 1), new.Series)
WHERE ImageId = new.ImageId;
{{if .NoPersist}}DELETE FROM _seriesmeta WHERE ImageId = new.ImageId;{{end}}
END;
{{end}}
DROP TRIGGER IF EXISTS _seriesmeta_seriesmeta_update;
{{if not .Uninstall}}
CREATE TRIGGER _seriesmeta_seriesmeta_update
AFTER UPDATE ON _seriesmeta WHEN
(SELECT count() FROM content WHERE ImageId = new.ImageId)
{{if .NoReplace}}AND ((SELECT Series FROM content WHERE ImageId = new.ImageId) IS NULL){{end}}
BEGIN
UPDATE content
SET
Series = new.Series,
SeriesNumber = new.SeriesNumber,
/* Get the SeriesID from books from the Kobo Store (WorkId NOT NULL) where the series name matches, otherwise just use the series name as the SeriesID (https://www.mobileread.com/forums/showthread.php?p=3959768) */
SeriesID = coalesce((SELECT SeriesID FROM content WHERE Series = new.Series AND WorkId NOT NULL AND SeriesID NOT NULL AND WorkId != "" AND SeriesID != "" LIMIT 1), new.Series)
WHERE ImageId = new.ImageId;
{{if .NoPersist}}DELETE FROM _seriesmeta WHERE ImageId = new.ImageId;{{end}}
END;
{{end}}
`)).Execute(buf, map[string]interface{}{
"NoReplace": noReplace,
"NoPersist": noPersist,
"Uninstall": uninstall,
})
_, err := k.DB.Exec(buf.String())
return err
}
// UpdateSeries updates the series metadata for all epub books on the device. All
// errors from individual books are returned through the log callback.
func (k *Kobo) UpdateSeries(log func(filename string, i, total int, series string, index float64, err error)) error {
var epubs []string
err := filepath.Walk(k.Path, func(path string, info os.FileInfo, err error) error {
if err != nil {
if path != k.Path {
fmt.Fprintf(os.Stderr, "Warning: Failed to scan %q: %v.\n", path, err)
} else {
return fmt.Errorf("error scanning %q: %w", path, err)
}
}
if !info.IsDir() && strings.EqualFold(filepath.Ext(path), ".epub") {
epubs = append(epubs, path)
}
return nil
})
if err != nil {
return err
}
relEpubs := make([]string, len(epubs))
for i, epub := range epubs {
relEpubs[i], err = filepath.Rel(k.Path, epub)
if err != nil {
return fmt.Errorf("could not resolve relative path to %#v: %w", epub, err)
}
}
tx, err := k.DB.Begin()
if err != nil {
return fmt.Errorf("could not begin db transaction: %w", err)
}
for i, epub := range epubs {
relEpub := relEpubs[i]
series, index, err := readEPUBSeriesInfo(epub)
if err != nil {
log(relEpub, i, len(epubs), series, index, err)
continue
}
if _, err := tx.Exec(
"INSERT OR REPLACE INTO _seriesmeta (ImageId, Series, SeriesNumber) VALUES (?, ?, ?)",
kobo.ContentIDToImageID(kobo.PathToContentID(relEpub)),
sql.NullString{String: series, Valid: len(series) > 0},
sql.NullString{String: strconv.FormatFloat(index, 'f', -1, 64), Valid: index > 0},
); err != nil {
log(relEpub, i, len(epubs), series, index, err)
continue
}
log(relEpub, i, len(epubs), series, index, nil)
}
if err := tx.Commit(); err != nil {
return fmt.Errorf("could not commit db transaction: %w", err)
}
return nil
}
// readEPUBSeriesInfo reads the series metadata from an epub book.
func readEPUBSeriesInfo(filename string) (series string, index float64, err error) {
zr, err := zip.OpenReader(filename)
if err != nil {
return "", 0, fmt.Errorf("could not open ebook: %w", err)
}
defer zr.Close()
var rootfile string
for _, f := range zr.File {
if strings.TrimLeft(strings.ToLower(f.Name), "/") == "meta-inf/container.xml" {
rc, err := f.Open()
if err != nil {
return "", 0, fmt.Errorf("could not open container.xml: %w", err)
}
doc := etree.NewDocument()
_, err = doc.ReadFrom(rc)
if err != nil {
rc.Close()
return "", 0, fmt.Errorf("could not parse container.xml: %w", err)
}
if el := doc.FindElement("//rootfiles/rootfile[@full-path]"); el != nil {
rootfile = el.SelectAttrValue("full-path", "")
}
rc.Close()
break
}
}
if rootfile == "" {
return "", 0, errors.New("could not open ebook: could not find package document")
}
for _, f := range zr.File {
if strings.TrimLeft(strings.ToLower(f.Name), "/") == strings.TrimLeft(strings.ToLower(rootfile), "/") {
rc, err := f.Open()
if err != nil {
return "", 0, fmt.Errorf("could not open container.xml: %w", err)
}
doc := etree.NewDocument()
_, err = doc.ReadFrom(rc)
if err != nil {
rc.Close()
return "", 0, fmt.Errorf("could not parse container.xml: %w", err)
}
// Calibre series metadata
if el := doc.FindElement("//meta[@name='calibre:series']"); el != nil {
series = el.SelectAttrValue("content", "")
if el := doc.FindElement("//meta[@name='calibre:series_index']"); el != nil {
index, _ = strconv.ParseFloat(el.SelectAttrValue("content", "0"), 64)
}
}
// EPUB3 series metadata
if series == "" {
if el := doc.FindElement("//meta[@property='belongs-to-collection']"); el != nil {
series = strings.TrimSpace(el.Text())
var ctype string
if id := el.SelectAttrValue("id", ""); id != "" {
for _, el := range doc.FindElements("//meta[@refines='#" + id + "']") {
val := strings.TrimSpace(el.Text())
switch el.SelectAttrValue("property", "") {
case "collection-type":
ctype = val
case "group-position":
index, _ = strconv.ParseFloat(val, 64)
}
}
}
if ctype != "" && ctype != "series" {
series, index = "", 0
}
}
}
break
}
}
return series, index, nil
}