-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
69 lines (56 loc) · 1.95 KB
/
main.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
import os
import genanki
import random
from utils.format import *
from vocabulary_builder.translator_result import TranslatorResult
from vocabulary_builder.plugins.oxford_translator import OxfordTranslator
from vocabulary_builder.translator import Translator
from configuration.config import GLOBAL_CFG
model_id = random.randrange(1 << 30, 1 << 31)
vocabulary_model = genanki.Model(
model_id,
'Simple Model',
fields=[
{'name': 'Question'},
{'name': 'Answer'},
],
templates=[
{
'name': 'Card 1',
'qfmt': '{{Question}}',
'afmt': '{{FrontSide}}<hr id="answer">{{Answer}}',
},
])
deck_id = random.randrange(1 << 30, 1 << 31)
vocabulary_deck = genanki.Deck(deck_id, GLOBAL_CFG['anki']['deck_name'])
def get_words_to_find():
words = []
with open(GLOBAL_CFG['vocabulary_file_name'], "r") as ins:
for line in ins:
line = line.rstrip("\n")
words.append(line)
return words
oxford_translator = OxfordTranslator(GLOBAL_CFG['language']['from'], GLOBAL_CFG['language']['to'])
translator = Translator([oxford_translator])
mp3_files = []
not_founds = []
words = get_words_to_find()
for word in words:
print("Searching for word %s" % word)
result = translator.find(word)
if result.mp3_full_path:
mp3_files.append(result.mp3_full_path)
if result == TranslatorResult.empty():
not_founds.append(word)
else:
new_note = genanki.Note(
model=vocabulary_model,
fields=["<h1>" + word + "</h1>", format_sound(result.mp3_full_path) + '\n\n' + format_senses(result)])
vocabulary_deck.add_note(new_note)
genanki.Package(vocabulary_deck, media_files=mp3_files).write_to_file(GLOBAL_CFG['anki']['deck_file_name'])
if len(not_founds) > 0:
print("Could not found translations for the following words:")
for nf in not_founds:
print(nf)
for sound_file in mp3_files:
os.remove(sound_file)