fix: handle exceptions

This commit is contained in:
Jeffrey Serio 2025-01-01 01:11:08 -06:00
parent 7214e12905
commit 89240d27a7
2 changed files with 20 additions and 17 deletions

View File

@ -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"]
response = requests.get(word_url, timeout=60)
api_url = f"https://api.dictionaryapi.dev/api/v2/entries/en/{args['WORD']}"
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.")
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,14 +54,6 @@ 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)
print(
@ -59,7 +61,8 @@ def main():
)
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"]:

View File

@ -1,6 +1,6 @@
[project]
name = "get-def"
version = "0.3"
version = "0.4"
authors = [
{ name="Jeffrey Serio", email="hyperreal@fedoraproject.org" },
]