-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdictocaller.py
104 lines (84 loc) · 3.44 KB
/
dictocaller.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
#!/usr/bin/env python3
import requests
import bs4
import pgi
import pyperclip
from pynput import keyboard
from pynput.keyboard import Key, Controller
pgi.require_version('Notify', '0.7')
from pgi.repository import Notify
def show_notif(word, alert):
#display notif
Notify.init('Dictionary')
notif = Notify.Notification.new(word, alert, 'dialog-information')
notif.set_urgency(2)
notif.show()
#clean up
Notify.uninit()
def decision():
if 'https://www.dictionary.com/misspelling' in page.url: #if word is not found in dictionary
alert = 'Word/phrase not found. Is that english?'
print(clipboard + ' not found.')
show_notif(clipboard, alert) #display notification
else: #if word is found
word = soup.find('h1').text #name of word
pron = soup.find('span', {'class':'pron-spell-content css-z3mf2 evh0tcl2'}).text #pronunciation
sec_children = soup.find('div', {'class':'css-1urpfgu e16867sm0'}).contents #this div's children
if len(sec_children) > 1: #format 1 in dictionary.com
define = pron + '\n'
for child in sec_children[1:]: #iterate through children from element 1
pos = child.contents[0].text
define += pos + '\n'
counter = 1 #numbering the definitions
for defs in child.contents[1]:
define += str(counter) + '. ' + defs.text + '\n'
counter += 1
if 'SEE MORESEE LESS' in define:
define = define.replace('SEE MORESEE LESS','')
print(clipboard + ' defined.')
show_notif(word, define) #display notification
define = ''
else: #format 2 in dictionary.com
define = ''
sec_children = soup.find('div', {'class':'default-content'}).contents #format 2's div's children
for child in sec_children[1:]: #iterate through children
pos = child.contents[0].text
define += pos + '\n'
counter = 1 #numbering the definitions
for defs in child.contents[1]:
define += str(counter) + '. ' + defs.text + '\n'
counter += 1
if 'SEE MORESEE LESS' in define:
define = define.replace('SEE MORESEE LESS','')
print(clipboard + ' defined.')
show_notif(word, define) #display notification
define = ''
def on_activate():
controller = Controller()
#simulate key presses to copy to clipboard
controller.press(Key.ctrl)
controller.press('c')
controller.release(Key.ctrl)
controller.release('c')
print('Copied to clipboard.')
global clipboard
clipboard = pyperclip.paste() #returns contents of clipboard
print('Clipboard extracted.')
#scraping the definition
url = 'https://www.dictionary.com/browse/'+clipboard
try:
global page
global soup
page = requests.get(url)
soup = bs4.BeautifulSoup(page.text, 'lxml')
print('Soup ready.')
decision()
except Exception:
show_notif('Error','Request did not through. Check your internet connection.')
if __name__ == '__main__':
combo = '<alt>+t' #change this if you want something else
try:
with keyboard.GlobalHotKeys({combo: on_activate}) as h:
h.join()
except KeyboardInterrupt:
print('\nProgram terminated')