2019-04-12 20:43:02 +08:00
|
|
|
|
# -*- coding:utf-8 -*-
|
2018-07-30 16:00:47 +08:00
|
|
|
|
import os
|
2019-01-13 11:55:07 +08:00
|
|
|
|
import re
|
|
|
|
|
|
|
2019-04-12 21:02:25 +08:00
|
|
|
|
from bs4 import Tag
|
|
|
|
|
|
|
2018-07-30 16:00:47 +08:00
|
|
|
|
from ..base import *
|
2018-07-27 00:08:40 +08:00
|
|
|
|
|
2018-10-29 23:11:09 +08:00
|
|
|
|
cambridge_url_base = u'https://dictionary.cambridge.org/'
|
2018-07-27 00:08:40 +08:00
|
|
|
|
cambridge_download_mp3 = True
|
2018-08-19 20:50:00 +08:00
|
|
|
|
cambridge_download_img = True
|
2018-07-27 00:08:40 +08:00
|
|
|
|
|
2019-03-20 22:56:41 +08:00
|
|
|
|
|
2018-07-27 00:08:40 +08:00
|
|
|
|
class Cambridge(WebService):
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
2019-03-09 10:17:42 +08:00
|
|
|
|
super().__init__()
|
2018-07-27 00:08:40 +08:00
|
|
|
|
|
2018-10-28 22:44:50 +08:00
|
|
|
|
def _get_url(self):
|
|
|
|
|
|
return cambridge_url_base
|
|
|
|
|
|
|
2018-07-30 16:00:47 +08:00
|
|
|
|
def _get_from_api(self):
|
2018-10-28 22:44:50 +08:00
|
|
|
|
data = self.get_response(u'{0}{1}'.format(self._get_url(), self.quote_word))
|
2018-07-27 00:08:40 +08:00
|
|
|
|
soup = parse_html(data)
|
|
|
|
|
|
result = {
|
|
|
|
|
|
'pronunciation': {'AmE': '', 'BrE': '', 'AmEmp3': '', 'BrEmp3': ''},
|
2018-08-19 20:50:00 +08:00
|
|
|
|
'image': '',
|
|
|
|
|
|
'thumb': '',
|
2018-10-28 11:44:46 +08:00
|
|
|
|
'def': '',
|
|
|
|
|
|
'def_list': []
|
2018-07-27 00:08:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-03-20 22:56:41 +08:00
|
|
|
|
# english
|
2019-09-11 19:57:09 +08:00
|
|
|
|
element = soup.find('div', class_='page')
|
2018-07-27 00:08:40 +08:00
|
|
|
|
if element:
|
2019-03-20 22:56:41 +08:00
|
|
|
|
# 页
|
2019-09-11 19:57:09 +08:00
|
|
|
|
elements = element.find_all('div', class_='entry-body__el')
|
2018-10-28 11:44:46 +08:00
|
|
|
|
header_found = False
|
|
|
|
|
|
for element in elements:
|
|
|
|
|
|
if element:
|
2019-03-20 22:56:41 +08:00
|
|
|
|
# 音
|
2018-10-28 11:44:46 +08:00
|
|
|
|
if not header_found:
|
|
|
|
|
|
header = element.find('div', class_='pos-header')
|
|
|
|
|
|
if header:
|
2019-09-11 19:57:09 +08:00
|
|
|
|
tags = header.find_all('span', class_='dpron-i')
|
2018-10-28 11:44:46 +08:00
|
|
|
|
if tags:
|
|
|
|
|
|
for tag in tags:
|
|
|
|
|
|
r = tag.find('span', class_='region')
|
|
|
|
|
|
reg = r.get_text() if r else u''
|
|
|
|
|
|
pn = 'AmE' if reg=='us' else 'BrE'
|
|
|
|
|
|
p = tag.find('span', class_='pron')
|
|
|
|
|
|
result['pronunciation'][pn] = p.get_text() if p else u''
|
2019-09-11 19:57:09 +08:00
|
|
|
|
snd = tag.find('source', type='audio/mpeg')
|
2018-10-28 11:44:46 +08:00
|
|
|
|
if snd:
|
2019-09-11 19:57:09 +08:00
|
|
|
|
result['pronunciation'][pn+'mp3'] = cambridge_url_base + snd.get('src')
|
2018-10-28 11:44:46 +08:00
|
|
|
|
header_found = True
|
2019-02-22 20:59:28 +08:00
|
|
|
|
|
2019-03-20 22:56:41 +08:00
|
|
|
|
# 义
|
2019-09-11 19:57:09 +08:00
|
|
|
|
senses = element.find_all('div', class_='pos-body')
|
2019-04-12 20:43:02 +08:00
|
|
|
|
# 词性
|
2019-09-11 19:57:09 +08:00
|
|
|
|
span_posgram = element.find('div', class_='posgram')
|
2019-04-12 21:37:18 +08:00
|
|
|
|
pos_gram = (span_posgram.get_text() if span_posgram else '')
|
2019-04-12 20:43:02 +08:00
|
|
|
|
|
2019-03-20 22:56:41 +08:00
|
|
|
|
if senses:
|
|
|
|
|
|
for sense in senses:
|
2019-04-12 20:43:02 +08:00
|
|
|
|
# 像ambivalent之类词语含有ambivalence解释,词性不同
|
2019-04-12 21:56:20 +08:00
|
|
|
|
runon_title = None
|
2019-04-12 21:37:18 +08:00
|
|
|
|
if sense['class'][0] == 'runon':
|
|
|
|
|
|
runon_pos = sense.find('span', class_='pos')
|
|
|
|
|
|
runon_gram = sense.find('span', class_='gram')
|
|
|
|
|
|
if runon_pos is not None:
|
2019-04-12 21:56:20 +08:00
|
|
|
|
pos_gram = runon_pos.get_text() + (runon_gram.get_text() if runon_gram else '')
|
|
|
|
|
|
h3_rt = sense.find('h3', class_='runon-title')
|
|
|
|
|
|
runon_title = (h3_rt.get_text() if h3_rt else None)
|
2019-04-12 20:43:02 +08:00
|
|
|
|
|
2019-04-12 21:16:54 +08:00
|
|
|
|
sense_body = sense.find('div', class_=re.compile("sense-body|runon-body pad-indent"))
|
2019-04-12 20:43:02 +08:00
|
|
|
|
|
2019-04-12 21:02:25 +08:00
|
|
|
|
if sense_body:
|
2019-03-20 22:56:41 +08:00
|
|
|
|
l = result['def_list']
|
2019-04-14 13:05:02 +08:00
|
|
|
|
|
|
|
|
|
|
def extract_sense(block, phrase=None):
|
2019-04-12 21:02:25 +08:00
|
|
|
|
if isinstance(block, Tag) is not True:
|
2019-04-14 13:05:02 +08:00
|
|
|
|
return
|
2019-04-12 21:02:25 +08:00
|
|
|
|
|
|
|
|
|
|
block_type = block['class'][0]
|
|
|
|
|
|
if block_type == 'def-block':
|
|
|
|
|
|
pass
|
|
|
|
|
|
elif block_type == 'phrase-block':
|
2019-04-14 13:05:02 +08:00
|
|
|
|
_phrase_header = block.find('span', class_='phrase-head')
|
|
|
|
|
|
_phrase_body = block.find('div', class_='phrase-body pad-indent')
|
|
|
|
|
|
if _phrase_body:
|
|
|
|
|
|
for p_b in _phrase_body:
|
|
|
|
|
|
extract_sense(p_b, _phrase_header.get_text() if _phrase_header else None)
|
|
|
|
|
|
return
|
2019-04-12 21:16:54 +08:00
|
|
|
|
elif block_type == 'runon-body':
|
|
|
|
|
|
pass
|
2019-04-12 21:02:25 +08:00
|
|
|
|
else:
|
2019-04-14 13:05:02 +08:00
|
|
|
|
return
|
2019-04-12 21:02:25 +08:00
|
|
|
|
|
2019-04-12 21:45:25 +08:00
|
|
|
|
span_df = block.find('span', class_='def-info')
|
|
|
|
|
|
def_info = (span_df.get_text().replace('›', '') if span_df else '')
|
2019-09-11 19:57:09 +08:00
|
|
|
|
d = block.find('div', class_='def')
|
2019-04-12 21:02:25 +08:00
|
|
|
|
tran = block.find('span', class_='trans')
|
2019-09-11 19:57:09 +08:00
|
|
|
|
examps = block.find_all('span', class_='eg deg')
|
2019-03-20 22:56:41 +08:00
|
|
|
|
l.append(
|
2019-04-12 21:56:20 +08:00
|
|
|
|
u'<li>{0}{1}{2}{3}{4} {5}{6}</li>'.format(
|
2019-04-12 21:45:25 +08:00
|
|
|
|
'<span class="epp-xref">{0}</span>'.format(pos_gram) if pos_gram != '' else '',
|
2019-04-12 21:56:20 +08:00
|
|
|
|
'<span class="epp-xref">{0}</span>'.format(runon_title) if runon_title else '',
|
2019-04-12 21:37:18 +08:00
|
|
|
|
'<span class="epp-xref">{0}</span>'.format(phrase) if phrase else '',
|
2019-04-12 21:45:25 +08:00
|
|
|
|
'<span class="epp-xref">{0}</span>'.format(def_info) if def_info.strip() != '' else '',
|
2019-04-12 21:02:25 +08:00
|
|
|
|
'<b class="def">{0}</b>'.format(d.get_text()) if d else u'',
|
2019-03-20 22:56:41 +08:00
|
|
|
|
|
2019-04-12 21:16:54 +08:00
|
|
|
|
'<span class="trans">{0}</span>'.format(tran.get_text()) if tran else '',
|
2019-03-20 22:56:41 +08:00
|
|
|
|
u''.join(
|
|
|
|
|
|
u'<div class="examp">{0}</div>'.format(e.get_text()) if e else u''
|
|
|
|
|
|
for e in examps
|
|
|
|
|
|
)
|
2018-10-28 11:44:46 +08:00
|
|
|
|
)
|
|
|
|
|
|
)
|
2019-04-14 13:05:02 +08:00
|
|
|
|
|
|
|
|
|
|
for b in sense_body:
|
|
|
|
|
|
extract_sense(b)
|
2019-03-20 22:56:41 +08:00
|
|
|
|
result['def'] = u'<ul>' + u''.join(s for s in l) + u'</ul>'
|
|
|
|
|
|
img = sense.find('img', class_='lightboxLink')
|
|
|
|
|
|
if img:
|
|
|
|
|
|
result['image'] = cambridge_url_base + img.get('data-image')
|
|
|
|
|
|
result['thumb'] = cambridge_url_base + img.get('src')
|
2018-07-27 00:08:40 +08:00
|
|
|
|
|
|
|
|
|
|
return self.cache_this(result)
|
2018-08-19 20:50:00 +08:00
|
|
|
|
|
2018-07-27 00:08:40 +08:00
|
|
|
|
@with_styles(need_wrap_css=True, cssfile='_cambridge.css')
|
|
|
|
|
|
def _css(self, val):
|
|
|
|
|
|
return val
|
|
|
|
|
|
|
|
|
|
|
|
@export('AME_PHON')
|
|
|
|
|
|
def fld_phonetic_us(self):
|
|
|
|
|
|
seg = self._get_field('pronunciation')
|
|
|
|
|
|
return seg.get('AmE', u'') if seg else u''
|
|
|
|
|
|
|
|
|
|
|
|
@export('BRE_PHON')
|
|
|
|
|
|
def fld_phonetic_uk(self):
|
|
|
|
|
|
seg = self._get_field('pronunciation')
|
|
|
|
|
|
return seg.get('BrE', u'') if seg else u''
|
|
|
|
|
|
|
2018-08-19 20:50:00 +08:00
|
|
|
|
def _fld_img(self, fld):
|
|
|
|
|
|
image_url = self._get_field(fld)
|
|
|
|
|
|
if cambridge_download_img and image_url:
|
|
|
|
|
|
filename = get_hex_name(self.unique.lower(), image_url, 'jpg')
|
|
|
|
|
|
if os.path.exists(filename) or self.net_download(filename, image_url):
|
|
|
|
|
|
return self.get_anki_label(filename, 'img')
|
|
|
|
|
|
return ''
|
|
|
|
|
|
|
2018-07-27 00:08:40 +08:00
|
|
|
|
def _fld_mp3(self, fld):
|
|
|
|
|
|
audio_url = self._get_field('pronunciation')[fld]
|
|
|
|
|
|
if cambridge_download_mp3 and audio_url:
|
2018-07-30 17:42:14 +08:00
|
|
|
|
filename = get_hex_name(self.unique.lower(), 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-27 00:08:40 +08:00
|
|
|
|
return self.get_anki_label(filename, 'audio')
|
|
|
|
|
|
return ''
|
|
|
|
|
|
|
2018-08-19 20:50:00 +08:00
|
|
|
|
@export('IMAGE')
|
|
|
|
|
|
def fld_image(self):
|
|
|
|
|
|
return self._fld_img('image')
|
|
|
|
|
|
|
2018-08-27 01:21:47 +08:00
|
|
|
|
@export([u'缩略图', u'Thumbnails'])
|
2018-08-19 20:50:00 +08:00
|
|
|
|
def fld_thumbnail(self):
|
|
|
|
|
|
return self._fld_img('thumb')
|
|
|
|
|
|
|
2018-07-27 00:08:40 +08:00
|
|
|
|
@export('AME_PRON')
|
|
|
|
|
|
def fld_mp3_us(self):
|
|
|
|
|
|
return self._fld_mp3('AmEmp3')
|
|
|
|
|
|
|
|
|
|
|
|
@export('BRE_PRON')
|
|
|
|
|
|
def fld_mp3_uk(self):
|
|
|
|
|
|
return self._fld_mp3('BrEmp3')
|
2018-08-19 20:50:00 +08:00
|
|
|
|
|
2018-07-27 00:08:40 +08:00
|
|
|
|
@export('DEF')
|
|
|
|
|
|
def fld_definition(self):
|
|
|
|
|
|
val = self._get_field('def')
|
|
|
|
|
|
if val == None or val == '':
|
|
|
|
|
|
return ''
|
|
|
|
|
|
return self._css(val)
|