-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMoji-fast.py
112 lines (104 loc) · 4.33 KB
/
Moji-fast.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
from ..base import *
import requests
import re
import json
import os
def wirte_word(worddict,word):#测试代码,写入
fileword=json.dumps(worddict)
_file="E:\\object\\weblio\\json\\Moji_{word}.json".format(word=word)
fileObject = open(_file, 'w')
fileObject.write(fileword)
fileObject.close()
def Word_t(word):
ret=''
word_Part_of_speech='['+''.join(re.findall(r'\[(.*?)\]',word['excerpt']))+']<br>'#词性
word_val=re.sub(r"\[(.*?)\]", "",word['excerpt']).split()#分割
ret+='''<div class="word-detail"><span class="detail-title">{pron}</span>{word_Part_of_speech}'''.format(pron=word['pron'],word_Part_of_speech=word_Part_of_speech)
i=1
word_text=''
for text in word_val:
word_text+='<p>{i_count}.{excerpt}</p>'.format(i_count=i,excerpt=text)
ret+=word_text+'</div>'
return ret
@register([u'Moji', u'Moji'])#接口名称
class Moji(WebService):#接口名称
target_search = 'https://api.mojidict.com/parse/functions/search_v3' #查询单词ID
target_fetch = 'https://api.mojidict.com/parse/functions/fetchWord_v2'#查询单词详细内容
target_tts = 'https://api.mojidict.com/parse/functions/fetchTts_v2'#TTS
hd = {'content-type': 'text/plain',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19551'}
data={#单词列表
'searchText':"",
"needWords": True,
"langEnv": "zh-CN_ja",
"_ApplicationId": "E62VyFVLMiW7kvbtVq3p",
"_ClientVersion": "js2.12.0",
}
word_data={#单词详细数据
"wordId": '',
"_ApplicationId": "E62VyFVLMiW7kvbtVq3p",
"_ClientVersion": "js2.12.0",
}
word_tts={#这个是例句的
"tarId":"",
"tarType":103,
"_ApplicationId":"E62VyFVLMiW7kvbtVq3p",
"_ClientVersion":"js2.12.0"
}
tts_data={#这个是单词的
"tarId": '',
"tarType": 102,
"_ApplicationId": "E62VyFVLMiW7kvbtVq3p",
"_ClientVersion": "js2.12.0"
}
def __init__(self):
super(Moji, self).__init__()#接口名称
def _get_from_api(self):
self.data['searchText']=self.word
r = requests.post(self.target_search, data=json.dumps(self.data), headers=self.hd) # POST请求
ans = r.json()['result']
# wirte_word(ans,self.word)
return self.cache_this(ans)
@with_styles(cssfile="_moji.css", need_wrap_css=False,wrap_class='mojidict-helper-card-container')
def _css(self, val):
# 如果是 css="@import 'moji.css'"
# # if os.path.exists('moji.css')==False:
# # from aqt.winpaths import get_appdata
# css_path=os.path.join(get_appdata(), "Anki2","addons21",'1807206748','service','static','_moji.css')#获取Anki的Fast插件位置
# import shutil
# try:
# shutil.copy(css_path,'_moji.css')
# except:
# pass
return val
@export('单词释义[简]')
def mean_simple(self):
words=self._get_field('words')
ret='''<div class="mojidict-helper-card"><div class="word-detail-container">'''
count=0
for word in words:
if word['spell']==self.word or word['pron']==self.word:
count+=1
ret+=Word_t(word)
if count==0:
temp=ret+Word_t(words[0])+'</div></div>'
ret=ret+'</div></div>'
if count==0:
return self._css(temp)
return self._css(ret)
@export('单词发音')
def mean_audio(self):
words=self._get_field('words')
self.tts_data['tarId']=words[0]['objectId']
r_tts = requests.post(self.target_tts, data=json.dumps(self.tts_data), headers=self.hd)#取单词发音
tts_url=r_tts.json()['result']['result']['url']
audio_name = get_hex_name(self.unique.lower(), re.search(r'.*?(?=&Expires)',tts_url[tts_url.rindex('/') + 1:]).group(), 'mp3')#唯一标识
if os.path.exists(audio_name) or self.download(tts_url, audio_name, 5):
with open(audio_name, 'rb') as f:
if f.read().strip() == '{"error":"Document not found"}':
res = ''
else:
res = self.get_anki_label(audio_name, 'audio')
if not res:
os.remove(audio_name)
return res