2018-07-01 10:55:30 +08:00
|
|
|
#-*- coding:utf-8 -*-
|
|
|
|
|
import json
|
|
|
|
|
from .base import WebService, export, register, with_styles
|
|
|
|
|
|
|
|
|
|
bing_download_mp3 = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@register(u'Bing xtk')
|
|
|
|
|
class BingXtk(WebService):
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
super(BingXtk, self).__init__()
|
|
|
|
|
|
|
|
|
|
def _get_content(self):
|
|
|
|
|
resp = {'pronunciation': '', 'defs': '', 'sams': ''}
|
|
|
|
|
headers = {
|
|
|
|
|
'Accept-Language': 'en-US,zh-CN;q=0.8,zh;q=0.6,en;q=0.4',
|
|
|
|
|
'User-Agent': 'WordQuery Addon (Anki)',
|
|
|
|
|
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'}
|
2018-07-06 20:56:20 +08:00
|
|
|
word = self.word.replace(' ', '_')
|
|
|
|
|
url = u'http://xtk.azurewebsites.net/BingDictService.aspx?Word={}'.format(word.encode('utf-8'))
|
2018-07-01 10:55:30 +08:00
|
|
|
try:
|
2018-07-06 20:56:20 +08:00
|
|
|
#request = urllib2.Request(u'http://xtk.azurewebsites.net/BingDictService.aspx?Word=' +\
|
|
|
|
|
# word.encode('utf-8'), headers=headers)
|
|
|
|
|
#resp = json.loads(urllib2.urlopen(request).read())
|
|
|
|
|
resp = json.loads(self.get_response(url, headers=headers, timeout=10))
|
2018-07-01 10:55:30 +08:00
|
|
|
return self.cache_this(resp)
|
2018-07-06 20:56:20 +08:00
|
|
|
except:
|
2018-07-01 10:55:30 +08:00
|
|
|
return resp
|
|
|
|
|
|
|
|
|
|
def _get_field(self, key, default=u''):
|
|
|
|
|
return self.cache_result(key) if self.cached(key) else self._get_content().get(key, default)
|
|
|
|
|
|
|
|
|
|
def _get_subfield(self, field, key, default=u''):
|
|
|
|
|
subfield = default
|
|
|
|
|
if field:
|
|
|
|
|
subfield = field.get(key, default)
|
|
|
|
|
if subfield is None:
|
|
|
|
|
subfield = default
|
|
|
|
|
return subfield
|
|
|
|
|
|
2018-07-06 20:56:20 +08:00
|
|
|
@export('AME_PHON', 1)
|
2018-07-01 10:55:30 +08:00
|
|
|
def fld_phonetic_us(self):
|
|
|
|
|
seg = self._get_field('pronunciation')
|
|
|
|
|
return self._get_subfield(seg, 'AmE')
|
|
|
|
|
|
2018-07-06 20:56:20 +08:00
|
|
|
@export('BRE_PHON', 2)
|
2018-07-01 10:55:30 +08:00
|
|
|
def fld_phonetic_uk(self):
|
|
|
|
|
seg = self._get_field('pronunciation')
|
|
|
|
|
return self._get_subfield(seg, 'BrE')
|
|
|
|
|
|
2018-07-06 20:56:20 +08:00
|
|
|
@export('AME_PRON', 3)
|
2018-07-01 10:55:30 +08:00
|
|
|
def fld_mp3_us(self):
|
|
|
|
|
seg = audio_url = self._get_field('pronunciation')
|
|
|
|
|
audio_url = self._get_subfield(seg, 'AmEmp3')
|
|
|
|
|
if bing_download_mp3 and audio_url:
|
|
|
|
|
filename = u'bing_{}_us.mp3'.format(self.word)
|
|
|
|
|
if self.download(audio_url, filename):
|
|
|
|
|
return self.get_anki_label(filename, 'audio')
|
|
|
|
|
return audio_url
|
|
|
|
|
|
2018-07-06 20:56:20 +08:00
|
|
|
@export('BRE_PRON', 4)
|
2018-07-01 10:55:30 +08:00
|
|
|
def fld_mp3_uk(self):
|
|
|
|
|
seg = self._get_field('pronunciation')
|
|
|
|
|
audio_url = self._get_subfield(seg, 'BrEmp3')
|
|
|
|
|
if bing_download_mp3 and audio_url:
|
|
|
|
|
filename = u'bing_{}_br.mp3'.format(self.word)
|
|
|
|
|
if self.download(audio_url, filename):
|
|
|
|
|
return self.get_anki_label(filename, 'audio')
|
|
|
|
|
return audio_url
|
|
|
|
|
|
|
|
|
|
@with_styles(css='.pos{font-weight:bold;margin-right:4px;}', need_wrap_css=True, wrap_class='bing')
|
|
|
|
|
def _css(self, val):
|
|
|
|
|
return val
|
|
|
|
|
|
2018-07-06 20:56:20 +08:00
|
|
|
@export('DEF', 5)
|
2018-07-01 10:55:30 +08:00
|
|
|
def fld_definition(self):
|
|
|
|
|
segs = self._get_field('defs')
|
|
|
|
|
if isinstance(segs, list) and len(segs) > 0:
|
2018-07-06 20:56:20 +08:00
|
|
|
val = u'<br>'.join([u'''<span class="pos"><b>{0}</b></span>
|
|
|
|
|
<span class="def">{1}</span>'''.format(seg['pos'], seg['def']) for seg in segs])
|
2018-07-01 10:55:30 +08:00
|
|
|
return self._css(val)
|
|
|
|
|
return ''
|
|
|
|
|
|
2018-07-06 20:56:20 +08:00
|
|
|
@export('EXAMPLE', 6)
|
2018-07-01 10:55:30 +08:00
|
|
|
# @with_styles(cssfile='_bing2.css', need_wrap_css=True, wrap_class=u'bing')
|
|
|
|
|
def fld_samples(self):
|
|
|
|
|
max_numbers = 10
|
|
|
|
|
segs = self._get_field('sams')
|
|
|
|
|
sentences = ''
|
|
|
|
|
for i, seg in enumerate(segs):
|
|
|
|
|
sentences = sentences +\
|
|
|
|
|
u"""<li><div class="se_li1">
|
|
|
|
|
<div class="sen_en">{0}</div>
|
|
|
|
|
<div class="sen_cn">{1}</div>
|
2018-07-06 20:56:20 +08:00
|
|
|
</div></li>""".format(seg['eng'], seg['chn'])#, i + 1)
|
2018-07-01 10:55:30 +08:00
|
|
|
if i == 9:
|
|
|
|
|
break
|
|
|
|
|
return u"""<div class="se_div">
|
|
|
|
|
<div class="sentenceCon">
|
|
|
|
|
<div id="sentenceSeg"><ol>{}</ol></div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>""".format(sentences)
|