-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyDict.py
60 lines (45 loc) · 1.97 KB
/
pyDict.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
from bs4 import BeautifulSoup
import requests
from datetime import datetime
import keyboard
import time
from tkinter import Tk
from win10toast import ToastNotifier
fileHandler = open('dictionary.txt', 'a')
listHandler = open('list.txt', 'a')
session = 0
toast = ToastNotifier()
while True:
if keyboard.is_pressed('ctrl + c'):
time.sleep(1)
word = Tk().clipboard_get()
try:
URL = "https://www.vocabulary.com/dictionary/" + word
page = requests.get(URL)
content = BeautifulSoup(page.content, 'html.parser')
results = content.find(class_='definition')
definition = ' '.join(results.text.split()[1:])
toast.show_toast(word, definition, duration=5, icon_path = "Dic.ico", threaded=True)
except AttributeError:
toast.show_toast("Error", "Please a single word", duration=5, icon_path = "Dic.ico", threaded=True)
elif keyboard.is_pressed('ctrl + i'):
if (session == 0):
fileHandler.write('-'*35 + 'New Session' + '(' + str(datetime.now()) + ')' + '-'*35 + '\n')
session = 1
flag = 0
listHandler = open('list.txt', 'r')
for w in listHandler:
if word in w:
toast.show_toast(word, "Already added to the dictionary", duration=5, icon_path = "Dic.ico", threaded=True)
flag = 1
break
if (flag == 0):
toast.show_toast(word, "Added to the dictionary!✌", duration=5, icon_path = "Dic.ico", threaded=True)
fileHandler.write('#' + word.upper() + '\n')
fileHandler.write(definition.lower() + '\n' + '\n')
listHandler = open('list.txt', 'a')
listHandler.write(word + '\n')
listHandler.close()
elif keyboard.is_pressed('ctrl + e'):
break
fileHandler.close()