Assorted shell and Python scripts
at main 125 lines 3.6 kB view raw
1#!/usr/bin/env -S uv run --script 2# /// script 3# dependencies = [ 4# "docopt", 5# "requests", 6# "rich", 7# ] 8# /// 9 10"""get-def 11 12Usage: 13 get-def (WORD) 14 get-def -n (WORD) 15 get-def -h 16 17Examples: 18 get-def hello 19 get-def -n hello 20 21Options: 22 -n, --no-pager do not use a pager 23 -h, --help show this help message and exit 24""" 25 26import requests 27from docopt import docopt 28from rich import box 29from rich.console import Console 30from rich.padding import Padding 31from rich.table import Table 32from rich.text import Text 33 34 35def print_def(console: Console, response: requests.Response): 36 word = response.json()[0].get("word") 37 38 console.print() 39 console.print(" :arrow_forward: ", Text(word, style="bold red", justify="center")) 40 console.print() 41 42 phonetics = response.json()[0].get("phonetics") 43 phonetics_table = Table(box=box.SQUARE) 44 phonetics_table.add_column("Phonetic Text", style="cyan") 45 phonetics_table.add_column("Phonetic Audio") 46 if len(phonetics) > 0: 47 for item in phonetics: 48 text = item.get("text") if item.get("text") else "None" 49 audio = item.get("audio") if item.get("audio") else "None" 50 phonetics_table.add_row(text, audio) 51 console.print(phonetics_table) 52 53 console.print( 54 "IPA chart: https://www.internationalphoneticassociation.org/IPAcharts/inter_chart_2018/IPA_2018.html" 55 ) 56 console.print() 57 58 meanings = response.json()[0].get("meanings") 59 60 for item in meanings: 61 console.print( 62 f"[bold]{meanings.index(item) + 1}. [underline]{item['partOfSpeech']}" 63 ) 64 for definition in item["definitions"]: 65 console.print( 66 Padding( 67 f"[bold blue]Definition:[/bold blue] {definition.get('definition')}", 68 (0, 0, 0, 3), 69 ) 70 ) 71 if definition.get("example") is not None: 72 console.print( 73 Padding( 74 f"[bold magenta]Example:[/bold magenta] {definition.get('example')}", 75 (0, 0, 0, 3), 76 ) 77 ) 78 if definition.get("synonyms"): 79 console.print( 80 Padding( 81 "[bold yellow]Synonyms:[/bold yellow] " 82 + ", ".join(definition.get("synonyms")), 83 (0, 0, 0, 3), 84 ) 85 ) 86 if definition.get("antonyms"): 87 console.print( 88 Padding( 89 "[bold yellow]Antonyms:[/bold yellow] " 90 + ", ".join(definition.get("antonyms")), 91 (0, 0, 0, 3), 92 ) 93 ) 94 console.print() 95 96 97def main(): 98 args = docopt(__doc__) 99 100 api_url = f"https://api.dictionaryapi.dev/api/v2/entries/en/{args['WORD']}" 101 102 try: 103 response = requests.get(api_url, timeout=30) 104 if response.status_code == 404: 105 exit( 106 "Sorry, we couldn't find definitions for the word you were looking for." 107 ) 108 except requests.Timeout: 109 exit( 110 "The connection has timed out. This might indicate an issue with DNS, firewall, or your internet connection." 111 ) 112 113 console = Console(width=100) 114 115 if not args["--no-pager"]: 116 with console.pager(styles=True): 117 print_def(console, response) 118 else: 119 print_def(console, response) 120 121 122if __name__ == "__main__": 123 main() 124 125# vim: ai et ft=python sts=4 sw=4 ts=4