-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexport-kobo.py
executable file
·690 lines (619 loc) · 23.3 KB
/
export-kobo.py
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
import argparse
import datetime
import csv
import io
import os
import sqlite3
import sys
import json
DAYS = [
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday",
]
MONTHS = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
]
# Main BookManager object
book_manager = None
class CommandLineTool(object):
"""
A class providing a generic command line tool,
with the associated functions, error reporting, etc.
It is based on ``argparse``.
"""
# overload in the actual subclass
#
AP_PROGRAM = sys.argv[0]
AP_DESCRIPTION = "Generic Command Line Tool"
AP_ARGUMENTS = [
# required args
# {"name": "foo", "nargs": 1, "type": str, "default": "baz", "help": "Foo help"},
#
# optional args
# {"name": "--bar", "nargs": "?", "type": str,, "default": "foofoofoo", "help": "Bar help"},
# {"name": "--quiet", "action": "store_true", "help": "Do not output to stdout"},
]
def __init__(self):
self.parser = argparse.ArgumentParser(
prog=self.AP_PROGRAM,
description=self.AP_DESCRIPTION
)
self.vargs = None
for arg in self.AP_ARGUMENTS:
if "action" in arg:
self.parser.add_argument(
arg["name"],
action=arg["action"],
help=arg["help"]
)
else:
self.parser.add_argument(
arg["name"],
nargs=arg["nargs"],
type=arg["type"],
default=arg["default"],
help=arg["help"]
)
def run(self):
"""
Run the command line tool.
"""
self.vargs = vars(self.parser.parse_args())
self.run_command()
sys.exit(0)
def run_command(self):
"""
The actual command to be run.
This function is meant to be overridden in an actual subclass.
"""
self.print_stdout("This script does nothing. Invoke another .py")
def error(self, message):
"""
Print an error and exit with exit code 1.
"""
self.print_stderr("ERROR: {}".format(message))
sys.exit(1)
def print_stdout(self, *args, **kwargs):
"""
Print to standard out.
"""
print(*args, **kwargs)
def print_stderr(self, *args, **kwargs):
"""
Print to standard error.
"""
print(*args, file=sys.stderr, **kwargs)
class Item(object):
"""
A class representing one of: annotation, bookmark, or highlight.
It is basically a named tuple, with some extra functions to
format the contents.
"""
ANNOTATION = "annotation"
BOOKMARK = "bookmark"
HIGHLIGHT = "highlight"
def __init__(self, values, book):
self.volumeid = values[0]
self.bookmarkid = values[9]
self.text = values[1].strip().rstrip() if values[1] else None
self.annotation = values[2]
self.datecreated = values[3] if values[3] is not None else "1970-01-01T00:00:00.000"
self.datemodified = values[4] if values[4] is not None else "1970-01-01T00:00:00.000"
self.booktitle = book.title
self.chapter = values[7]
self.author = book.author
self.kind = self.BOOKMARK
self.dateformatted = self.format_date()
if (self.text is not None) and (self.text != "") and (self.annotation is not None) and (self.annotation != ""):
self.kind = self.ANNOTATION
elif (self.text is not None) and (self.text != ""):
self.kind = self.HIGHLIGHT
def csv_tuple(self):
"""
Return a tuple representing this Item, for CSV output purposes.
"""
return (self.kind, self.booktitle, self.author, self.chapter, self.datecreated, self.datemodified, self.annotation, self.text)
def markdown(self, last_entry=None, add_chapter_headings=False):
"""
Output markdown item contains only highlights and notes.
"""
if self.kind == self.ANNOTATION:
if add_chapter_headings and (last_entry is None or last_entry.chapter != self.chapter):
output = f"\n### {self.chapter}\n\n"
output += "- {}\n\n *{}*\n\n".format(self.text, self.annotation)
else:
output = "- {}\n\n *{}*\n\n".format(self.text, self.annotation)
elif self.kind == self.HIGHLIGHT:
if add_chapter_headings and (last_entry is None or last_entry.chapter != self.chapter):
#output = "- {}\n\n".format(self.text)
output = f"\n### {self.chapter}\n\n - {self.text}\n"
else:
output = "- {}\n".format(self.text)
else:
output = ""
return output
def format_date(self):
d = "Thursday, 1 January 1970 00:00:00"
try:
p1, p2 = self.datecreated.split("T")
year, month, day = [int(x) for x in p1.split("-")]
hour, minute, second = [int(float(x)) for x in p2.split(":")]
sday = DAYS[datetime.datetime(year=year, month=month, day=day).weekday()]
smonth = MONTHS[month - 1]
# e.g. "Friday, 19 December 2014 19:54:11"
d = "{}, {} {} {} {:02d}:{:02d}:{:02d}".format(sday, day, smonth, year, hour, minute, second)
except:
pass
return d
def kindle_my_clippings(self):
"""
Return a string representing this Item, in the Kindle "My Clippings" format.
"""
date = self.format_date()
output = []
output.append("{} ({})".format(self.title, self.author))
if self.kind == self.ANNOTATION:
output.append("- Your Note on page {} | location {} | Added on {}".format(1, 1, date))
output.append("")
output.append(self.annotation)
elif self.kind == self.HIGHLIGHT:
output.append("- Your Highlight on page {} | location {} | Added on {}".format(1, 1, date))
output.append("")
output.append(self.text)
else:
output.append("- Your Bookmark on page {} | location {} | Added on {}".format(1, 1, date))
output.append("")
output.append("==========")
return "\n".join(output)
def __repr__(self):
return "({})".format(self.csv_tuple())
def __str__(self):
output = []
hsep = "\n=== === ===\n"
asep = "\n### ### ###\n"
date = self.format_date()
if self.kind == self.ANNOTATION:
output.append("Type: {}".format(self.kind))
output.append("Title: {}".format(self.booktitle))
output.append("Author: {}".format(self.author))
output.append("Chapter: {}".format(self.chapter))
output.append("Date created: {}".format(date))
output.append("Annotation: {}{}{}".format(asep, self.annotation, asep))
output.append("Reference text: {}{}{}".format(hsep, self.text, hsep))
if self.kind == self.HIGHLIGHT:
output.append("Type: {}".format(self.kind))
output.append("Title: {}".format(self.booktitle))
output.append("Author: {}".format(self.author))
output.append("Chapter: {}".format(self.chapter))
output.append("Date created: {}".format(date))
output.append("Reference text: {}{}{}".format(hsep, self.text, hsep))
return "\n".join(output)
class Book(object):
"""
A class representing a book.
It is basically a named tuple, with some extra functions to
format the contents.
"""
def __init__(self, values):
self.volumeid = values[0]
self.title = values[1]
self.author = values[2]
self.itemscount = values[3]
def __repr__(self):
return "({}, {}, {})".format(self.volumeid, self.title, self.author)
def __str__(self):
return self.__repr__()
def to_markdown(self):
return "# {}\n## by {}\n---\n\n".format(self.title, self.author)
class ExportKobo(CommandLineTool):
"""
The actual command line tool to export
annotations, bookmarks, and highlights
from a Kobo SQLite file.
"""
AP_PROGRAM = "export-kobo"
AP_DESCRIPTION = "Export annotations and highlights from a Kobo SQLite file."
AP_ARGUMENTS = [
{
"name": "db",
"nargs": None,
"type": str,
"default": None,
"help": "Path of the input KoboReader.sqlite file"
},
{
"name": "--ui",
"action": "store_true",
"help": "Start a web server to navigate the books"
},
{
"name": "--output",
"nargs": "?",
"type": str,
"default": None,
"help": "Output to file instead of using the standard output"
},
{
"name": "--csv",
"action": "store_true",
"help": "Output data in CSV format"
},
{
"name": "--markdown",
"action": "store_true",
"help": "Output in Markdown list format"
},
{
"name": "--json",
"action": "store_true",
"help": "Output data in JSON format"
},
{
# "name": "--group-by-chapter",
"name": "--add-chapter-headings",
"action": "store_true",
"help": "Add the chapter headings to the output (markdown only)."
},
{
"name": "--kindle",
"action": "store_true",
"help": "Output in Kindle 'My Clippings.txt' format"
},
{
"name": "--list",
"action": "store_true",
"help": "List the titles of books with annotations or highlights"
},
{
"name": "--book",
"nargs": "?",
"type": str,
"default": None,
"help": "Output annotations and highlights only from the book with the given title"
},
{
"name": "--bookid",
"nargs": "?",
"type": str,
"default": None,
"help": "Output annotations and highlights only from the book with the given ID"
},
{
"name": "--annotations-only",
"action": "store_true",
"help": "Outputs annotations only, excluding highlights"
},
{
"name": "--highlights-only",
"action": "store_true",
"help": "Outputs highlights only, excluding annotations"
},
{
"name": "--info",
"action": "store_true",
"help": "Print information about the number of annotations and highlights"
},
{
"name": "--raw",
"action": "store_true",
"help": "Output in raw text instead of human-readable format"
},
]
QUERY_DB_VERSION = "SELECT version FROM DbVersion;"
# Query items: get all books items (highlights and annotation) using VolumeID.
#
# There's a problem for new DB version (175):
# ContentID gets different for some reason between Bookmark and Content.
# I'm not sure if this is a problem due to some annotation migration or is
# a bug into the db of Kobo Color model.
# Once confirmed please fix and use a single query if possible.
QUERY_ITEMS_V175 = """
SELECT
b.VolumeID,
b.Text,
b.Annotation,
b.DateCreated,
b.DateModified,
b.ChapterProgress,
c.BookTitle,
c.Title as Chapter,
c.Attribution as Author,
b.BookmarkID
FROM Bookmark b INNER JOIN content c
ON b.VolumeID = c.BookID
GROUP BY b.DateCreated
ORDER BY b.ChapterProgress ASC, b.DateCreated ASC;
"""
QUERY_ITEMS_V174 = """
SELECT
b.VolumeID,
b.Text,
b.Annotation,
b.DateCreated,
b.DateModified,
b.ChapterProgress,
c.BookTitle,
c.Title as Chapter,
c.Attribution as Author,
b.BookmarkID
FROM Bookmark b LEFT JOIN content c
ON b.ContentID = c.ContentID
GROUP BY b.DateCreated
ORDER BY b.ChapterProgress ASC, b.DateCreated ASC;
"""
QUERY_BOOKS = """
SELECT DISTINCT
b.VolumeID,
c.Title,
c.Attribution as Author,
(SELECT COUNT(*) FROM Bookmark b2 WHERE b2.VolumeID = b.VolumeID) AS Items
FROM
Bookmark b
INNER JOIN content c ON b.VolumeID = c.ContentID
ORDER BY
c.Title;
"""
def __init__(self):
super(ExportKobo, self).__init__()
self.books = []
self.items = []
self.db_version = 0
def run_command(self):
"""
The main function of the tool:
1. parse the parameters,
2. read the given SQLite file
3. format/output data as requested.
"""
if self.vargs["db"] is None:
self.error("You must specify a valid path to your KoboReader.sqlite file.")
# read db version
self.read_db_version()
# read list of books from db
dict_books, enum_books = self.read_books()
if self.vargs["ui"]:
self.run_server()
else:
if self.vargs["list"]:
# export: list of books only
output = []
output.append(("ID", "AUTHOR", "TITLE"))
for (i, book) in enum_books:
output.append((i, book.author, book.title))
if self.vargs["json"]:
output = json.dumps(list(dict_books.values()), default=lambda o: o.__dict__, indent=2)
elif self.vargs["csv"]:
output = self.list_to_csv(output)
else:
frmt = lambda v: "{}\t{:30}\t{}".format(v[0], v[1] or "None", v[2] or "None")
output = "\n".join([frmt(v) for v in output])
else:
# export: annotations and/or highlights
self.read_items(dict_books, enum_books)
if self.vargs["kindle"]:
output = "\n".join([i.kindle_my_clippings() for i in self.items])
elif self.vargs["json"]:
output = json.dumps(self.items, default=lambda o: o.__dict__, indent=2)
elif self.vargs["csv"]:
output = self.list_to_csv([i.csv_tuple() for i in self.items])
elif self.vargs["markdown"]:
output = self.list_to_markdown(enum_books)
elif self.vargs["raw"]:
output = "\n".join([("{}\n".format(i.text)) for i in self.items])
else:
# human-readable format
output = "\n".join([("{}\n".format(i)) for i in self.items])
if self.vargs["output"] is not None:
# write to file
try:
with io.open(self.vargs["output"], "w", encoding="utf-8") as f:
f.write(output)
except IOError:
self.error("Unable to write output file. Please check that the path is correct and that you have write permissions.")
elif self.vargs["info"]:
# Print some info about the extraction
self.print_stdout(f"Books with notes: {len(enum_books)}")
if not self.vargs["list"]:
total_highlights = len([i for i in self.items if i.kind == Item.HIGHLIGHT])
self.print_stdout(
f"Total highlights: {total_highlights}\n"
f"Total annotations: {len(self.items) - total_highlights}"
)
else:
# write to stdout
try:
self.print_stdout(output)
except UnicodeEncodeError:
self.print_stdout(output.encode("ascii", errors="replace"))
def read_db_version(self):
"""
Query the database version and store it as an integer in self.version.
"""
result = self.query(self.QUERY_DB_VERSION)
self.db_version = int(result[0][0])
def get_books(self):
"""
Returns a list of tuple, with volumeid and Book instance.
"""
if not self.books:
self.books = [(d[0], Book(d)) for d in self.query(self.QUERY_BOOKS)]
return self.books
def get_book_by_id(self, bookid):
"""
Returns a book by it's book id.
"""
return self.books[int(bookid) - 1][1]
def get_book_with_items_by_index(self, book_idx):
"""
Returns a book by index with its items formatted, in a tuple.
"""
try:
book = self.books[book_idx][1]
filtered_items = [i for i in self.items if i.volumeid == book.volumeid]
return (book, filtered_items)
except Exception:
self.error("Book not found at index.")
def run_server(self):
"""
Starts the flask server.
"""
from flask import Flask, g, render_template
app = Flask(__name__)
@app.before_request
def before_request():
g.book_manager = book_manager
@app.route('/')
def index():
"""
Index page displays only the list of books.
"""
books = [x[1] for x in g.book_manager.get_books()]
return render_template('index.html', books=books)
@app.route('/book/<int:book_id>')
def book_details(book_id):
"""
When user click on a book, show all the information displayed.
"""
books = [x[1] for x in g.book_manager.get_books()]
(book, items) = g.book_manager.get_book_with_items_by_index(book_id)
if book and items:
return render_template('index.html', books=books, book=book, book_items=items)
else:
return "Book not found."
app.run(port=5001)
def list_to_markdown(self, books):
"""
Convert the given Item data into a well-formed Markdown string.
"""
output = ""
book = self.current_book(books)
if book is None:
# no books specified, so print list of books
for (idx, b) in books:
if idx != 0:
output += "\n\n"
output += b.to_markdown()
# filter items of the current book
filtered_items = [i for i in self.items if i.volumeid == b.volumeid]
output += "".join([i.markdown() for i in filtered_items])
else:
output += book.to_markdown()
if not self.vargs["add_chapter_headings"]:
output += "".join([i.markdown() for i in self.items])
else:
last_entry = None
for i in self.items:
output += i.markdown(last_entry=last_entry, add_chapter_headings=True)
last_entry = i
return output
def list_to_csv(self, data):
"""
Convert the given Item data into a well-formed CSV string.
"""
output = io.StringIO()
writer = csv.writer(output)
for d in data:
try:
writer.writerow(d)
except UnicodeEncodeError:
writer.writerow(tuple([(v.encode("ascii", errors="replace") if v is not None else "") for v in d]))
return output.getvalue()
def read_books(self):
"""
Return the list of books into two formats:
a dict with ``{volumeId: Book}``,
a list of pairs ``(int, Book)`` with the index starting at one.
"""
books = self.get_books()
ids, books = zip(*books)
return dict(zip(ids, books)), list(enumerate(books, start=1))
def volumeid_from_bookid(self, books):
"""
Get the correct ``volumeid`` from the ``bookid``,
that is, the index of the book
as produced by the ``enumerate_books()``.
"""
bookid = self.vargs["bookid"]
try:
return books[int(bookid) - 1][1].volumeid
except:
self.error("The bookid value must be an integer between 1 and {}".format(len(books)))
def current_book(self, books):
"""
Returns the current book.
"""
bookid, booktitle = self.vargs["bookid"], self.vargs["book"]
if (bookid is None) and (booktitle is not None):
return None
if bookid is not None:
return self.get_book_by_id(bookid)
if booktitle is not None:
return filter(lambda i, b: b.title == booktitle, books)[0]
def read_items(self, dict_books, enum_books):
"""
Query the SQLite file, filtering Item objects as specified
by the user.
This function modifies the object's state by setting self.items.
"""
# Creating a new Item with the relative Book for extract title+author coming from the other table
items = []
print(self.db_version)
db_query = self.QUERY_ITEMS_V175 if self.db_version and self.db_version == 175 else self.QUERY_ITEMS_V174
for item in self.query(db_query):
volumeId = item[0]
book = dict_books.get(volumeId)
items.append(Item(item, book))
if len(items) == 0:
return items
if (self.vargs["bookid"] is not None) and (self.vargs["book"] is not None):
self.error("You cannot specify both --book and --bookid.")
if self.vargs["bookid"] is not None:
items = [i for i in items if i.volumeid == self.volumeid_from_bookid(enum_books)]
if self.vargs["book"] is not None:
items = [i for i in items if i.title == self.vargs["book"]]
if self.vargs["highlights_only"]:
items = [i for i in items if i.kind == Item.HIGHLIGHT]
if self.vargs["annotations_only"]:
items = [i for i in items if i.kind == Item.ANNOTATION]
# Set items into the object
self.items = items
def query(self, query, fetchone=False):
"""
Run the given query over the SQLite file.
"""
db_path = self.vargs["db"]
if not os.path.exists(db_path):
self.error("Unable to read KoboReader.sqlite file. Please check that the path is correct and that you have permission to read it.")
try:
sql_connection = sqlite3.connect(db_path)
sql_cursor = sql_connection.cursor()
sql_cursor.execute(query)
if fetchone:
data = sql_cursor.fetchone()
else:
data = sql_cursor.fetchall()
sql_cursor.close()
sql_connection.close()
except Exception as exc:
self.error("Unexpected error reading your KoboReader.sqlite file: {}".format(exc))
# NOTE the values are Unicode strings (str on python3) hence data is a list of tuples of Unicode strings
return data
if __name__ == "__main__":
book_manager = ExportKobo()
book_manager.run()