-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathkindle2anki.py
executable file
·305 lines (254 loc) · 9.67 KB
/
kindle2anki.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
#!/usr/bin/env python
import card_creator
import sys
import argparse
import csv
import os
import re
import sqlite3
import datetime
import retrying
import service
import urllib
import urllib.parse
import urllib.request
import logging
import pyperclip
from colorama import init, Fore, Back, Style
init()
TIMESTAMP_PATH = os.path.expanduser('~/.kindle')
def get_lookups(db, timestamp=0):
conn = sqlite3.connect(db)
res = []
sql = """
SELECT w.stem,l.usage, w.timestamp
FROM `WORDS` as w
LEFT JOIN `LOOKUPS` as l
ON w.id=l.word_key where w.timestamp>""" + str(timestamp) + """;
"""
for row in conn.execute(sql):
res.append(row)
conn.close()
return res
def get_lookups_from_file(filename, last_timestamp=0, max_length=30):
TITLE_LINE = 0
CLIPPING_INFO = 1
CLIPPING_TEXT = 3
MOD = 5
words = []
infile = open(filename, 'r')
for line_num, x in enumerate(infile):
# trim \r\n from line
x = re.sub('[\r\n]', '', x)
# trim hex bytes at start if they're there
if x[:3] == '\xef\xbb\xbf':
x = x[3:]
# if we're at a title line and it doesn't match the last title
if line_num % MOD == TITLE_LINE:
title = x
elif line_num % MOD == CLIPPING_INFO:
# include metadata (location, time etc.) if desired
date = re.findall(
r'(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec).*\d\s(?:AM|PM)',
x)
timestamp = datetime.datetime.strptime(
date[0], "%B %d, %Y %I:%M:%S %p").timestamp() * 1000
logging.debug("timestamp: " + str(timestamp))
elif line_num % MOD == CLIPPING_TEXT:
# Skip trying to write if we have no body
if x == '':
continue
if ((last_timestamp == 0 or timestamp > last_timestamp) and
len(x) < max_length):
x = re.sub(',', '', x)
words.append([x, '', timestamp])
return words
def get_last_timestamp_from_lookup(db):
conn = sqlite3.connect(db)
res = conn.execute(
'select timestamp from WORDS order by timestamp desc limit 1;').fetchall(
)
conn.close()
last_timestamp = res[0][0] if len(res) > 0 else None
logging.debug("last timestamp from lookup: " + str(last_timestamp))
return last_timestamp
def get_last_timestamp():
try:
with open(TIMESTAMP_PATH, 'r') as tfile:
last_timestamp = int(float(tfile.readline().strip()))
logging.debug("last timestamp from file: " + str(last_timestamp))
return last_timestamp
except Exception as e:
logging.debug(e)
return 0
def update_last_timestamp(timestamp):
logging.debug("update timestamp: " + str(timestamp))
with open(TIMESTAMP_PATH, 'w') as tfile:
tfile.write('{}'.format(timestamp))
def translate(lingualeo, word):
result = lingualeo.get_translates(word)
sound_url = result['sound_url']
pic_url = result['translate'][0]['pic_url']
# tr = result['translate'][0]['value']
tr = [i['value'] for i in result['translate']][:3]
# remove duplicates
tr = '<br>'.join(list(set(tr)))
transcription = result['transcription']
return (tr, transcription, sound_url, pic_url)
def extract_filename_from_url(url):
path = urllib.parse.urlparse(url).path
return os.path.split(path)[-1]
@retrying.retry(stop_max_attempt_number=3)
def download_file(url, path=''):
res = urllib.request.urlretrieve(url, os.path.join(
path, extract_filename_from_url(url)))
return res
def write_to_csv(file, data):
with open(file, 'w', newline='') as csvfile:
spamwriter = csv.writer(
csvfile,
delimiter='\t',
dialect='unix',
quotechar='|',
quoting=csv.QUOTE_MINIMAL)
for row in data:
spamwriter.writerow(row)
def highlight_word_in_context(word, context):
return re.sub(r'{}'.format(word),
'<span class=highlight>{}</span>'.format(word), context)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'--vocab-db', help='Path to Kindle vocab DB file (usually "/system/vocabulary/vocab.db" on Kindle). Provide this either this or --clippings')
parser.add_argument(
'--clippings', help='Path to clippings (usually "/documents/My Clippings.txt" on Kindle)')
parser.add_argument('--deck', help='Anki deck name')
parser.add_argument(
'--update-timestamp',
help='Only update local timestamp to now and exit',
default=False,
action="store_true")
parser.add_argument(
'--no-ask',
help='Do not ask for card back in the command line',
default=False,
action="store_true")
parser.add_argument(
'--clipboard',
help='Copy each word to clipboard',
default=False,
action="store_true")
parser.add_argument(
'-o',
'--out',
help='CSV output filename to import into Anki, if not provided words are added to Anki using anki-connect')
parser.add_argument(
'-m',
'--media-path',
help='Where to store media files (sounds/images) from Lingualeo')
parser.add_argument('--email', help='LinguaLeo account email/login')
parser.add_argument('--pwd', help='LinguaLeo account password')
parser.add_argument(
'--max-length',
help='Maximum length of words from clippings, to avoid importing big sentences',
default=30)
parser.add_argument(
'--verbose',
help='Show debug messages',
default=False,
action="store_true")
args = parser.parse_args()
if (args.verbose):
logging.getLogger().setLevel(logging.DEBUG)
if (args.update_timestamp):
update_last_timestamp(datetime.datetime.now().timestamp() * 1000)
sys.exit(0)
media_path = args.media_path if args.media_path else ''
timestamp = get_last_timestamp()
lingualeo = False
if (args.email and args.pwd):
lingualeo = service.Lingualeo(email, password)
res = lingualeo.auth()
if args.vocab_db:
lookups = get_lookups(args.vocab_db, timestamp)
elif args.clippings:
lookups = get_lookups_from_file(
args.clippings, timestamp, args.max_length)
else:
logging.error("No input specified")
sys.exit(1)
card = card_creator.CardCreator(args.deck)
data = []
prev_timestamp = 0
for i, (word, context, timestamp) in enumerate(lookups):
progress = int(100.0 * i / len(lookups))
to_print = ('' + Style.DIM + '[{}%]' + Style.RESET_ALL + '\t \n'
'' + Fore.GREEN + 'Word: ' + Style.RESET_ALL + '{} \n'
'' + Fore.GREEN + 'Context:' + Style.RESET_ALL + ' {} \n')
print(to_print.format(progress, word, context), end='', flush=True)
if args.clipboard:
pyperclip.copy(word)
if lingualeo:
tr, transcription, sound_url, img_url = translate(lingualeo, word)
if sound_url:
print('ok, get sound...', end='', flush=True)
try:
sound, _ = download_file(sound_url, media_path)
sound = os.path.basename(sound)
except:
sound = ''
if img_url:
print('ok, get image...', end='', flush=True)
try:
img, _ = download_file(img_url, media_path)
img = os.path.basename(img)
except:
img = ''
print('ok!')
else:
desc = ''
if not args.no_ask:
print(Style.BRIGHT + "Enter card back:" + Style.RESET_ALL +
Style.DIM + '[q/s]' + Style.RESET_ALL)
desc = input()
if desc == 'q':
if prev_timestamp != 0:
update_last_timestamp(prev_timestamp)
if args.out:
print(
'[100%]\tWrite to file {}...'.format(args.out),
end='',
flush=True)
write_to_csv(args.out, data)
sys.exit(0)
if desc == 's':
print(
Style.DIM + "===============================================================================" + Style.RESET_ALL)
prev_timestamp = timestamp
continue
if not context:
context = ''
# remove all kinds of quotes/backticks as Anki sometimes has troubles
# with them
context = re.sub(r'[\'"`]', '', context)
context = highlight_word_in_context(word, context)
if args.out:
if lingualeo:
data.append((word, transcription, '[sound:{}]'.format(sound), tr,
img, highlight_word_in_context(word, context)))
else:
data.append((word, desc + "<br /><br />" +
highlight_word_in_context(word, context)))
else:
card.create(word, desc + "<br /><br />" + highlight_word_in_context(word, context))
print(Style.DIM + "===============================================================================" + Style.RESET_ALL)
prev_timestamp = timestamp
if len(lookups):
if args.out:
print(
'[100%]\tWrite to file {}...'.format(args.out),
end='',
flush=True)
write_to_csv(args.out, data)
update_last_timestamp(datetime.datetime.now().timestamp() * 1000)
sys.exit(0)