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

181 lines
6.3 KiB
Python
Raw Normal View History

2018-07-01 10:55:30 +08:00
#-*- coding:utf-8 -*-
2018-07-30 17:42:14 +08:00
import os
import xml.etree.ElementTree
from ..base import *
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'
2018-07-30 17:42:14 +08:00
'&le={0}&q={1}').format(lang, self.quote_word)
2018-07-09 19:01:37 +08:00
result ={
'phonetic': '',
'us_phonetic': '',
'uk_phonetic': '',
2018-07-09 19:01:37 +08:00
'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)
us_phonetics = '/%s/' % us_symbol
uk_phonetics = '/%s/' % uk_symbol
#phonetics = '/%s/' % (us_symbol)
2018-07-01 10:55:30 +08:00
elif symbol:
phonetics = '/%s/' % symbol
us_phonetics = ''
uk_phonetics = ''
2018-07-01 10:55:30 +08:00
else:
phonetics = ''
us_phonetics = ''
uk_phonetics = ''
2018-07-01 10:55:30 +08:00
# 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, 'us_phonetic': us_phonetics, 'uk_phonetic': uk_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)
2018-09-18 14:51:25 +08:00
@export('PHON')
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
@export('AME_PHON')
def fld_phonetic_us(self):
return self._get_field('us_phonetic')
@export('BRE_PHON')
def fld_phonetic_uk(self):
return self._get_field('uk_phonetic')
2018-09-18 13:55:33 +08:00
@export([u'基本释义', u'Explanations'])
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-30 17:42:14 +08:00
self.quote_word,
2018-07-09 19:01:37 +08:00
single_dict,
lang
)
2018-07-01 10:55:30 +08:00
try:
2018-07-30 17:42:14 +08:00
result = self.get_response(url, timeout=5)
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-09-18 14:51:25 +08:00
@export('BRE_PRON')
2018-07-01 10:55:30 +08:00
def fld_british_audio(self):
2018-07-30 17:42:14 +08:00
audio_url = u'http://dict.youdao.com/dictvoice?audio={}&type=1'.format(self.quote_word)
2018-07-01 10:55:30 +08:00
if youdao_download_mp3:
2018-07-30 17:42:14 +08:00
filename = get_hex_name(self.unique.lower(), audio_url, 'mp3')
if os.path.exists(filename) or self.download(audio_url, filename):
2018-07-01 10:55:30 +08:00
return self.get_anki_label(filename, 'audio')
return audio_url
2018-09-18 14:51:25 +08:00
@export('AME_PRON')
2018-07-01 10:55:30 +08:00
def fld_american_audio(self):
2018-07-30 17:42:14 +08:00
audio_url = u'http://dict.youdao.com/dictvoice?audio={}&type=2'.format(self.quote_word)
2018-07-01 10:55:30 +08:00
if youdao_download_mp3:
2018-07-30 17:42:14 +08:00
filename = get_hex_name(self.unique.lower(), audio_url, 'mp3')
if os.path.exists(filename) or self.download(audio_url, filename):
2018-07-01 10:55:30 +08:00
return self.get_anki_label(filename, 'audio')
return audio_url
2018-09-18 13:55:33 +08:00
@export([u'柯林斯英汉', u'Collins'])
2018-07-01 10:55:30 +08:00
def fld_collins(self):
return self._get_singledict('collins')
2018-09-18 13:55:33 +08:00
@export([u'21世纪', u'21st century'])
2018-07-01 10:55:30 +08:00
def fld_ec21(self):
return self._get_singledict('ec21')
2018-09-18 13:55:33 +08:00
@export([u'英英释义', u'English-english dictionary'])
2018-07-01 10:55:30 +08:00
def fld_ee(self):
return self._get_singledict('ee')
2018-09-18 13:55:33 +08:00
@export([u'网络释义', u'Web translation'])
2018-07-01 10:55:30 +08:00
def fld_web_trans(self):
return self._get_singledict('web_trans')
2018-09-18 13:55:33 +08:00
@export([u'同根词', u'Related words'])
2018-07-01 10:55:30 +08:00
def fld_rel_word(self):
return self._get_singledict('rel_word')
2018-09-18 13:55:33 +08:00
@export([u'同近义词', u'Synonyms'])
2018-07-01 10:55:30 +08:00
def fld_syno(self):
return self._get_singledict('syno')
2018-09-18 13:55:33 +08:00
@export([u'双语例句', u'Bilingual examples'])
2018-07-01 10:55:30 +08:00
def fld_blng_sents_part(self):
return self._get_singledict('blng_sents_part')
@export([u'原生例句', u''])
2018-07-01 10:55:30 +08:00
def fld_media_sents_part(self):
return self._get_singledict('media_sents_part')
2018-09-18 13:55:33 +08:00
@export([u'权威例句', u'Authoritative examples'])
2018-07-01 10:55:30 +08:00
def fld_auth_sents_part(self):
return self._get_singledict('auth_sents_part')
2018-09-18 13:55:33 +08:00
@export([u'新英汉大辞典(中)', u'New english-chinese dictionary'])
2018-07-01 10:55:30 +08:00
def fld_ce_new(self):
return self._get_singledict('ce_new')
2018-09-18 13:55:33 +08:00
@export([u'百科', u'baike'])
2018-07-01 10:55:30 +08:00
def fld_baike(self):
return self._get_singledict('baike')
2018-09-18 13:55:33 +08:00
@export([u'汉语词典(中)', u'Chinese dictionary'])
2018-07-01 10:55:30 +08:00
def fld_hh(self):
return self._get_singledict('hh')
2018-09-18 13:55:33 +08:00
@export([u'专业释义(中)', u'Professional translation'])
2018-07-01 10:55:30 +08:00
def fld_special(self):
return self._get_singledict('special')