-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathkindle.py
executable file
·94 lines (70 loc) · 1.92 KB
/
kindle.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import collections
import json
import os
import re
BOUNDARY = u"==========\r\n"
DATA_FILE = u"clips.json"
OUTPUT_DIR = u"output"
def get_sections(filename):
with open(filename, 'rb') as f:
content = f.read().decode('utf-8')
content = content.replace(u'\ufeff', u'')
return content.split(BOUNDARY)
def get_clip(section):
clip = {}
lines = [l for l in section.split(u'\r\n') if l]
if len(lines) != 3:
return
clip['book'] = lines[0]
match = re.search(r'(\d+)-\d+', lines[1])
if not match:
return
position = match.group(1)
clip['position'] = int(position)
clip['content'] = lines[2]
return clip
def export_txt(clips):
"""
Export each book's clips to single text.
"""
for book in clips:
lines = []
for pos in sorted(clips[book]):
lines.append(clips[book][pos].encode('utf-8'))
filename = os.path.join(OUTPUT_DIR, u"%s.md" % book)
with open(filename, 'wb') as f:
f.write("\n\n---\n\n".join(lines))
def load_clips():
"""
Load previous clips from DATA_FILE
"""
try:
with open(DATA_FILE, 'rb') as f:
return json.load(f)
except (IOError, ValueError):
return {}
def save_clips(clips):
"""
Save new clips to DATA_FILE
"""
with open(DATA_FILE, 'wb') as f:
json.dump(clips, f)
def main():
# load old clips
clips = collections.defaultdict(dict)
clips.update(load_clips())
# extract clips
sections = get_sections(u'My Clippings.txt')
for section in sections:
clip = get_clip(section)
if clip:
clips[clip['book']][str(clip['position'])] = clip['content']
# remove key with empty value
clips = {k: v for k, v in clips.items() if v}
# save/export clips
save_clips(clips)
export_txt(clips)
if __name__ == '__main__':
main()