forked from valuex/GoldenDict2Anki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGD2Anki.py
86 lines (75 loc) · 2.37 KB
/
GD2Anki.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
# -*- coding: utf-8 -*-
"""
Created on Mon Oct 28 00:00:15 2019
@author: valuex
"""
import json
import urllib.request
import re
import sys
from mdict_query import IndexBuilder
import configparser
#import winsound # for sound
#import time # for sleep
import os
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)))
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'])
return response['result']
def NoteContent(FrontStr, BackStr):
BackStr=BackStr.replace('"','\\"')
BackStr=BackStr.replace(u'\xa0',u' ')
BackStr = re.sub(r'<img.*?>', '', BackStr)
# BackStr=BackStr.replace('</span>','</span><br>')
newnote="""
{
"deckName": "NewWords",
"modelName": "NewWordsType",
"fields": {
"Front": "%s",
"Back": "%s"
},
"options": {
"allowDuplicate": false
},
"tags": [ ]
}
""" % (FrontStr,BackStr)
return newnote
if __name__ == '__main__':
config = configparser.ConfigParser()
# get absolute path
fp_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
iniFile = os.path.join(fp_dir, "Config.ini")
# print(iniFile)
config.read(iniFile, encoding='utf-8')
mdict=config['Default']['mdxfile']
builder = IndexBuilder(mdict)
# Word="abandon"
Word=sys.argv[1]
Meanings=builder.mdx_lookup(Word, ignorecase = True)
record = Meanings[0]
CardNote=NoteContent(Word, record)
# print(CardNote)
# t3=time.time()
newnote=json.loads(CardNote,strict=False)
# print(newnote)
# t4=time.time()
try:
result = invoke('addNote',note=newnote)
print(result)
# winsound.Beep(440, 250) # frequency, duration
except:
pass
# winsound.Beep(600, 250)
# w=WindowsBalloonTip("Note Added", result)