-
Notifications
You must be signed in to change notification settings - Fork 0
/
anki_tool.py
58 lines (51 loc) · 1.8 KB
/
anki_tool.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
import json
import urllib.request
import time
start_time = time.time()
def request(action, **params):
return {'action': action, 'params': params, 'version': 6}
def invoke(action, **params):
requestJson = json.dumps(request(action, **params)).encode('utf-8')
response = json.load(urllib.request.urlopen(
urllib.request.Request('http://localhost:8765', requestJson)))
try:
if len(response) != 2:
raise Exception('response has an unexpected number of fields')
if 'error' not in response:
raise Exception('response is missing required error field')
if 'result' not in response:
raise Exception('response is missing required result field')
if response['error'] is not None:
raise Exception(response['error'])
except:
pass
return response['result']
with open('kanji-jouyou.json', encoding='utf-8') as f:
jouyou: dict = json.load(f)
jouyou
notes = []
jlpt_new = []
jlpt_old = []
not_jlpt = []
for x in jouyou:
if jouyou[x]['jlpt_new'] is None:
jlpt_new.append(x)
if jouyou[x]['jlpt_old'] is None:
jlpt_old.append(x)
if jouyou[x]['jlpt_old'] is None and jouyou[x]['jlpt_new'] is None:
not_jlpt.append(x)
level = 'N' + str(jouyou[x]['jlpt_new'])
notes.append({
'deckName': f'常用漢字|JLPT再編成::{level}',
'modelName': 'japanese_en',
'fields': {
'Kanji': x,
'音読み': ', '.join(jouyou[x]['readings_on']),
'訓読み': ', '.join(jouyou[x]['readings_kun']),
'意味': ', '.join(jouyou[x]['meanings'])
},
'tags': [
level, 'kanji', f"strokes: {jouyou[x]['strokes']}",
]
})
print("--- %s seconds ---" % (time.time() - start_time))