Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# 'summary': {
# 'word': ...,
# 'pronounce': [('KK', '...'), (...)], // optional.
# // e.g. 'google'
# 'explain': [(optional)], # 'hospitalized' is summary-only
# 'grammar': [(optional)],
# },
# 'explain': [...],
# 'verbose': [(optional)],
# }
# Construct summary (required)
try:
content['summary'] = self.parse_summary(data, word)
except AttributeError:
raise NotFoundError(word)
# Handle explain (required)
try:
content['explain'] = self.parse_explain(data)
except IndexError:
raise NotFoundError(word)
# Extract verbose (optional)
content['verbose'] = self.parse_verbose(data)
record = Record(
word=word,
content=json.dumps(content),
source=self.provider,
)
return record
lambda x: re.match(r'(.*)(\[.*\])', x).groups(),
p.find('ul').text.strip().split()
)
)
def get_grammar(d: bs4.element.Tag):
s = ('div#web ol.searchCenterMiddle '
'div.dictionaryWordCard > ul > li')
return list(map(text, d.select(s)))
node = data.select_one('div#web ol.searchCenterMiddle')
node = node.select('div.sys_dict_word_card > div.grp-main > div')
p = None # optional
if node is None or len(node) <= 1: # e.g. "fabor"
raise NotFoundError(word)
elif len(node) == 2: # e.g. "apples"
w, e = node
elif len(node) == 3: # e.g. ?
w, _, e = node
elif len(node) == 4: # e.g. ?
w, _, _, e = node
elif len(node) == 5: # e.g. "metadata"
w, p, _, _, e = node
elif len(node) == 6:
w, p, _, _, _, e = node
return {
'word': w.find('span').text.strip(),
'pronounce': get_pronounce(p) if p else [], # optional
'explain': get_explain(e),
'grammar': get_grammar(data), # optional
if record:
self.show(record)
return
try:
record = self.query(word)
except exceptions.NoNetworkError as e:
self.color.print(e, 'red')
print()
except exceptions.TimeoutError as e:
self.color.print(e, 'red')
print()
except exceptions.APIKeyError as e:
self.color.print(e, 'red')
print()
except exceptions.NotFoundError as e:
self.color.print(e, 'yellow')
print()
except Exception:
import traceback
traceback.print_exc()
url = "https://github.com/zdict/zdict/issues"
print()
print("We have problem for this word 😢")
print("Please report this word to {}".format(url))
print("Dictionary: {}".format(self.title))
print("Word: '{}'".format(word))
import sys
sys.exit(1)
else:
self.save(record, word)
self.show(record)
def query(self, word: str):
try:
content = self._get_raw(word)
except QueryError as exception:
raise NotFoundError(exception.word)
record = Record(
word=word,
content=content,
source=self.provider,
)
return record
def query(self, word: str):
content = self._get_raw(word)
content_json = json.loads(content)
if not content_json['data']:
raise NotFoundError(word)
record = Record(
word=word,
content=content,
source=self.provider,
)
return record
def query(self, word: str):
content_str = self._get_raw(word)
content_dict = json.loads(content_str)
if content_dict['list'] == []:
raise NotFoundError(word)
record = Record(
word=word,
content=content_str,
source=self.provider,
)
return record
def query(self, word: str):
try:
content = self._get_raw(word)
except QueryError as exception:
raise NotFoundError(exception.word)
content = json.loads(content)
try:
# Get the first definition string from JSON.
content = content['en']
except KeyError:
# API can return JSON that does not contain 'en' language.
raise NotFoundError(word)
# Define a list that will be used to create a Record.
r_content = []
# For every part of speech append r_content corresponding list.
for i, d in enumerate(content):
# Add what part of speech current definitions refers to.
try:
content = self._get_raw(word)
except QueryError as exception:
raise NotFoundError(exception.word)
content_json = json.loads(content)
status = content_json.get('code')
if status != 200:
# https://tech.yandex.com/translate/doc/dg/reference/translate-docpage/#codes
message = self.status_code.get(
status,
'Some bad thing happened with Yandex'
)
print('Yandex: ' + message)
raise NotFoundError(word)
record = Record(
word=word,
content=content,
source=self.provider,
)
return record
def query(self, word: str):
try:
content = self._get_raw(word)
except QueryError as exception:
raise NotFoundError(exception.word)
content = json.loads(content)
try:
# Get the first definition string from JSON.
content = content['en']
except KeyError:
# API can return JSON that does not contain 'en' language.
raise NotFoundError(word)
# Define a list that will be used to create a Record.
r_content = []
# For every part of speech append r_content corresponding list.
for i, d in enumerate(content):
# Add what part of speech current definitions refers to.
r_content.append({'part_of_speech': d['partOfSpeech']})
# Create a list that will store english_definitions
# of the current part of speech.
r_content[i]['definitions'] = []
for j, d2 in enumerate(d['definitions']):
# Parse definition and append definitions list.
definition = BeautifulSoup(d2['definition'],
def query(self, word: str):
try:
app_id, app_key = self._get_app_key()
content = self._get_raw(word, headers={
'app_id': app_id,
'app_key': app_key
})
except QueryError as exception:
msg = self.status_code.get(exception.status_code,
'Some bad thing happened')
self.color.print('Oxford: ' + msg, 'red')
raise NotFoundError(exception.word)
record = Record(
word=word,
content=content,
source=self.provider,
)
return record