-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathultron.py
590 lines (437 loc) · 15.2 KB
/
ultron.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
import pyttsx3
import speech_recognition as sr
import datetime
import os
import cv2
import wikipedia
import webbrowser
import pywhatkit as kit
import smtplib
import sys
import time
import pyjokes
import pyautogui
import requests
from bs4 import BeautifulSoup
import pyowm
import json
import PyPDF2
from tkinter import Tk # from tkinter import Tk for Python 3.x
from tkinter.filedialog import askopenfilename
import shutil
import PyPDF2
from requests import get
engine = pyttsx3.init()
voices = engine.getProperty('voices')
print(voices)
voice_id = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_ZIRA_11.0"
rates ={'slow':100 , 'medium':200 , "fast":300}
r = 'fast'
print(rates[r])
engine.setProperty('voices',voice_id)
# engine.setProperty('rate', rates[r] )
#tts
def speak(audio):
engine.say(audio)
# print(audio)
engine.runAndWait()
# speech recognition
def takeCommand():
try:
r=sr.Recognizer()
with sr.Microphone() as source:
print("listening...")
r.pause_threshold=3
audio = r.listen(source,timeout=5,phrase_time_limit=5)
print("Recognizing...")
query = r.recognize_google(audio , language='en-in')
print(f"user said: {query}")
query=query.lower()
# speak(query)
except Exception as e:
# speak("Say that again please...")
return "none"
return query
def wish():
hour = datetime.datetime.now().hour
if hour>=0 and hour<=12:
speak("Good Morning")
elif hour>=12 and hour <=18:
speak("Good Afternoon")
else:
speak("Good evening")
speak("I am Ultron sir, please tell me what to do")
def remove(string):
return string.replace(" ", "")
def setAlarm(tim):
# time.sleep(5)
# x,y = pyautogui.position()
# print(x,y)
# pyautogui.click(195,216)
#
tim = tim.split()
if(len(tim[0]) ==3 ):
h= tim[0][0:1]
h = int(h)
# print(int(h))
m= tim[0][1:3]
m=int(m)
# print(int(m))
elif(len(tim[0]) ==4 ):
h= tim[0][0:1]
h = int(h)
print(int(h))
m= tim[0][2:4]
m=int(m)
print(int(m))
elif len(tim[0]) ==5:
h= tim[0][0:2]
h = int(h)
print(int(h))
m= tim[0][3:5]
m=int(m)
print(int(m))
time.sleep(2)
pyautogui.click(650, 1100)
time.sleep(2)
pyautogui.press('c')
time.sleep(1)
pyautogui.press('l')
pyautogui.press('o')
time.sleep(1)
pyautogui.press('c')
time.sleep(1)
pyautogui.press('k')
time.sleep(5)
pyautogui.click(650, 350)
time.sleep(13)
for i in range(6):
pyautogui.press('tab')
pyautogui.press('enter')
time.sleep(5)
x,y = pyautogui.position()
print(len(tim[0]), tim[0],)
for i in range(5):
pyautogui.press('up')
for i in range(h):
pyautogui.press('up')
pyautogui.press('tab')
for i in range (m) :
# print(i)
pyautogui.press('up')
pyautogui.press('tab')
if(tim[1][0]=="p"):
pyautogui.press('up')
for i in range(6):
pyautogui.press('tab')
pyautogui.press('enter')
time.sleep(2)
pyautogui.keyDown('alt')
pyautogui.press('f4')
time.sleep(1)
pyautogui.keyUp('alt')
speak(f"Alarm has been set at {tim}")
# # for t in tim:
# # print(t)
def sendEmail(to,content):
server=smtplib.SMTP('smtp.gmail.com', 587)
# server.ehlo()
server.starttls()
server.login('[email protected]', 'ymuv nqni njyw zxll')
server.sendmail('[email protected]', to , content)
server.close()
def pdf_reader():
Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
filename = askopenfilename()
# show an "Open" dialog box and return the path to the selected file
filename=filename.replace("/" ,"\\\\")
print(filename)
givenFile = filename [ filename.rindex("\\")+1 : ]
shutil.copy(filename , "C:\\Users\\LENOVO 7XIN\\OneDrive\\Desktop\\ULTRON\\Mypdfs")
book = open(f"Mypdfs/{givenFile}" , "rb")
pdfReader =PyPDF2.PdfFileReader(book)
pages = pdfReader.numPages
speak("Enter the page number ")
pg = takeCommand()
s= pg.split()
# print(s)
x = int (s[1]
)
# print(x-1)
page = pdfReader.getPage(x-1)
text = page.extractText()
print(text)
speak(text)
def changeRate():
speak("What speed do you want")
r= takeCommand().lower()
engine.setProperty('rate', rates[r])
def taskExecution():
wish()
while True :
# if 1:
query = takeCommand().lower()
if "open notepad" in query:
npath="C:\\Windows\\notepad.exe"
os.startfile(npath)
elif "open the notepad" in query:
npath="C:\\Windows\\notepad.exe"
os.startfile(npath)
elif "change rate" in query:
changeRate()
elif "change the rate" in query:
changeRate()
elif "open command prompt" in query:
cpath="C:\\Users\\LENOVO 7XIN\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\System Tools\\Command Prompt.lnk"
os.startfile(cpath)
elif "open the command prompt" in query:
cpath="C:\\Users\\LENOVO 7XIN\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\System Tools\\Command Prompt.lnk"
os.startfile(cpath)
elif "open camera" in query:
cap=cv2.VideoCapture(0)
while True:
ret,img = cap.read()
cv2.imshow('webcam' , img )
k= cv2.waitKey(50)
if k==27:
break
cap.release()
cv2.destroyAllWindows()
elif "open the camera" in query:
cap=cv2.VideoCapture(0)
while True:
ret,img = cap.read()
cv2.imshow('webcam' , img )
k= cv2.waitKey(50)
if k==27:
break
cap.release()
cv2.destroyAllWindows()
elif "close camera" in query:
pyautogui.press('esc')
# elif "open camera" in query:
elif "close the camera" in query:
pyautogui.press('esc')
elif "play music" in query:
speak("What song, Sir ?")
song = takeCommand()
song = song+'.mp3'
music_dir = "D:\\music"
songs = os.listdir(music_dir)
song = remove(song).lower()
for s in songs:
if song == s:
os.startfile(os.path.join(music_dir,song))
elif "play some music" in query:
speak("What song, Sir ?")
song = takeCommand()
song = song+'.mp3'
music_dir = "D:\\music"
songs = os.listdir(music_dir)
song = remove(song).lower()
for s in songs:
if song == s:
os.startfile(os.path.join(music_dir,song))
elif "play the music" in query:
speak("What song, Sir ?")
song = takeCommand()
song = song+'.mp3'
music_dir = "D:\\music"
songs = os.listdir(music_dir)
song = remove(song).lower()
for s in songs:
if song == s:
os.startfile(os.path.join(music_dir,song))
# os.startfile(os.path.join(music_dir,songs[0]))
elif "ip address" in query:
ip= get("https://api.ipify.org").text
speak(f" Your ip address is {ip}")
elif "wikipedia" in query:
speak("Searching wikipedia...")
query=query.replace("wikipedia","")
print(query)
results = wikipedia.summary(query, sentences=2)
speak("Accoring to wikipedia")
speak(results)
print(results)
# elif "stop ultron" in query:
# break
elif "open youtube" in query:
webbrowser.open("youtube.com")
elif "open stack overflow" in query:
webbrowser.open("stackoverflow.com")
elif "open google" in query:
speak("Sir, what should I search on google")
cm= takeCommand().lower()
webbrowser.open(f"{cm}")
elif "send message through whatsapp" in query:
# speak("")
webbrowser.open("https://web.whatsapp.com")
time.sleep(15)
speak("to who")
user = remove(takeCommand().lower())
pyautogui.click(195,216)
for i in user:
pyautogui.press(i)
pyautogui.press("enter")
time.sleep(3)
speak("What is the message?")
msg = takeCommand().lower()
for i in msg:
pyautogui.press(i)
pyautogui.press('enter')
time.sleep(2)
pyautogui.keyDown('alt')
pyautogui.press('tab')
time.sleep(1)
pyautogui.keyUp('alt')
no= remove(takeCommand())
# no = "+91"+no
# kit.sendwhatmsg(no, "asfasfdsfs" , 12,19 )
elif "send some message through whatsapp" in query:
# speak("")
webbrowser.open("https://web.whatsapp.com")
time.sleep(15)
speak("to who")
user = remove(takeCommand().lower())
pyautogui.click(195,216)
for i in user:
pyautogui.press(i)
pyautogui.press("enter")
time.sleep(3)
speak("What is the message?")
msg = takeCommand().lower()
for i in msg:
pyautogui.press(i)
pyautogui.press('enter')
time.sleep(2)
pyautogui.keyDown('alt')
pyautogui.press('tab')
time.sleep(1)
pyautogui.keyUp('alt')
no= remove(takeCommand())
# no = "+91"+no
elif "play songs on youtube" in query:
speak("Sir, which song?")
song = takeCommand().lower()
kit.playonyt(song)
time.sleep(5)
elif "play some songs on youtube" in query:
speak("Sir, which song?")
song = takeCommand().lower()
kit.playonyt(song)
time.sleep(5)
elif "send email" in query:
try:
speak("What should I say?")
content = takeCommand().lower()
speak("email ?")
to = takeCommand().lower()
to=remove(to)
# to = "[email protected]"
print(remove(to))
print(content)
sendEmail(to , content)
speak("Email has been sent")
except Exception as e:
print(e)
speak("Some error occurred")
elif "send an email" in query:
try:
speak("What should I say?")
content = takeCommand().lower()
speak("email ?")
to = takeCommand().lower()
to=remove(to)
# to = "[email protected]"
print(remove(to))
print(content)
sendEmail(to , content)
speak("Email has been sent")
except Exception as e:
print(e)
speak("Some error occurred")
elif "no thanks" in query:
speak("Thanks for using me ")
sys.exit()
# elif "wait a minute" in query:
# speak("Sir, how much time")
# time.sleep(10)
elif "close notepad" in query:
speak("okay sir, closing notepad")
os.system("taskkill /f /im notepad.exe")
elif "close the notepad" in query:
speak("okay sir, closing notepad")
os.system("taskkill /f /im notepad.exe")
# elif "set alarm" in query:
# speak("")
elif "set alarm" in query:
speak("Setting an alarm")
time.sleep(1)
speak("What time?")
alarm = takeCommand().lower()
setAlarm(alarm)
elif "set an alarm" in query:
speak("Setting an alarm")
time.sleep(1)
speak("What time?")
alarm = takeCommand().lower()
setAlarm(alarm)
elif "tell me a joke" in query:
joke = pyjokes.get_joke()
speak(joke)
elif "tell a joke" in query:
joke = pyjokes.get_joke()
speak(joke)
elif "switch the tab" in query:
pyautogui.keyDown('alt')
pyautogui.press('tab')
time.sleep(1)
pyautogui.keyUp('alt')
elif "close the tab" in query:
pyautogui.keyDown('alt')
pyautogui.press('f4')
time.sleep(1)
pyautogui.keyUp('alt')
elif "temperature" in query:
res = requests.get('https://api.openweathermap.org/data/2.5/weather?lat=17.4122998&lon=78.267959&appid=94d65bb26e67c1b54dabcec6a215fdcc&=celsius')
response = json.loads(res.text)
speak(f"the temperature of hyderabad is { round(response['main']['temp']-273 , 2 )}")
elif "activate how to do mode" in query:
from pywikihow import search_wikihow
speak("How to do mode is activated, please tell me what you want to know")
how = takeCommand()
max_results=1
how_to= search_wikihow(how,max_results)
assert len(how_to) == 1
how_to[0].print()
speak(how_to[0].summary )
elif "read pdf" in query:
pdf_reader()
elif "read the pdf" in query:
pdf_reader()
elif "hello" in query:
speak("Hello sir, may I help you with something")
elif "hey" in query:
speak("Hello sir, may I help you with something")
elif "how are you" in query:
speak("I am fine sir, how about you")
elif "also good" in query:
speak("That's great to hear from you")
elif "fine" in query:
speak("That's great to hear from you")
elif "thank you" in query:
speak("It's my pleasure sir")
elif "thanks" in query:
speak("It's my pleasure sir")
elif "pause" in query:
speak("Ok sir, I am going to sleep. You can call me anytime")
break
if __name__=="__main__":
while True:
permission = takeCommand()
if "wake up" in permission:
taskExecution()
elif "goodbye" in permission:
speak("Thanks for using me sir, have a good day")
sys.exit()