diff --git a/get_def.py b/get_def.py index 3280a10..6294169 100644 --- a/get_def.py +++ b/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"]: diff --git a/pyproject.toml b/pyproject.toml index 0c9d906..0e83d87 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "get-def" -version = "0.3" +version = "0.4" authors = [ { name="Jeffrey Serio", email="hyperreal@fedoraproject.org" }, ]