get-def/get_def.py

117 lines
3.4 KiB
Python
Raw Normal View History

2024-12-30 06:13:56 +01:00
#!/usr/bin/env python3
"""get-def
Usage:
get-def (WORD)
2025-01-08 02:12:24 +01:00
get-def -n (WORD)
2024-12-30 06:13:56 +01:00
get-def -h
Examples:
get-def hello
2025-01-08 02:12:24 +01:00
get-def -n hello
2024-12-30 06:13:56 +01:00
Options:
2025-01-08 02:12:24 +01:00
-n, --no-pager do not use a pager
2024-12-30 06:13:56 +01:00
-h, --help show this help message and exit
"""
import requests
from docopt import docopt
2025-01-08 02:12:24 +01:00
from rich import box
2024-12-30 06:13:56 +01:00
from rich.console import Console
from rich.padding import Padding
from rich.table import Table
from rich.text import Text
2025-01-08 02:12:24 +01:00
def print_def(console: Console, response: requests.Response):
2024-12-30 06:13:56 +01:00
word = response.json()[0].get("word")
2025-01-08 02:12:24 +01:00
console.print()
console.print(" :arrow_forward: ", Text(word, style="bold red", justify="center"))
console.print()
2025-01-01 08:11:08 +01:00
2024-12-30 06:13:56 +01:00
phonetics = response.json()[0].get("phonetics")
phonetics_table = Table(box=box.SQUARE)
phonetics_table.add_column("Phonetic Text", style="cyan")
phonetics_table.add_column("Phonetic Audio")
if len(phonetics) > 0:
for item in phonetics:
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)
2025-01-01 08:11:08 +01:00
console.print(phonetics_table)
2024-12-30 06:13:56 +01:00
2025-01-08 02:12:24 +01:00
console.print(
"IPA chart: https://www.internationalphoneticassociation.org/IPAcharts/inter_chart_2018/IPA_2018.html"
2024-12-30 06:13:56 +01:00
)
2025-01-08 02:12:24 +01:00
console.print()
2024-12-30 06:13:56 +01:00
2025-01-01 08:11:08 +01:00
meanings = response.json()[0].get("meanings")
2024-12-30 06:13:56 +01:00
for item in meanings:
2025-01-08 02:12:24 +01:00
console.print(
f"[bold]{meanings.index(item) + 1}. [underline]{item["partOfSpeech"]}"
)
2024-12-30 06:13:56 +01:00
for definition in item["definitions"]:
2025-01-08 02:12:24 +01:00
console.print(
2024-12-30 06:13:56 +01:00
Padding(
f"[bold blue]Definition:[/bold blue] {definition.get("definition")}",
(0, 0, 0, 3),
)
)
if definition.get("example") is not None:
2025-01-08 02:12:24 +01:00
console.print(
2024-12-30 06:13:56 +01:00
Padding(
f"[bold magenta]Example:[/bold magenta] {definition.get("example")}",
(0, 0, 0, 3),
)
)
if definition.get("synonyms"):
2025-01-08 02:12:24 +01:00
console.print(
2024-12-30 06:13:56 +01:00
Padding(
f"[bold yellow]Synonyms:[/bold yellow] "
+ ", ".join(definition.get("synonyms")),
(0, 0, 0, 3),
)
)
if definition.get("antonyms"):
2025-01-08 02:12:24 +01:00
console.print(
2024-12-30 06:13:56 +01:00
Padding(
f"[bold yellow]Antonyms:[/bold yellow] "
+ ", ".join(definition.get("antonyms")),
(0, 0, 0, 3),
)
)
2025-01-08 02:12:24 +01:00
console.print()
def main():
args = docopt(__doc__)
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."
)
except requests.Timeout:
exit(
"The connection has timed out. This might indicate an issue with DNS, firewall, or your internet connection."
)
console = Console(width=100)
if not args["--no-pager"]:
with console.pager(styles=True):
print_def(console, response)
else:
print_def(console, response)
2024-12-30 06:13:56 +01:00
if __name__ == "__main__":
main()