/
add_cards.py
102 lines (85 loc) · 3.8 KB
/
add_cards.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
from mwb_help import deck_is_available, create_deck,\
model_is_available, add_model, add_note
from scraper_lxml import get_note_default, get_note_simple
class AnkiDutchDeck():
def __init__(self, deck_name=None):
if deck_name is None:
deck_name = 'tidbits'
if not deck_is_available(deck_name):
create_deck(deck_name)
self.deck_name = deck_name
self.default_model_name = 'dutch_default'
self.simple_model_name = 'dutch_simple'
if not model_is_available(self.default_model_name):
self.add_model_default()
if not model_is_available(self.simple_model_name):
self.add_model_simple()
def add_model_default(self):
model_name = 'dutch_default'
note_fields = ['Dutch', 'Misc', 'Explanations', 'Examples']
card_templates = [{'Front': '{{Dutch}}',
'Back' : '{{Misc}}<hr><hr>{{Explanations}}'},
{'Front': '{{Dutch}}<hr>{{Misc}}',
'Back' : '{{Explanations}}<hr><hr>{{Examples}}'},
{'Front': '{{Explanations}}',
'Back' : '{{Dutch}}<hr>{{Misc}}<hr><hr>{{Examples}}'},
{'Front': '{{Examples}}',
'Back' : '{{Dutch}}<hr>{{Misc}}<hr><hr>{{Explanations}}'}]
add_model(model_name, note_fields, card_templates)
def add_model_simple(self):
model_name = 'dutch_simple'
note_fields = ['Dutch', 'Explanations']
card_templates = [{'Front': '{{Dutch}}',
'Back' : '{{Explanations}}'},
{'Front': '{{Explanations}}',
'Back' : '{{Dutch}}'}]
add_model(model_name, note_fields, card_templates)
def add_note_default(self, note_fields):
add_note(note_fields, self.deck_name, self.default_model_name)
def add_note_simple(self, note_fields):
add_note(note_fields, self.deck_name, self.simple_model_name)
def add_note_from_word(self, word, output_file=None):
is_default_model = True
try:
note_fields = get_note_default(word)
if note_fields is None:
print(f'"{word}" not found in mijnwoordenbook')
if output_file:
with open(output_file, 'a') as f:
f.write('\n')
f.write(word)
return
except:
is_default_model = False
note_fields = get_note_simple(word)
try:
if is_default_model:
self.add_note_default(note_fields)
else:
self.add_note_simple(note_fields)
except:
print(f'"{word}" already exists in deck {self.deck_name}')
def add_note_from_list(self, word_list, output_file=None):
[self.add_note_from_word(w, output_file) for w in word_list]
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='file containing word list')
parser.add_argument('-f', '--file',
help='select file containing word list')
parser.add_argument('-l', '--list',
help='input word list, separated by comma')
parser.add_argument('-o', '--output',
help='output unfound words to file')
args = parser.parse_args()
word_list = []
if args.file is not None:
words = open(args.file).read().split('\n')
word_list.extend(words)
if args.list is not None:
words = args.list.strip().split(',')
word_list.extend(words)
# word_list = ['hhhsss', 'duits', 'alsjeblieft', 'waterpokken']
if "" in word_list:
word_list.remove("")
ADD = AnkiDutchDeck()
ADD.add_note_from_list(word_list, args.output)