anki-word-query/addons/fastwq/service/dict/youdao.py

170 lines
5.5 KiB
Python
Raw Normal View History

2018-07-01 10:55:30 +08:00
#-*- coding:utf-8 -*-
import re
import urllib2
import xml.etree.ElementTree
from aqt.utils import showInfo
2018-07-14 21:21:31 +08:00
from ..base import WebService, export, register, with_styles
2018-07-01 10:55:30 +08:00
js = '''
var initVoice = function () {
var player = document.getElementById('dictVoice');
document.addEventListener('click', function (e) {
var target = e.target;
if (target.hasAttribute('role') && target.getAttribute('role').indexOf('dict_audio_js') >= 0) {
var url = target.getAttribute('data-rel');
player.setAttribute('src', url);
player.volume = 1;
player.play();
e.preventDefault();
}
}, false);
};
initVoice();
'''
youdao_download_mp3 = True
2018-07-09 19:01:37 +08:00
@register([u'有道词典-英汉', u'Youdao'])
2018-07-01 10:55:30 +08:00
class Youdao(WebService):
def __init__(self):
super(Youdao, self).__init__()
def _get_from_api(self, lang='eng'):
2018-07-09 19:01:37 +08:00
url = (u'http://dict.youdao.com/fsearch?client=deskdict'
'&keyfrom=chrome.extension&pos=-1'
'&doctype=xml&xmlVersion=3.2'
'&dogVersion=1.0&vendor=unknown'
'&appVer=3.1.17.4208'
'&le={0}&q={1}').format(lang, self.word)
result ={
'phonetic': '',
'explains':'',
}
2018-07-01 10:55:30 +08:00
try:
2018-07-09 19:01:37 +08:00
html = self.get_response(url, timeout=5)
doc = xml.etree.ElementTree.fromstring(html)
2018-07-01 10:55:30 +08:00
# fetch symbols
symbol, uk_symbol, us_symbol = doc.findtext(".//phonetic-symbol"), doc.findtext(
".//uk-phonetic-symbol"), doc.findtext(".//us-phonetic-symbol")
if uk_symbol and us_symbol:
#phonetics = 'UK [%s] US [%s]' % (uk_symbol, us_symbol)
phonetics = '/%s/' % (us_symbol)
elif symbol:
phonetics = '/%s/' % symbol
else:
phonetics = ''
# fetch explanations
2018-07-09 19:01:37 +08:00
explains = '<br>'.join([node.text
for node in doc.findall(
".//custom-translation/translation/content"
)])
result.update({'phonetic': phonetics, 'explains': explains})
2018-07-01 10:55:30 +08:00
except:
2018-07-09 19:01:37 +08:00
pass
return self.cache_this(result)
def _get_field(self, key, default=u''):
return self.cache_result(key) if self.cached(key) else self._get_from_api().get(key, default)
2018-07-01 10:55:30 +08:00
2018-07-13 22:08:35 +08:00
@export(u'音标')
2018-07-01 10:55:30 +08:00
def fld_phonetic(self):
2018-07-09 19:01:37 +08:00
return self._get_field('phonetic')
2018-07-01 10:55:30 +08:00
2018-07-13 22:08:35 +08:00
@export(u'基本释义')
2018-07-01 10:55:30 +08:00
def fld_explains(self):
2018-07-09 19:01:37 +08:00
return self._get_field('explains')
2018-07-01 10:55:30 +08:00
@with_styles(cssfile='_youdao.css', js=js, need_wrap_css=True, wrap_class='youdao')
def _get_singledict(self, single_dict, lang='eng'):
url = u"http://m.youdao.com/singledict?q={0}&dict={1}&le={2}&more=false".format(
2018-07-09 19:01:37 +08:00
self.word,
single_dict,
lang
)
2018-07-01 10:55:30 +08:00
try:
result = urllib2.urlopen(url, timeout=5).read()
2018-07-09 19:01:37 +08:00
return (u'<div id="{0}_contentWrp" class="content-wrp dict-container">'
'<div id="{0}" class="trans-container {0} ">{1}</div>'
'</div>'
'<div id="outer">'
'<audio id="dictVoice" style="display: none"></audio>'
'</div>').format(
single_dict,
result.decode('utf-8')
)
2018-07-01 10:55:30 +08:00
except:
return ''
2018-07-13 22:08:35 +08:00
@export(u'英式发音')
2018-07-01 10:55:30 +08:00
def fld_british_audio(self):
2018-07-09 19:01:37 +08:00
audio_url = u'http://dict.youdao.com/dictvoice?audio={}&type=1'.format(self.word)
2018-07-01 10:55:30 +08:00
if youdao_download_mp3:
filename = u'_youdao_{}_uk.mp3'.format(self.word)
if self.download(audio_url, filename):
return self.get_anki_label(filename, 'audio')
return audio_url
2018-07-13 22:08:35 +08:00
@export(u'美式发音')
2018-07-01 10:55:30 +08:00
def fld_american_audio(self):
2018-07-09 19:01:37 +08:00
audio_url = u'http://dict.youdao.com/dictvoice?audio={}&type=2'.format(self.word)
2018-07-01 10:55:30 +08:00
if youdao_download_mp3:
filename = u'_youdao_{}_us.mp3'.format(self.word)
if self.download(audio_url, filename):
return self.get_anki_label(filename, 'audio')
return audio_url
2018-07-13 22:08:35 +08:00
@export(u'柯林斯英汉')
2018-07-01 10:55:30 +08:00
def fld_collins(self):
return self._get_singledict('collins')
2018-07-13 22:08:35 +08:00
@export(u'21世纪')
2018-07-01 10:55:30 +08:00
def fld_ec21(self):
return self._get_singledict('ec21')
2018-07-13 22:08:35 +08:00
@export(u'英英释义')
2018-07-01 10:55:30 +08:00
def fld_ee(self):
return self._get_singledict('ee')
2018-07-13 22:08:35 +08:00
@export(u'网络释义')
2018-07-01 10:55:30 +08:00
def fld_web_trans(self):
return self._get_singledict('web_trans')
2018-07-13 22:08:35 +08:00
@export(u'同根词')
2018-07-01 10:55:30 +08:00
def fld_rel_word(self):
return self._get_singledict('rel_word')
2018-07-13 22:08:35 +08:00
@export(u'同近义词')
2018-07-01 10:55:30 +08:00
def fld_syno(self):
return self._get_singledict('syno')
2018-07-13 22:08:35 +08:00
@export(u'双语例句')
2018-07-01 10:55:30 +08:00
def fld_blng_sents_part(self):
return self._get_singledict('blng_sents_part')
2018-07-13 22:08:35 +08:00
@export(u'原生例句')
2018-07-01 10:55:30 +08:00
def fld_media_sents_part(self):
return self._get_singledict('media_sents_part')
2018-07-13 22:08:35 +08:00
@export(u'权威例句')
2018-07-01 10:55:30 +08:00
def fld_auth_sents_part(self):
return self._get_singledict('auth_sents_part')
2018-07-13 22:08:35 +08:00
@export(u'新英汉大辞典(中)')
2018-07-01 10:55:30 +08:00
def fld_ce_new(self):
return self._get_singledict('ce_new')
2018-07-13 22:08:35 +08:00
@export(u'百科')
2018-07-01 10:55:30 +08:00
def fld_baike(self):
return self._get_singledict('baike')
2018-07-13 22:08:35 +08:00
@export(u'汉语词典(中)')
2018-07-01 10:55:30 +08:00
def fld_hh(self):
return self._get_singledict('hh')
2018-07-13 22:08:35 +08:00
@export(u'专业释义(中)')
2018-07-01 10:55:30 +08:00
def fld_special(self):
return self._get_singledict('special')