anki-word-query/addons21/fastwq/service/dict/bing.py

127 lines
4.3 KiB
Python
Raw Normal View History

2018-07-01 10:55:30 +08:00
#-*- coding:utf-8 -*-
import re
2018-07-30 16:00:47 +08:00
import os
from ..base import *
2018-07-01 10:55:30 +08:00
2018-07-08 03:21:16 +08:00
bing_download_mp3 = True
2018-07-01 10:55:30 +08:00
2018-07-08 03:21:16 +08:00
@register([u'Bing', u'Bing'])
2018-07-01 10:55:30 +08:00
class Bing(WebService):
def __init__(self):
super(Bing, self).__init__()
2018-07-30 16:00:47 +08:00
def _get_from_api(self):
2019-08-19 09:21:45 +08:00
data = self.get_response(u"https://cn.bing.com/dict/search?q={}&mkt=zh-cn".format(self.quote_word))
2018-07-13 22:08:35 +08:00
soup = parse_html(data)
2018-07-07 22:52:59 +08:00
result = {
2018-07-08 03:21:16 +08:00
'pronunciation': {'AmE': '', 'BrE': '', 'AmEmp3': '', 'BrEmp3': ''},
2018-09-06 08:31:26 +08:00
'def': [],
'sams': [],
2018-07-07 22:52:59 +08:00
}
2018-07-08 03:21:16 +08:00
#音
element = soup.find('div', class_='hd_tf_lh')
2018-07-01 10:55:30 +08:00
if element:
2018-07-08 03:21:16 +08:00
audios = element.find_all('a')
#美式英标
2019-08-19 09:21:45 +08:00
tag = element.find('div', class_='hd_prUS')
2018-07-08 03:21:16 +08:00
if tag:
2018-07-30 16:00:47 +08:00
result['pronunciation']['AmE'] = str(tag)
2018-07-08 03:21:16 +08:00
#美音
if audios:
tag = audios[0]
audio_url = tag.get('onclick')
if audio_url:
result['pronunciation']['AmEmp3'] = u''.join(re.findall(r'https://.*\.mp3', audio_url))
2018-07-08 03:21:16 +08:00
#英式音标
2019-08-19 09:21:45 +08:00
tag = element.find('div', class_='hd_pr')
2018-07-08 03:21:16 +08:00
if tag:
2018-07-30 16:00:47 +08:00
result['pronunciation']['BrE'] = str(tag)
2018-07-08 03:21:16 +08:00
#英音
if audios:
tag = audios[1]
audio_url = tag.get('onclick')
if audio_url:
result['pronunciation']['BrEmp3'] = u''.join(re.findall(r'https://.*\.mp3', audio_url))
2018-07-08 03:21:16 +08:00
#释义
element = soup.find('div', class_='qdef')
2018-07-01 10:55:30 +08:00
if element:
element = getattr(element, 'ul', '')
if element:
result['def'] = u''.join([str(content) for content in element.contents])
2018-07-08 03:21:16 +08:00
#例句
element = soup.find('div', id='sentenceSeg')
if element:
#英文例句
tags = element.find_all('div', {"class": 'sen_en'})
result['sams'] = [{'eng': u''.join(tag.find_all(text=True))} for tag in tags]
#例句翻译
tags = element.find_all('div', {"class": 'sen_cn'})
for i, tag in enumerate(tags):
result['sams'][i]['chn'] = u''.join(tag.find_all(text=True))
2018-07-01 10:55:30 +08:00
return self.cache_this(result)
2018-07-08 03:21:16 +08:00
@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-13 22:08:35 +08:00
@export('AME_PHON')
2018-07-01 10:55:30 +08:00
def fld_phonetic_us(self):
2018-07-08 03:21:16 +08:00
seg = self._get_field('pronunciation')
2018-07-26 16:00:11 +08:00
return seg.get('AmE', u'') if seg else u''
2018-07-01 10:55:30 +08:00
2018-07-13 22:08:35 +08:00
@export('BRE_PHON')
2018-07-01 10:55:30 +08:00
def fld_phonetic_uk(self):
2018-07-08 03:21:16 +08:00
seg = self._get_field('pronunciation')
2018-07-26 16:00:11 +08:00
return seg.get('BrE', u'') if seg else u''
2018-07-01 10:55:30 +08:00
2018-07-08 03:21:16 +08:00
def _fld_mp3(self, fld):
2018-09-19 08:48:32 +08:00
seg = self._get_field('pronunciation')
audio_url = seg[fld] if seg else u''
2018-07-08 03:21:16 +08:00
if bing_download_mp3 and audio_url:
2018-07-30 17:42:14 +08:00
filename = get_hex_name('bing', audio_url, 'mp3')
2018-07-30 16:00:47 +08:00
if os.path.exists(filename) or self.net_download(filename, audio_url):
2018-07-12 00:06:19 +08:00
return self.get_anki_label(filename, 'audio')
2018-07-08 03:21:16 +08:00
return ''
2018-07-01 10:55:30 +08:00
2018-07-13 22:08:35 +08:00
@export('AME_PRON')
2018-07-08 03:21:16 +08:00
def fld_mp3_us(self):
return self._fld_mp3('AmEmp3')
2018-07-13 22:08:35 +08:00
@export('BRE_PRON')
2018-07-08 03:21:16 +08:00
def fld_mp3_uk(self):
return self._fld_mp3('BrEmp3')
2018-07-01 10:55:30 +08:00
2018-07-13 22:08:35 +08:00
@export('DEF')
2018-07-01 10:55:30 +08:00
def fld_definition(self):
val = self._get_field('def')
if val == None or val == '':
return ''
return self._css(val)
2018-07-08 03:21:16 +08:00
2018-07-13 22:08:35 +08:00
@export('EXAMPLE')
2018-07-08 03:21:16 +08:00
def fld_samples(self):
max_numbers = 10
segs = self._get_field('sams')
2018-09-15 19:05:59 +08:00
if segs:
sentences = u''
for i, seg in enumerate(segs):
sentences += u"""<li><div class="se_li1">
<div class="sen_en">{0}.{1}</div>
<div class="sen_cn">{2}</div>
</div></li>""".format(i+1, seg['eng'], seg['chn'])
if i == 9:
break
if sentences:
return u"""<div class="se_div">
<div class="sentenceCon">
<div id="sentenceSeg"><ol>{0}</ol></div>
</div>
</div>""".format(sentences)
return u''