mirror of
https://codeberg.org/hyperreal/get-def
synced 2025-01-18 16:33:45 +01:00
fix: handle exceptions
This commit is contained in:
parent
7214e12905
commit
89240d27a7
35
get_def.py
35
get_def.py
@ -25,16 +25,26 @@ from rich.text import Text
|
||||
def main():
|
||||
args = docopt(__doc__)
|
||||
|
||||
api_url = "https://api.dictionaryapi.dev/api/v2/entries/en/"
|
||||
word_url = api_url + args["WORD"]
|
||||
api_url = f"https://api.dictionaryapi.dev/api/v2/entries/en/{args['WORD']}"
|
||||
|
||||
response = requests.get(word_url, timeout=60)
|
||||
|
||||
if response.status_code == 404:
|
||||
exit("Sorry, we couldn't find definitions for the word you were looking for.")
|
||||
try:
|
||||
response = requests.get(api_url, timeout=30)
|
||||
if response.status_code == 404:
|
||||
exit(
|
||||
"Sorry, we couldn't find definitions for the word you were looking for."
|
||||
)
|
||||
except requests.Timeout:
|
||||
exit(
|
||||
"The connection has timed out. This might indicate an issue with DNS, firewall, or your internet connection."
|
||||
)
|
||||
|
||||
word = response.json()[0].get("word")
|
||||
|
||||
console = Console(width=100)
|
||||
print()
|
||||
print(" :arrow_forward: ", Text(word, style="bold red", justify="center"))
|
||||
print()
|
||||
|
||||
phonetics = response.json()[0].get("phonetics")
|
||||
phonetics_table = Table(box=box.SQUARE)
|
||||
phonetics_table.add_column("Phonetic Text", style="cyan")
|
||||
@ -44,22 +54,15 @@ def main():
|
||||
text = item.get("text") if item.get("text") else "None"
|
||||
audio = item.get("audio") if item.get("audio") else "None"
|
||||
phonetics_table.add_row(text, audio)
|
||||
|
||||
meanings = response.json()[0].get("meanings")
|
||||
|
||||
console = Console(width=100)
|
||||
print()
|
||||
print(" :arrow_forward: ", Text(word, style="bold red", justify="center"))
|
||||
print()
|
||||
|
||||
console.print(phonetics_table)
|
||||
console.print(phonetics_table)
|
||||
|
||||
print(
|
||||
"Click to view [link=https://www.internationalphoneticassociation.org/IPAcharts/inter_chart_2018/IPA_2018.html]Interactive IPA chart[/link]"
|
||||
)
|
||||
print()
|
||||
|
||||
# console.rule()
|
||||
meanings = response.json()[0].get("meanings")
|
||||
|
||||
for item in meanings:
|
||||
print(f"[bold]{meanings.index(item) + 1}. [underline]{item["partOfSpeech"]}")
|
||||
for definition in item["definitions"]:
|
||||
|
@ -1,6 +1,6 @@
|
||||
[project]
|
||||
name = "get-def"
|
||||
version = "0.3"
|
||||
version = "0.4"
|
||||
authors = [
|
||||
{ name="Jeffrey Serio", email="hyperreal@fedoraproject.org" },
|
||||
]
|
||||
|
Loading…
Reference in New Issue
Block a user