/
jp-tool.py
executable file
·140 lines (115 loc) · 3.97 KB
/
jp-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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import json
import colorful
import api.anki as anki
import api.dict as Dict
from api import api_call
def print_bold(s):
print(colorful.bold | s)
def handle_multiwords(check_word, multi_words, dict_service):
print_bold('Multi words:')
for i, word in enumerate(multi_words):
print_bold("%d: %s %s" %
(i, word.get('expression', ''), word.get('pronounce', '')))
number = 0
try:
number = int(input('Input Number: '))
except ValueError:
print(colorful.red | "Error input !")
handle_multiwords(check_word, multi_words, dict_service)
if len(multi_words) > number:
expression = multi_words[number].get('expression', '')
pronounce = multi_words[number].get('pronounce', '')
word_dict = dict_service.get_dict(expression, pronounce)
expression = word_dict.get('word_expressions', '')
pronounce = word_dict.get('word_pronounce', '')
if check_word == expression:
word_dict['word_expressions'] = expression + pronounce
print(colorful.bold | Dict.format_hjdict(word_dict))
save(word_dict)
else:
print(colorful.red | "Error input!")
def save(word_dict):
confirm = input('save to anki[y/N]: ')
if confirm == 'y':
word_simple = word_dict.get('word_simple', [])
word_descs = word_dict.get('word_descs', [])
word_simple_null = len(word_simple) == 0
if word_simple_null:
word_simple = word_descs
audio_url = word_dict.get('word_audio', '')
if audio_url.startswith('https://'):
audio_url = audio_url.replace('https://', 'http://', 1)
field = {
'expression':
word_dict.get('word_expressions', ''),
'pronounce':
word_dict.get('word_pronounce', ''),
'kata':
word_dict.get('word_kata', ''),
'tone':
word_dict.get('word_tone', ''),
'audio':
"[sound:%s]" % audio_url,
'simple':
anki.format_simple(word_simple, word_simple_null),
'sentence':
anki.format_descs(word_dict.get('word_descs', []))
}
tags = ['japanese(dict)']
model = 'japanese(dict)'
deck = 'Japanese_Word'
anki_api = anki.AnkiConnectApi()
if anki_api.canAddNote(deck, model, field, tags):
print(anki_api.addNote(deck, model, field, tags))
else:
print(colorful.bold & colorful.red
| 'Can not add note ! Duplicate !')
else:
print("Disable!")
DEBUG = False
def printException(e):
if DEBUG:
raise e
else:
print(colorful.red | e)
def fanyiApi(text):
for vender in ['google', 'tencent', 'youdao', 'baidu']:
try:
print(colorful.bold | '{}:'.format(vender))
result = api_call('fanyi', {
'vender': vender,
'q': text,
'from': 'ja',
'to': 'zh'
})
for s in result:
for s in filter(lambda x: x != '', s.split('。')):
print(' ' + s + '。')
print()
except Exception as e:
printException(e)
def main():
dict_service = Dict.HJDictApi()
check_word = ""
try:
word_dict = ""
if len(sys.argv) == 2:
check_word = sys.argv[1]
word_dict = dict_service.get_dict(check_word)
print(colorful.bold | Dict.format_hjdict(word_dict))
save(word_dict)
else:
print(colorful.bold & colorful.red | 'param: translate text')
except Dict.MultiWordsException as e:
try:
handle_multiwords(check_word, e.multi_words, dict_service)
except Exception as e:
printException(e)
except Dict.NotfoundException as e:
fanyiApi(check_word)
except Exception as e:
printException(e)
main()