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

67 lines
1.9 KiB
Python
Raw Normal View History

2018-07-01 10:55:30 +08:00
#-*- coding:utf-8 -*-
2018-07-28 10:25:19 +08:00
from ..base import WebService, export, register, with_styles, parse_html
2018-07-01 10:55:30 +08:00
@register(u'海词迷你词典')
class MiniDict(WebService):
def __init__(self):
super(MiniDict, self).__init__()
2018-07-28 10:25:19 +08:00
def _get_content(self):
data = self.get_response(u"http://apii.dict.cn/mini.php?q={}".format(self.word))
soup = parse_html(data)
result = {
'expressions': u'',
'sentences': u'',
'variations': u'',
'phonetic': u'',
}
# 音标
tag = soup.find('span', class_='p')
if tag:
result['phonetic'] = str(tag.get_text()).encode('utf-8')
tag.decompose()
# 基本释义
tag = soup.find('div', id='e')
if tag:
result['expressions'] = str(tag).encode('utf-8')
tag.decompose()
# 例句与用法
tag = soup.find('div', id='s')
if tag:
result['sentences'] = str(tag).encode('utf-8')
tag.decompose()
# 词形变化
tag = soup.find('div', id='t')
if tag:
result['variations'] = str(tag).encode('utf-8')
tag.decompose()
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)
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-28 10:25:19 +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-28 10:25:19 +08:00
return self._get_field('expressions')
2018-07-01 10:55:30 +08:00
2018-07-13 22:08:35 +08:00
@export(u'例句与用法')
2018-07-28 10:25:19 +08:00
@with_styles(css='em {color:#cc0066;font-style:normal;}', need_wrap_css=True, wrap_class='minidict')
2018-07-01 10:55:30 +08:00
def fld_sentences(self):
2018-07-28 10:25:19 +08:00
return self._get_field('sentences')
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_variations(self):
2018-07-28 10:25:19 +08:00
return self._get_field('variations')