-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.py
368 lines (296 loc) · 12.5 KB
/
project.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import argparse
import requests
import sys
import json
import time
import os.path
import playsound
import eng_to_ipa as p
from rich.progress import Progress
from rich import print
from rich.panel import Panel
from rich.prompt import Confirm, Prompt
from rich.console import Console
from rich.table import Table
from rich.theme import Theme
from rich import box
from gtts import gTTS
from jsonschema import ValidationError, validate
# Global variables
# API URL
API_URL = "https://api.dictionaryapi.dev/api/v2/entries/en/"
# Custom theme for display messages
custom_theme = Theme({
"info": "dim cyan",
"warning": "magenta",
"danger": "bold red"
})
# Initializing console object
console = Console(theme=custom_theme)
# Function to get the response of the word
def get_details(word):
try:
# Progress bar hack for api request. This has nothing to do with api request.
with Progress(transient=True) as progress:
task = progress.add_task("[green]Fetching data...", total=10)
while not progress.finished:
progress.update(task, advance=0.9)
time.sleep(0.2)
response = requests.get(f"{API_URL}{word}", timeout=2)
if response.status_code == 200:
return response.json()
elif response.status_code == 404:
console.print(
"Are you sure that word exists?", style="info")
sys.exit(1)
elif response.status_code >= 400 and response.status_code < 500:
console.print("Client error.", style="danger")
sys.exit(2)
elif response.status_code >= 500 and response.status_code < 600:
console.print(
"A server error occured. Try again later.", style="danger")
sys.exit(3)
except requests.Timeout:
console.print(
"Error: Connection timed out. Try again later.", style="danger")
sys.exit(5)
except requests.ConnectionError:
console.print(
"Error: A connection error occured.", style="danger")
sys.exit(6)
# Function to play the pronunciation of the word
def pronunce_word(word):
tts = gTTS(text=word, lang='en', slow=True)
filename = "pronunciation.mp3"
tts.save(filename)
# Progress bar hack for pronunciation.
with Progress(transient=True) as progress:
task = progress.add_task("[green]Playing pronunciation...", total=10)
while not progress.finished:
progress.update(task, advance=0.9)
time.sleep(0.2)
while True:
playsound.playsound(filename)
hear_again = Confirm.ask(
"Do you want to hear again", default=True)
if hear_again == False:
break
os.remove(filename)
# Function to validate json
def validate_json(json_data):
try:
with open("schema.json") as schema_file:
schema = json.load(schema_file)
validate(instance=json_data, schema=schema)
except ValidationError:
print("bookmarks.json not matching schema. Did you change something?")
sys.exit(8)
except FileNotFoundError:
print("Could not find schema.json file. Did you remove it?")
sys.exit(4)
# Function to print the details from response
def print_details(details):
# Initalizing a table object
table = Table(title="Search Results", padding=1)
# Adding coloumns to the table
table.add_column("Part of speech", justify="left",
style="#FAEA48 bold")
table.add_column("Defintion", justify="left", style="#66BFBF")
table.add_column("Example", justify="center", style="#3AB4F2")
word = details[0]["word"]
phonetic = p.ipa_list(word)
print(Panel(f"Word : {word.title()}, Phonetic : [italic]{phonetic}",
title=f"Word : {word.title()}", border_style="green", style="bold"))
# List of synonym_antonym objects
synonym_antonym_list = []
# Adding the partofspeech, definition and example to the table
for i in range(len(details)):
meanings = details[i]["meanings"]
for meaning in meanings:
part_of_speech = meaning["partOfSpeech"]
# Append the synonyms and antonyms to the list
synonym_antonym_list.append(
{"partOfSpeech": part_of_speech, "synonyms": meaning["synonyms"], "antonyms": meaning["antonyms"]})
definitions = meaning["definitions"]
for definition in definitions:
example = "__"
if "example" in definition:
example = definition["example"]
table.add_row(part_of_speech,
definition["definition"], example)
# Printing the table
console.print(table)
# Print synonyms and antonyms
print_synonyms_antonyms(synonym_antonym_list)
return word
# Function to print the synonyms and antonyms of the word
def print_synonyms_antonyms(synonym_antonym_list):
# Initializing table for synonyms and antonyms
synonym_antonym_table = Table(
title="Synonyms and antonyms", padding=1, box=box.ROUNDED)
# Adding columns for part of speech, synonym and antonym
synonym_antonym_table.add_column("Part of speech", justify="center",
style="#FAEA48 bold")
synonym_antonym_table.add_column(
"Synonyms", justify="center", style="#66BFBF")
synonym_antonym_table.add_column(
"Antonyms", justify="center", style="#3AB4F2")
# Adding rows for each synonym antonym
for synonym_antonym_dict in synonym_antonym_list:
part_of_speech = synonym_antonym_dict["partOfSpeech"]
synonyms = ", ".join(synonym_antonym_dict["synonyms"])
antonyms = ", ".join(synonym_antonym_dict["antonyms"])
if len(synonyms) == 0:
synonyms = "__"
if len(antonyms) == 0:
antonyms = "__"
synonym_antonym_table.add_row(
part_of_speech, synonyms, antonyms)
# Printing the synonym antonym table
console.print(synonym_antonym_table)
# Function to bookmark the searched word for later review
def bookmark_word(json_details):
# Path to the bookmarks file
path = Prompt.ask(
"[cyan]Enter the path where you want to bookmark the file : ", default=".")
try:
# Check if the bookmarks.json file exists
if not os.path.exists(f"{os.path.join(path, 'bookmarks.json')}"):
json_list = []
json_list.append(json_details)
json_object = json.dumps(json_list, indent=4)
# Write the json object to bookmarks.json
with open(f"{os.path.join(path, 'bookmarks.json')}", "w") as file:
file.write(json_object)
print(
f"[green]Created bookmarks.json at {path} and bookmarked word successfully.")
# If bookmarks.json already exist
else:
# Read the file and deserialize it into a python object
with open(f"{os.path.join(path, 'bookmarks.json')}", "r") as file:
json_list = json.load(file)
# Append the new word details into the json list
json_list.append(json_details)
# Serialize the json list
json_object = json.dumps(json_list, indent=4)
# Write the json object to a file
with open(f"{os.path.join(path, 'bookmarks.json')}", "w") as file:
file.write(json_object)
print(
f"[green]Bookmarked word to bookmarks.json file successfully.")
except json.JSONDecodeError:
print("[red bold]An error occured while reading the json file.")
sys.exit(5)
# Function to print bookmarked words
def print_bookmarked_words(path):
# Exit if bookmarks.json does not exist
if not os.path.exists(f"{os.path.join(path, 'bookmarks.json')}"):
print(f"[red bold]bookmarks.json does not exist in {path}")
sys.exit(4)
try:
with open(f"{os.path.join(path, 'bookmarks.json')}", "r") as file:
word_json_list = json.load(file)
validate_json(word_json_list)
bookmarked_words = ""
for word_details in word_json_list:
bookmarked_words += "✳️" + word_details[0]["word"] + "\n"
print(Panel(bookmarked_words, box=box.DOUBLE_EDGE,
title="Bookmarked Words", style="cyan"))
except json.JSONDecodeError:
print("[red bold]An error occured while parsing the bookmarks.json file")
sys.exit(5)
# Function to print bookmarked words along with their details
def print_bookmarks(path):
try:
# Exit if bookmarks.json does not exist
if not os.path.exists(f"{os.path.join(path, 'bookmarks.json')}"):
print(f"[red bold]bookmarks.json does not exist in {path}")
sys.exit(4)
with open(f"{os.path.join(path, 'bookmarks.json')}", "r") as file:
word_json_list = json.load(file)
validate_json(word_json_list)
console.rule("[bold red]Bookmarks")
for word_details in word_json_list:
print_details(word_details)
console.rule(style="#6E85B7")
except json.JSONDecodeError:
print("[red bold]An error occured while parsing the bookmarks.json file")
sys.exit(5)
# Function to delete bookarked word
def delete_bookmarks(delete_word, path):
try:
# Exit if bookmarks.json does not exist
if not os.path.exists(f"{os.path.join(path, 'bookmarks.json')}"):
print(f"[red bold]bookmarks.json does not exist in {path}")
sys.exit(4)
word_found = False
with open(f"{os.path.join(path, 'bookmarks.json')}", "r") as file:
word_json_list = json.load(file)
for index, word_details in enumerate(word_json_list):
word = word_details[0]["word"]
if word == delete_word:
print(f"Deleting {word}")
word_json_list.pop(index)
print(f"[green]Deleted word successfully")
word_found = True
if not word_found:
print("[red]That word is not bookmarked.")
if word_found:
with open(f"{os.path.join(path, 'bookmarks.json')}", "w") as file:
json_object = json.dumps(word_json_list, indent=4)
file.write(json_object)
print(
f"[green]Modified bookmarks.json")
except json.JSONDecodeError:
print("[red bold]An error occured while parsing the bookmarks.json file")
sys.exit(5)
# Main function
def main():
# Intializing parser object
parser = argparse.ArgumentParser(
description="Commandline dictionary utility")
group = parser.add_mutually_exclusive_group(required=True)
# Adding an argument to view bookmarked words
group.add_argument("-bw", help="View bookmarked words only",
metavar="<path to bookmarks>")
# Adding an argument to view bookmarked words along with their meanings
group.add_argument("-bm", help="View bookmarked words and their meanings",
metavar="<path to bookmarks>")
group.add_argument("-d", nargs="+", help="Delete bookmarked word",
metavar="<word> <path to bookmarks>")
# Adding word argument to parser
group.add_argument(
"-w", help="Get details about the given word", metavar="<word>")
# Output of parsed arguments
args = parser.parse_args()
if args.d:
try:
delete_bookmarks(args.d[0], args.d[1])
except IndexError:
print(f"[bold green]Path not provided, defaulting to {(os.getcwd())}")
delete_bookmarks(args.d[0], ".")
# Show the bookmarked words
if args.bw:
print_bookmarked_words(args.bw)
# Show the bookmarked words and their meanings
if args.bm:
print_bookmarks(args.bm)
# Find details about the given word
if args.w:
word = args.w
# Calling the get_details function
details = get_details(word)
# Printing the details
word_returned = print_details(details)
# Pronunce the word
pronunce = Confirm.ask(
"[yellow]Do you want the pronunciation of this word?", default=True)
if pronunce:
pronunce_word(word_returned)
# Bookmark the word
bookmark_current_word = Confirm.ask(
"[yellow]Do you want to bookmark this word?", default=True)
if bookmark_current_word:
bookmark_word(details)
if __name__ == "__main__":
main()