-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathkindle2memrise.py
More file actions
executable file
·267 lines (191 loc) · 6.32 KB
/
kindle2memrise.py
File metadata and controls
executable file
·267 lines (191 loc) · 6.32 KB
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
#!/usr/bin/env python3
import os
import re
import sys
import argparse
import sqlite3
from urllib.request import urlopen
from urllib.request import urlretrieve
from bs4 import BeautifulSoup
CAMBRIDGE_ENDPOINT = \
"https://dictionary.cambridge.org/search/english-polish/direct/?q="
# https://github.com/pasternak/cambridge-cli/blob/master/cambridge-cli.py
# https://github.com/DrewSSP/bulk-audio-upload
class Translation(object):
def translate(self):
term = re.sub(r"\s", "-", self.word)
try:
cdict = urlopen(CAMBRIDGE_ENDPOINT + term)
if (cdict.getcode() != 200):
print("Error")
return
except:
return
soup = BeautifulSoup(cdict, 'html.parser')
desc = definition = example = pronunciation = audiofileURL = None
try:
translation = soup.find(attrs={"class": "trans"}).get_text().strip()
except:
translation = None
try:
definition = soup.find(attrs={"class": "def"}).get_text().strip().capitalize()
except:
definition = None
try:
example = soup.find(attrs={"title": "Example"}).get_text().strip()
except:
example = None
try:
pronunciation = soup.find(attrs={"class": "ipa"}).get_text().strip()
except:
pronunciation = None
try:
audiofileURL = soup.find(attrs={"data-src-mp3": True}).attrs['data-src-mp3']
except:
audiofileURL = None
if(translation == None):
translation = definition
definition = None
print("\n\033[1m%s:\033[0m" % term)
print("\t>> %s" % translation )
print("\n[-] Definition:")
print("\t%s\n" % definition)
print("\n[-] Example:")
print("\t%s\n" % example)
print("\n[-] Pronunciation:")
print("\t%s\n" % pronunciation)
print("\n[-] Audio URL:")
print("\t%s\n" % audiofileURL)
self.translation = translation
self.definition = definition
self.example = example
self.pronunciation = pronunciation
self.audiofileURL = audiofileURL
self.audiofilePath = downloadAudioFile(audiofileURL, term)
def __init__(self, word):
self.word = word
self.translation = None
self.definition = None
self.pronunciation = None
self.partofdpeech = None
self.gender = None
self.audiofileURL = None
self.audiofilePath = None
self.translate()
class DatabaseManager(object):
def __init__(self, db):
self.conn = sqlite3.connect(db)
self.conn.execute('pragma foreign_keys = on')
self.conn.commit()
self.cur = self.conn.cursor()
def query(self, *arg):
self.cur.execute(*arg)
self.conn.commit()
return self.cur
def close(self):
self.conn.close()
def __del__(self):
self.conn.close()
def translate(kindleDBFilename, dictionaryDBFilename, outputFilename, outputRevision, debug):
newWords = 0
print("Opening kindleDB: %s" % kindleDBFilename)
kindleDB = DatabaseManager(kindleDBFilename)
print("Opening dictionaryDB: %s" % dictionaryDBFilename)
dictionaryDB = DatabaseManager(dictionaryDBFilename)
dictionaryDB.query("CREATE TABLE IF NOT EXISTS dictionary (word TEXT UNIQUE, translation TEXT, definition TEXT, example TEXT, pronunciation TEXT, partofdpeech TEXT, gender TEXT, audiofileURL TEXT, audiofilePath TEXT, revision INTEGER)")
try:
revisions = dictionaryDB.query("SELECT MAX(revision) FROM dictionary")
revision = revisions.fetchone()[0] + 1
except:
revision = 1
rows = kindleDB.query("SELECT stem FROM WORDS WHERE lang='en'")
while True:
row = rows.fetchone()
if row == None:
break
# stem (i.e. word)
word = row[0];
if(debug):
print("Found in KindleDB: %s" % word)
alreadyInDB = dictionaryDB.query("SELECT COUNT(*) FROM dictionary where word=?", [word])
if(alreadyInDB.fetchone()[0] > 0):
continue;
try:
translated = Translation(word)
dictionaryDB.query("INSERT INTO dictionary VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
[
translated.word,
translated.translation,
translated.definition,
translated.example,
translated.pronunciation,
translated.partofdpeech,
translated.gender,
translated.audiofileURL,
translated.audiofilePath,
revision
] )
if(translated.translation != None):
newWords += 1
except sqlite3.Error as e:
print("Skipping.... %s" % e)
# If no new words, fallback to the previous revision)
print("Number of new words: %d" % newWords)
#if(newWords == 0):
# revision -= 1
if(outputRevision == 0):
print
revision = 0
outputMemrise(dictionaryDB, revision, outputFilename)
# dictionaryDB.query("DELETE FROM dictionary WHERE word=?", [ "retorted" ]);
dictionaryDB.close()
kindleDB.close()
def outputMemrise(dictionaryDB, revision, outputFilename):
print ("Writting output memrise file: %s" % outputFilename)
print ("Revision (or greater) %d " % revision)
try:
with open(outputFilename, 'w') as f:
cursor = dictionaryDB.query("SELECT word, translation, definition, example, pronunciation, partofdpeech, gender FROM dictionary WHERE definition IS NOT NULL AND revision >= ?", [revision])
while True:
row = cursor.fetchone()
if(row == None):
break
outputLine = ""
for i in row:
if(i == None):
outputLine += "\t"
else:
outputLine += i + "\t"
print(outputLine, file=f)
except Error as e:
print ("Error writting the file" + e)
def downloadAudioFile(URL, filename):
directory = 'audio'
filename = filename.replace(" ", "_")
if not os.path.exists(directory):
os.makedirs(directory)
try:
filename = os.path.join(directory, filename + '.mp3')
urlretrieve(URL, filename)
except:
filename = None
return filename
def usage(parser):
parser.print_help()
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-kindleDB', help="Kindle vocabulary db filename (default: vocab.db)", default="vocab.db")
parser.add_argument('-dictionaryDB', help="Memrise dictionary db filename (default: memrise.db)", default="memrise.db")
parser.add_argument('-output', help="Output file to import to memrise.com (default: memrise.txt)", default="memrise.txt")
parser.add_argument('-revision', type=int, help="Revision to output. Not specfied (default): last, 0 - all", default="-1")
parser.add_argument('-debug', action='store_true', help="Enable debug", default=False)
args = parser.parse_args()
translate(
args.kindleDB,
args.dictionaryDB,
args.output,
args.revision,
args.debug
)
if __name__ == "__main__":
main()