This commit is contained in:
St.Huang 2018-07-09 19:01:37 +08:00
parent 1c394462d8
commit f301c45c5e
2 changed files with 71 additions and 35 deletions

View File

@ -26,20 +26,26 @@ initVoice();
youdao_download_mp3 = True youdao_download_mp3 = True
@register(u'有道词典') @register([u'有道词典-英汉', u'Youdao'])
class Youdao(WebService): class Youdao(WebService):
def __init__(self): def __init__(self):
super(Youdao, self).__init__() super(Youdao, self).__init__()
def _get_from_api(self, lang='eng'): def _get_from_api(self, lang='eng'):
url = "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=%s&q=%s" % ( url = (u'http://dict.youdao.com/fsearch?client=deskdict'
lang, self.word) '&keyfrom=chrome.extension&pos=-1'
phonetics, explains = '', '' '&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':'',
}
try: try:
result = urllib2.urlopen(url, timeout=5).read() html = self.get_response(url, timeout=5)
# showInfo(str(result)) doc = xml.etree.ElementTree.fromstring(html)
doc = xml.etree.ElementTree.fromstring(result)
# fetch symbols # fetch symbols
symbol, uk_symbol, us_symbol = doc.findtext(".//phonetic-symbol"), doc.findtext( symbol, uk_symbol, us_symbol = doc.findtext(".//phonetic-symbol"), doc.findtext(
".//uk-phonetic-symbol"), doc.findtext(".//us-phonetic-symbol") ".//uk-phonetic-symbol"), doc.findtext(".//us-phonetic-symbol")
@ -51,34 +57,50 @@ class Youdao(WebService):
else: else:
phonetics = '' phonetics = ''
# fetch explanations # fetch explanations
explains = '<br>'.join([node.text for node in doc.findall( explains = '<br>'.join([node.text
".//custom-translation/translation/content")]) for node in doc.findall(
return self.cache_this({'phonetic': phonetics, 'explains': explains}) ".//custom-translation/translation/content"
)])
result.update({'phonetic': phonetics, 'explains': explains})
except: except:
return {'phonetic': phonetics, 'explains': explains} 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)
@export(u'音标', 0) @export(u'音标', 0)
def fld_phonetic(self): def fld_phonetic(self):
return self.cache_result('phonetic') if self.cached('phonetic') else self._get_from_api()['phonetic'] return self._get_field('phonetic')
@export(u'基本释义', 1) @export(u'基本释义', 1)
def fld_explains(self): def fld_explains(self):
return self.cache_result('explains') if self.cached('explains') else self._get_from_api()['explains'] return self._get_field('explains')
@with_styles(cssfile='_youdao.css', js=js, need_wrap_css=True, wrap_class='youdao') @with_styles(cssfile='_youdao.css', js=js, need_wrap_css=True, wrap_class='youdao')
def _get_singledict(self, single_dict, lang='eng'): def _get_singledict(self, single_dict, lang='eng'):
url = u"http://m.youdao.com/singledict?q={0}&dict={1}&le={2}&more=false".format( url = u"http://m.youdao.com/singledict?q={0}&dict={1}&le={2}&more=false".format(
self.word, single_dict, lang) self.word,
single_dict,
lang
)
try: try:
result = urllib2.urlopen(url, timeout=5).read() result = urllib2.urlopen(url, timeout=5).read()
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')) 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')
)
except: except:
return '' return ''
@export(u'英式发音', 2) @export(u'英式发音', 2)
def fld_british_audio(self): def fld_british_audio(self):
audio_url = u'http://dict.youdao.com/dictvoice?audio={}&type=1'.format( audio_url = u'http://dict.youdao.com/dictvoice?audio={}&type=1'.format(self.word)
self.word)
if youdao_download_mp3: if youdao_download_mp3:
filename = u'_youdao_{}_uk.mp3'.format(self.word) filename = u'_youdao_{}_uk.mp3'.format(self.word)
if self.download(audio_url, filename): if self.download(audio_url, filename):
@ -87,8 +109,7 @@ class Youdao(WebService):
@export(u'美式发音', 3) @export(u'美式发音', 3)
def fld_american_audio(self): def fld_american_audio(self):
audio_url = u'http://dict.youdao.com/dictvoice?audio={}&type=2'.format( audio_url = u'http://dict.youdao.com/dictvoice?audio={}&type=2'.format(self.word)
self.word)
if youdao_download_mp3: if youdao_download_mp3:
filename = u'_youdao_{}_us.mp3'.format(self.word) filename = u'_youdao_{}_us.mp3'.format(self.word)
if self.download(audio_url, filename): if self.download(audio_url, filename):

