-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdictionary.py
60 lines (57 loc) · 2.41 KB
/
dictionary.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import requests
import logging
logger = logging.getLogger(__name__)
class dictionary_class:
def __init__(self):
pass
async def get_definitions(self,word):
url = f"https://api.dictionaryapi.dev/api/v2/entries/en/{word}"
try:
response = requests.get(url)
definitions_rich_text = []
if response.json() is not None and response.status_code == 200:
meanings = response.json()[0]["meanings"]
url = response.json()[0]["sourceUrls"]
for meaning in meanings:
definition_rich_text = [
{
'type': 'text',
'text': {
'content': f"{meaning['partOfSpeech']}: ",
'link': {
"url":f"{url[0]}#{str(meaning['partOfSpeech']).capitalize()}"
}
},
'annotations': {
'bold': True,
'italic': False,
'strikethrough': False,
'underline': False,
'code': False,
'color': 'default'
},
'plain_text': f"{meaning['partOfSpeech']}: ",
'href': f"{url[0]}#{str(meaning['partOfSpeech']).capitalize()}"
},
{
'type': 'text',
'text': {
'content': f"{meaning['definitions'][0]['definition']}\n",
'link': None
},
'annotations': {
'bold': False,
'italic': True,
'strikethrough': False,
'underline': False,
'code': False,
'color': 'default'
},
'plain_text': f"{meaning['definitions'][0]['definition']}\n",
'href': None
}
]
definitions_rich_text.extend(definition_rich_text)
return definitions_rich_text
except:
logger.error("Dictionary request error")