-
Notifications
You must be signed in to change notification settings - Fork 2
/
load_from_csv.py
106 lines (81 loc) · 3.14 KB
/
load_from_csv.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
#!/usr/bin/env python2
# See
# https://www.juliensobczak.com/tell/2016/12/26/anki-scripting.html
#
import argparse
import io
from os import getcwd
from os.path import join
import sys
from anki import Collection
from yatetradki.tools.log import get_logger
from yatetradki.tools.pronunciation import Pronunciation
from yatetradki.utils import cleanup_query
_logger = get_logger('load_from_csv')
sys.path.insert(0, '/usr/share/anki')
#COLLECTION = '/home/bz/Documents/Anki/bz/collection.anki2'
COLLECTION = '/home/bz/.local/share/Anki2/bz/collection.anki2'
def load_from_csv(args):
cwd = getcwd()
col = Collection(COLLECTION, log=True)
pronunciation = Pronunciation(args.audio)
# Set the model
modelBasic = col.models.byName(args.model)
deck = col.decks.byName(args.deck)
col.decks.select(deck['id'])
col.decks.current()['mid'] = modelBasic['id']
query_template = 'deck:"%s" note:"%s" word:"%s"'
for line in io.open(join(cwd, args.csv), encoding='utf8'):
word, example, meaning = line.split('\t')
query = cleanup_query(query_template % (args.deck, args.model, word))
_logger.info('>>> QUERY: %s', query)
found_notes = col.findNotes(query)
# import ipdb; ipdb.set_trace()
# deck:english::lingvo-online epiphany
# print(query)
# print(found_notes)
# continue
if found_notes:
_logger.debug('Duplicate notes (%s) for word %s: %s',
len(found_notes), word, found_notes)
if not args.update:
_logger.debug('Skipping word %s', word)
continue
_logger.debug('Updating note %s', found_notes[0])
note = col.getNote(found_notes[0])
else:
note = col.newNote()
note.model()['did'] = deck['id']
fields = {
'Word': word,
'Example': example,
'Description': meaning,
}
if args.audio:
pronunciation.fill(word, col, fields)
for field, value in fields.items():
note.fields[args.fields.index(field)] = value
if found_notes:
_logger.debug('Updated: %s', word)
else:
col.addNote(note)
_logger.info('Added: %s', word)
note.flush()
col.save()
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--csv", help="File to import data from", required=False)
parser.add_argument("--deck", help="Deck name to import to", required=True)
parser.add_argument("--model", help="Model to use (card type)", required=True)
parser.add_argument("--fields", help="List of fields of the model", required=True)
parser.add_argument("--update", help="True if existing notes should be updated",
default=False, action='store_true')
parser.add_argument("--audio", choices=['none', 'norwegian', 'korean', 'english'],
help="If set, add audio generated by TTS depending on the choice",
default='none')
args = parser.parse_args()
args.fields = args.fields.split(',')
_logger.debug('Args: %s', args)
load_from_csv(args)
if __name__ == '__main__':
main()