View File

@ -7,26 +7,34 @@ from aqt.utils import showInfo
from .base import WebService, export, register, with_styles from .base import WebService, export, register, with_styles
@register(u'有道词典-法语') @register([u'有道词典-法语', u'Youdao-French'])
class Youdaofr(WebService): class Youdaofr(WebService):
def __init__(self): def __init__(self):
super(Youdaofr, self).__init__() super(Youdaofr, self).__init__()
def _get_from_api(self, lang='fr'): def _get_from_api(self, lang='fr'):
url = "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=%s&q=%s" % ( url = (u'http://dict.youdao.com/fsearch?client=deskdict'
lang, self.word) '&keyfrom=chrome.extension&pos=-1'
explains = '' '&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':'',
}
try: try:
result = urllib2.urlopen(url, timeout=5).read() html = self.get_response(url, timeout=5)
# showInfo(str(result)) # showInfo(str(result))
doc = xml.etree.ElementTree.fromstring(result) doc = xml.etree.ElementTree.fromstring(html)
# fetch explanations # fetch explanations
explains = '<br>'.join([node.text for node in doc.findall( explains = '<br>'.join([node.text for node in doc.findall(
".//custom-translation/translation/content")]) ".//custom-translation/translation/content")])
return self.cache_this({'explains': explains}) result.update({'explains': explains})
except: except:
return {'explains': explains} pass
return self.cache_this(result)
@export(u'基本释义', 1) @export(u'基本释义', 1)
def fld_explains(self): def fld_explains(self):
@ -35,18 +43,25 @@ class Youdaofr(WebService):
@with_styles(cssfile='_youdao.css', need_wrap_css=True, wrap_class='youdao') @with_styles(cssfile='_youdao.css', need_wrap_css=True, wrap_class='youdao')
def _get_singledict(self, single_dict, lang='fr'): def _get_singledict(self, single_dict, lang='fr'):
url = "http://m.youdao.com/singledict?q=%s&dict=%s&le=%s&more=false" % ( url = u"http://m.youdao.com/singledict?q={0}&dict={1}&le={2}&more=false".format(
self.word, single_dict, lang) self.word, single_dict, lang
)
try: try:
result = urllib2.urlopen(url, timeout=5).read() html = urllib2.urlopen(url, timeout=5).read()
return '<div id="%s_contentWrp" class="content-wrp dict-container"><div id="%s" class="trans-container %s ">%s</div></div><div id="outer"><audio id="dictVoice" style="display: none"></audio></div>' % (single_dict, single_dict, single_dict, result) return (u'<div id="{0}_contentWrp" class="content-wrp dict-container">'
'<div id="{1}" class="trans-container {2} ">{3}</div>'
'</div>'
'<div id="outer">'
'<audio id="dictVoice" style="display: none"></audio>'
'</div>').format(
single_dict,
single_dict,
single_dict,
html
)
except: except:
return '' return ''
# @export(u'英英释义', 4)
# def fld_ee(self):
# return self._get_singledict('ee')
@export(u'网络释义', 5) @export(u'网络释义', 5)
def fld_web_trans(self): def fld_web_trans(self):
return self._get_singledict('web_trans') return self._get_singledict('web_trans')