-
Notifications
You must be signed in to change notification settings - Fork 3
/
custom_input.py
66 lines (57 loc) · 1.9 KB
/
custom_input.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
#!/usr/bin/env python
import sys
import json
import importlib
import os
sys.path.append('anki')
from anki import Collection as aopen
def initAnkiModule(data, collection, card_type):
if bool(collection) == False or "deck" not in data:
print('Collection or deck is not found!')
return None
deck = aopen(collection)
deckId = deck.decks.id(data['deck'])
deck.decks.select(deckId)
model = deck.models.byName(card_type.GetCardType(deck.models))
if model is not None:
model['did'] = deckId
deck.models.save(model)
deck.models.setCurrent(model)
return deck
def handleCard(data, collection):
print('deck file:{}'.format(data['deck']))
print('card_type:{}'.format(data['card_type'] if 'card_type' in data else 'Basic'))
card_type = data['card_type'] if 'card_type' in data else 'basic'
card_type = importlib.import_module('cardtype.{}'.format(card_type))
deck = initAnkiModule(data, collection, card_type)
for result in data['content']:
print(result)
if result is None:
return
card_data = card_type.MakeCard(result)
if 0 == len(card_data):
return
card = deck.newNote()
for key in card_data:
card[key] = card_data[key]
try:
deck.addNote(card)
except(Exception, e):
if hasattr(e, "data"):
sys.exit("ERROR: Cound not add {}:{}", e.data["field"], e.data['type'])
else:
sys.exit(e)
deck.save()
deck.close()
def load_input(path):
with open(path, encoding='utf-8') as data_file:
return json.load(data_file)
if '__main__':
if len(sys.argv) >= 2:
input_path = sys.argv[1]
else:
input_path = 'customInput.json'
data = load_input(input_path)
collection = data['collection']
for card in data['cards']:
handleCard(card, collection)