generated from dannz510/dannzheart.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_control.py
257 lines (217 loc) · 6.56 KB
/
app_control.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
import pyscreenshot as ImageGrab
import time
import subprocess
from pynput.keyboard import Key, Controller
import psutil
class SystemTasks:
def __init__(self):
self.keyboard = Controller()
def openApp(self, appName):
appName = appName.replace('paint', 'mspaint')
appName = appName.replace('wordpad', 'write')
appName = appName.replace('word', 'write')
appName = appName.replace('calculator', 'calc')
try: subprocess.Popen('C:\\Windows\\System32\\'+appName[5:]+'.exe')
except: pass
def write(self, text):
text = text[5:]
for char in text:
self.keyboard.type(char)
time.sleep(0.02)
def select(self):
self.keyboard.press(Key.ctrl)
self.keyboard.press('a')
self.keyboard.release('a')
self.keyboard.release(Key.ctrl)
def hitEnter(self):
self.keyboard.press(Key.enter)
self.keyboard.release(Key.enter)
def delete(self):
self.keyboard.press(Key.backspace)
self.keyboard.release(Key.enter)
def save(self, text):
if "don't" in text:
self.keyboard.press(Key.right)
else:
self.keyboard.press(Key.ctrl)
self.keyboard.press('s')
self.keyboard.release('s')
self.keyboard.release(Key.ctrl)
self.hitEnter()
class TabOpt:
def __init__(self):
self.keyboard = Controller()
def switchTab(self):
self.keyboard.press(Key.ctrl)
self.keyboard.press(Key.tab)
self.keyboard.release(Key.tab)
self.keyboard.release(Key.ctrl)
def closeTab(self):
self.keyboard.press(Key.ctrl)
self.keyboard.press('w')
self.keyboard.release('w')
self.keyboard.release(Key.ctrl)
def newTab(self):
self.keyboard.press(Key.ctrl)
self.keyboard.press('n')
self.keyboard.release('n')
self.keyboard.release(Key.ctrl)
class WindowOpt:
def __init__(self):
self.keyboard = Controller()
def openWindow(self):
self.maximizeWindow()
def closeWindow(self):
self.keyboard.press(Key.alt_l)
self.keyboard.press(Key.f4)
self.keyboard.release(Key.f4)
self.keyboard.release(Key.alt_l)
def minimizeWindow(self):
for i in range(2):
self.keyboard.press(Key.cmd)
self.keyboard.press(Key.down)
self.keyboard.release(Key.down)
self.keyboard.release(Key.cmd)
time.sleep(0.05)
def maximizeWindow(self):
self.keyboard.press(Key.cmd)
self.keyboard.press(Key.up)
self.keyboard.release(Key.up)
self.keyboard.release(Key.cmd)
def moveWindow(self, operation):
self.keyboard.press(Key.cmd)
if "left" in operation:
self.keyboard.press(Key.left)
self.keyboard.release(Key.left)
elif "right" in operation:
self.keyboard.press(Key.right)
self.keyboard.release(Key.right)
elif "down" in operation:
self.keyboard.press(Key.down)
self.keyboard.release(Key.down)
elif "up" in operation:
self.keyboard.press(Key.up)
self.keyboard.release(Key.up)
self.keyboard.release(Key.cmd)
def switchWindow(self):
self.keyboard.press(Key.alt_l)
self.keyboard.press(Key.tab)
self.keyboard.release(Key.tab)
self.keyboard.release(Key.alt_l)
def takeScreenShot(self):
from random import randint
im = ImageGrab.grab()
im.save(f'Files and Document/ss_{randint(1, 100)}.jpg')
def isContain(text, lst):
for word in lst:
if word in text:
return True
return False
def Win_Opt(operation):
w = WindowOpt()
if isContain(operation, ['open']):
w.openWindow()
elif isContain(operation, ['close']):
w.closeWindow()
elif isContain(operation, ['mini']):
w.minimizeWindow()
elif isContain(operation, ['maxi']):
w.maximizeWindow()
elif isContain(operation, ['move', 'slide']):
w.moveWindow(operation)
elif isContain(operation, ['switch','which']):
w.switchWindow()
elif isContain(operation, ['screenshot','capture','snapshot']):
w.takeScreenShot()
return
def Tab_Opt(operation):
t = TabOpt()
if isContain(operation, ['new','open','another','create']):
t.newTab()
elif isContain(operation, ['switch','move','another','next','previous','which']):
t.switchTab()
elif isContain(operation, ['close','delete']):
t.closeTab()
else:
return
def System_Opt(operation):
s = SystemTasks()
if 'delete' in operation:
s.delete()
elif 'save' in operation:
s.save(operation)
elif 'type' in operation:
s.write(operation)
elif 'select' in operation:
s.select()
elif 'enter' in operation:
s.hitEnter()
elif isContain(operation, ['notepad','paint','calc','word']):
s.openApp(operation)
elif isContain(operation, ['music','video']):
s.playMusic(operation)
else:
open_website(operation)
return
###############################
########### VOLUME ###########
###############################
keyboard = Controller()
def mute():
for i in range(50):
keyboard.press(Key.media_volume_down)
keyboard.release(Key.media_volume_down)
def full():
for i in range(50):
keyboard.press(Key.media_volume_up)
keyboard.release(Key.media_volume_up)
def volumeControl(text):
if 'full' in text or 'max' in text: full()
elif 'mute' in text or 'min' in text: mute()
elif 'incre' in text:
for i in range(5):
keyboard.press(Key.media_volume_up)
keyboard.release(Key.media_volume_up)
elif 'decre' in text:
for i in range(5):
keyboard.press(Key.media_volume_down)
keyboard.release(Key.media_volume_down)
def systemInfo():
import wmi
c = wmi.WMI()
my_system_1 = c.Win32_LogicalDisk()[0]
my_system_2 = c.Win32_ComputerSystem()[0]
info = ["Total Disk Space: " + str(round(int(my_system_1.Size)/(1024**3),2)) + " GB",
"Free Disk Space: " + str(round(int(my_system_1.Freespace)/(1024**3),2)) + " GB",
"Manufacturer: " + my_system_2.Manufacturer,
"Model: " + my_system_2. Model,
"Owner: " + my_system_2.PrimaryOwnerName,
"Number of Processors: " + str(my_system_2.NumberOfProcessors),
"System Type: " + my_system_2.SystemType]
return info
def batteryInfo():
# usage = str(psutil.cpu_percent(interval=0.1))
battery = psutil.sensors_battery()
pr = str(battery.percent)
if battery.power_plugged:
return "Your System is currently on Charging Mode and it's " + pr + "% done."
return "Your System is currently on " + pr + "% battery life."
def OSHandler(query):
if isContain(query, ['system', 'info']):
return ['Here is your System Information...', '\n'.join(systemInfo())]
elif isContain(query, ['cpu', 'battery']):
return batteryInfo()
from difflib import get_close_matches
import json
from random import choice
import webbrowser
data = json.load(open('C:/Users/TTC/Documents/Code/PersonalAssistantChatbot-master/assets/websites.json', encoding='utf-8'))
def open_website(query):
query = query.replace('open','')
if query in data:
response = data[query]
else:
query = get_close_matches(query, data.keys(), n=2, cutoff=0.5)
if len(query)==0: return "None"
response = choice(data[query[0]])
webbrowser.open(response)