2018-07-01 10:55:30 +08:00
|
|
|
#-*- coding:utf-8 -*-
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
|
|
from aqt.utils import showInfo, showText
|
2018-07-06 12:29:50 +08:00
|
|
|
from .base import WebService, export, register, with_styles, parseHtml
|
2018-07-01 10:55:30 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@register(u'Bing')
|
|
|
|
|
class Bing(WebService):
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
super(Bing, self).__init__()
|
|
|
|
|
|
|
|
|
|
def _get_content(self):
|
|
|
|
|
word = self.word.replace(' ', '_')
|
|
|
|
|
data = self.get_response(u"http://cn.bing.com/dict/search?q={}&mkt=zh-cn".format(word))
|
2018-07-06 12:29:50 +08:00
|
|
|
soup = parseHtml(data)
|
2018-07-01 10:55:30 +08:00
|
|
|
result = {}
|
2018-07-06 12:29:50 +08:00
|
|
|
|
|
|
|
|
element = soup.find('div', class_='hd_prUS')
|
2018-07-01 10:55:30 +08:00
|
|
|
if element:
|
|
|
|
|
result['phonitic_us'] = str(element).decode('utf-8')
|
2018-07-06 12:29:50 +08:00
|
|
|
|
|
|
|
|
element = soup.find('div', class_='hd_pr')
|
2018-07-01 10:55:30 +08:00
|
|
|
if element:
|
|
|
|
|
result['phonitic_uk'] = str(element).decode('utf-8')
|
2018-07-06 12:29:50 +08:00
|
|
|
|
|
|
|
|
element = soup.find('div', class_='hd_if')
|
2018-07-01 10:55:30 +08:00
|
|
|
if element:
|
|
|
|
|
result['participle'] = str(element).decode('utf-8')
|
2018-07-06 12:29:50 +08:00
|
|
|
|
|
|
|
|
element = soup.find('div', class_='qdef')
|
2018-07-01 10:55:30 +08:00
|
|
|
if element:
|
2018-07-06 12:29:50 +08:00
|
|
|
element = getattr(element, 'ul', '')
|
|
|
|
|
if element:
|
|
|
|
|
result['def'] = u''.join([str(content) for content in element.contents])
|
|
|
|
|
|
2018-07-01 10:55:30 +08:00
|
|
|
return self.cache_this(result)
|
|
|
|
|
|
|
|
|
|
def _get_field(self, key, default=u''):
|
|
|
|
|
return self.cache_result(key) if self.cached(key) else self._get_content().get(key, default)
|
|
|
|
|
|
|
|
|
|
@export(u'美式音标', 1)
|
|
|
|
|
def fld_phonetic_us(self):
|
|
|
|
|
return self._get_field('phonitic_us')
|
|
|
|
|
|
|
|
|
|
@export(u'英式音标', 2)
|
|
|
|
|
def fld_phonetic_uk(self):
|
|
|
|
|
return self._get_field('phonitic_uk')
|
|
|
|
|
|
|
|
|
|
@export(u'词语时态', 3)
|
|
|
|
|
def fld_participle(self):
|
|
|
|
|
return self._get_field('participle')
|
|
|
|
|
|
|
|
|
|
@with_styles(css='.pos{font-weight:bold;margin-right:4px;}', need_wrap_css=True, wrap_class='bing')
|
|
|
|
|
def _css(self, val):
|
|
|
|
|
return val
|
|
|
|
|
|
|
|
|
|
@export(u'释义', 4)
|
|
|
|
|
def fld_definition(self):
|
|
|
|
|
val = self._get_field('def')
|
|
|
|
|
if val == None or val == '':
|
|
|
|
|
return ''
|
|
|
|
|
return self._css(val)
|