-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPA.py
591 lines (478 loc) · 23 KB
/
PA.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
591
from PyQt5 import QtWidgets, uic
import sys
from PyQt5.uic.properties import QtCore
from PyQt5.QtGui import QPixmap
from PyQt5.QtMultimedia import QSound
from PyQt5.QtWidgets import QMainWindow, QApplication, QFileDialog
import pyttsx3 # pip install pyttsx3
import datetime
import speech_recognition as sr # pip install SpeechRecognition
import wikipedia # pip install wikipedia
import webbrowser
import os
import sys
import smtplib
from email.message import EmailMessage
import pywhatkit # pip install pywhatkit
import MyAlarm # user-defined
import pyjokes # pip install pyjokes
from speedtest import Speedtest # pip install speedtest-cli
from pywikihow import search_wikihow # pip install pywikihow
import pyautogui # pip install pyAutoGUI
import poetpy # pip install poetpy
import random
from forex_python.converter import CurrencyRates # pip install forex-python
import requests # pip install requests
import bs4 # pip install beautifulsoup4
import time
import wolframalpha # pip install wolframalpha
from quote import quote # pip install quote
import winshell as winshell # pip install winshell
from geopy.geocoders import Nominatim # pip install geopy and pip install geocoder
from geopy import distance
from playsound import playsound
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
def fun_talk(audio):
engine.say(audio)
engine.runAndWait()
def wish_user():
hour = int(datetime.datetime.now().hour)
if hour >= 0 and hour < 12:
fun_talk("Good Morning !")
elif hour >= 12 and hour < 18:
fun_talk("Good Afternoon !")
else:
fun_talk("Good Evening !")
fun_talk("I am Py-siri, Press Spacebar or click listen to start")
def get_command():
rec = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
rec.pause_threshold = 1
audio = rec.listen(source)
try:
print("Recognizing...")
query = rec.recognize_google(audio, language='en-in')
print(f"User said: {query}\n")
playsound('success.mp3')
except Exception as e:
playsound('fail.mp3')
print("Say that again please...")
return "None"
return query
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
uic.loadUi('PA.ui', self)
self.start.clicked.connect(self.do_command)
self.setWindowTitle("Python Assistant")
self.show()
wish_user()
# def keyPressEvent(self, event):
# if event.key() == QtCore.Qt.Key_Return:
# self.do_command()
def do_command(self):
playsound('listen.mp3')
query = get_command().lower()
if 'wikipedia' in query:
fun_talk('Searching Wikipedia')
query = query.replace("wikipedia", "")
results = wikipedia.summary(query, sentences=3)
fun_talk("According to Wikipedia")
print(results)
fun_talk(results)
elif 'open youtube' in query:
webbrowser.open("www.youtube.com")
elif 'open spotify' in query:
webbrowser.open("www.spotify.com")
elif 'open google' in query:
webbrowser.open("www.google.com")
elif 'open cu login' in query:
webbrowser.open("uims.cuchd.in/uims/")
elif 'open blackboard' in query:
webbrowser.open("cuchd.blackboard.com")
elif 'open stack overflow' in query:
webbrowser.open("stackoverflow.com")
elif 'the time' in query:
strTime = datetime.datetime.now().strftime("%H:%M:%S")
fun_talk(f"The time is {strTime}")
elif 'the date' in query:
strDate = datetime.datetime.today().strftime('%Y-%m-%d')
print(strDate)
fun_talk(f"The date is {strDate}")
elif 'open visual studio code' in query:
os.startfile("C:\\Users\\91954\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\"
"Programs\\Visual Studio Code\\Visual Studio Code")
elif 'open eclipse' in query:
os.startfile("C:\\Users\\91954\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\"
"Programs\\Eclipse\\Eclipse IDE for Java Developers - 2020-06")
elif 'open notepad' in query:
os.startfile("C:\\Users\\91954\\AppData\\Roaming\\Microsoft\\Windows\\"
"Start Menu\\Programs\\Accessories\\Notepad")
elif 'open pycharm' in query:
os.startfile("C:\\Program Files\\JetBrains\\PyCharm Community Edition 2020.1.1\\bin\\pycharm64.exe")
elif 'open code blocks' in query:
os.startfile("C:\\Users\\91954\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\"
"Programs\\CodeBlocks\\CodeBlocks")
elif 'open mozilla firefox' in query:
os.startfile("C:\\Program Files\\Mozilla Firefox\\firefox.exe")
elif 'open chrome' in query:
os.startfile("C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe")
elif 'open whatsapp' in query:
os.startfile("C:\\Users\\91954\\AppData\\Local\\WhatsApp\\WhatsApp.exe")
elif 'open vlc' in query:
os.startfile("C:\\Program Files\\VideoLAN\\VLC\\vlc.exe")
elif 'who are you' in query:
fun_talk("I am P.A. (Python Assistant), developed by Ahmed Hossam "
"Rachit Dwivedi and Umesh Singh as a project in their college.")
elif 'what you want to do' in query:
fun_talk("I want to help people to do certain tasks on their single voice commands.")
elif 'alexa' in query:
fun_talk("I don't know Alexa, but I've heard of Alexa. If you have Alexa, "
"I may have just triggered Alexa. If so, sorry Alexa.")
elif 'google assistant' in query:
fun_talk("He was my classmate, too intelligent guy. We both are best friends.")
elif 'siri' in query:
fun_talk("Siri, She's a competing virtual assistant on a competitor's phone. "
"Not that I'm competitive or anything.")
elif 'cortana' in query:
fun_talk("I thought you'd never ask. So I've never thought about it.")
elif 'python assistant' in query:
fun_talk("Are you joking. You're coming in loud and clear.")
elif 'what language you use' in query:
fun_talk("I am written in Python and I generally speak english.")
elif 'send email' in query:
def send_mail(receiver, subject, message):
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login('[email protected]', 'your_password')
email = EmailMessage()
email['From'] = '[email protected]'
email['To'] = receiver
email['Subject'] = subject
email.set_content(message)
server.send_message(email)
email_list = {
'Umesh': '[email protected]',
'Rishabh': '[email protected]',
'name': '[email protected]',
'assitant': '[email protected]'
}
def get_mail_info():
fun_talk('To whom you want to send email')
name = get_command()
receiver = email_list[name]
print(receiver)
fun_talk('What is the subject of your email?')
subject = get_command()
fun_talk('Tell me the text in your email')
message = get_command()
send_mail(receiver, subject, message)
fun_talk('Hey lazy person. Your email is sent Successfully.')
fun_talk('Do you want to send more email?')
send_more = get_command()
if 'yes' in send_more:
get_mail_info()
get_mail_info()
elif 'play' in query:
cmd_info = query.replace('play', '')
fun_talk(f'Playing {cmd_info} ')
print(cmd_info)
pywhatkit.playonyt(cmd_info)
elif 'search' in query:
query = query.replace('search', '')
pywhatkit.search(query)
elif 'set alarm' in query:
fun_talk("Tell me the time to set an Alarm. For example, set an alarm for 11:21 AM")
a_info = get_command()
a_info = a_info.replace('set an alarm for', '')
a_info = a_info.replace('.', '')
a_info = a_info.upper()
MyAlarm.alarm(a_info)
elif 'exit p a' in query:
fun_talk("Exiting Sir...")
sys.exit()
elif 'close command prompt' in query:
os.system("TASKKILL /F /IM cmd.exe")
elif 'close firefox' in query:
os.system("TASKKILL /F /IM firefox.exe")
# subprocess.call(["taskkill", "/F", "/IM", "firefox.exe"])
elif 'close visual studio code' in query:
os.system("TASKKILL /F /IM Code.exe")
elif 'close eclipse' in query:
os.system("TASKKILL /F /IM eclipse.exe")
elif 'close notepad' in query:
os.system("TASKKILL /F /IM notepad.exe")
elif 'close pycharm' in query:
os.system("TASKKILL /F /IM pycharm64.exe")
elif 'close code blocks' in query:
os.system("TASKKILL /F /IM codeblocks.exe")
elif 'close chrome' in query:
os.system("TASKKILL /F /IM chrome.exe")
elif 'close whatsapp' in query:
os.system("TASKKILL /F /IM WhatsApp.exe")
elif 'close vlc' in query:
os.system("TASKKILL /F /IM vlc.exe")
elif 'close spotify' in query:
os.system("TASKKILL /F /IM Spotify.exe")
elif 'poem' in query:
fun_talk('Poem of which author you want to listen?')
auth = get_command()
poem = poetpy.get_poetry('author', auth, 'title,linecount')
poems = poetpy.get_poetry('author', auth, 'lines')
poem_len = len(poem)
# print(poem_len)
poem_no = random.randint(1, poem_len)
print("Title- ", poem[poem_no]['title'])
fun_talk(f"Title- {poem[poem_no]['title']}")
print("No. of lines-", poem[poem_no]['linecount'])
fun_talk(f"No. of lines- {poem[poem_no]['linecount']}")
poem_str = '\n'
print("Poem-\n", poem_str.join(poems[poem_no]['lines']))
fun_talk(f"Poem-\n {poem_str.join(poems[poem_no]['lines'])}")
elif 'resume' in query or 'pause' in query:
pyautogui.press("playpause")
elif 'previous' in query:
pyautogui.press("prevtrack")
elif 'next' in query:
pyautogui.press("nexttrack")
elif 'convert currency' in query:
try:
curr_list = {
'dollar': 'USD', 'taka': 'BDT', 'dinar': 'BHD',
'rupee': 'INR', 'afghani': 'AFN', 'real': 'BRL',
'yen': 'JPY', 'peso': 'ARS', 'pound': 'EGP', 'rial': 'OMR',
'lek': 'ALL', 'kwanza': 'AOA', 'manat': 'AZN', 'franc': 'CHF'
}
cur = CurrencyRates()
# print(cur.get_rate('USD', 'INR'))
fun_talk('From which currency u want to convert?')
from_cur = get_command()
src_cur = curr_list[from_cur.lower()]
fun_talk('To which currency u want to convert?')
to_cur = get_command()
dest_cur = curr_list[to_cur.lower()]
fun_talk('Tell me the value of currency u want to convert.')
val_cur = float(get_command())
# print(val_cur)
print(cur.convert(src_cur, dest_cur, val_cur))
except Exception as e:
print("Couldn't get what you have said, Can you say it again??")
elif 'covid-19' in query or 'corona' in query:
fun_talk('For which region u want to see the Covid-19 cases. '
'Overall cases in the world or any specific country?')
c_query = get_command()
if 'overall' in c_query or 'over all' in c_query or 'world' in c_query or 'total' in c_query or 'worldwide' in c_query:
def world_cases():
try:
url = 'https://www.worldometers.info/coronavirus/'
info_html = requests.get(url)
info = bs4.BeautifulSoup(info_html.text, 'lxml')
info2 = info.find('div', class_='content-inner')
new_info = info2.findAll('div', id='maincounter-wrap')
# print(new_info)
print('Worldwide Covid-19 information--')
fun_talk('Worldwide Covid-19 information--')
for i in new_info:
head = i.find('h1', class_=None).get_text()
counting = i.find('span', class_=None).get_text()
print(head, "", counting)
fun_talk(f'{head}: {counting}')
except Exception as e:
pass
world_cases()
elif 'country' in c_query or 'specific country' in c_query:
def country_cases():
try:
fun_talk('Tell me the country name.')
c_name = get_command()
c_url = f'https://www.worldometers.info/coronavirus/country/{c_name}/'
data_html = requests.get(c_url)
c_data = bs4.BeautifulSoup(data_html.text, 'lxml')
new_data = c_data.find('div', class_='content-inner').findAll('div', id='maincounter-wrap')
# print(new_data)
print(f'Covid-19 information for {c_name}--')
fun_talk(f'Covid-19 information for {c_name}')
for j in new_data:
c_head = j.find('h1', class_=None).get_text()
c_counting = j.find('span', class_=None).get_text()
print(c_head, "", c_counting)
fun_talk(f'{c_head}: {c_counting}')
except Exception as e:
pass
country_cases()
elif 'weather' in query or 'temperature' in query:
try:
fun_talk("Tell me the city name.")
city = get_command()
api = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "&appid=eea37893e6d01d234eca31616e48c631"
w_data = requests.get(api).json()
weather = w_data['weather'][0]['main']
temp = int(w_data['main']['temp'] - 273.15)
temp_min = int(w_data['main']['temp_min'] - 273.15)
temp_max = int(w_data['main']['temp_max'] - 273.15)
pressure = w_data['main']['pressure']
humidity = w_data['main']['humidity']
visibility = w_data['visibility']
wind = w_data['wind']['speed']
sunrise = time.strftime("%H:%M:%S", time.gmtime(w_data['sys']['sunrise'] + 19800))
sunset = time.strftime("%H:%M:%S", time.gmtime(w_data['sys']['sunset'] + 19800))
all_data1 = f"Condition: {weather} \nTemperature: {str(temp)}°C\n"
all_data2 = f"Minimum Temperature: {str(temp_min)}°C \nMaximum Temperature: {str(temp_max)}°C \n" \
f"Pressure: {str(pressure)} millibar \nHumidity: {str(humidity)}% \n\n" \
f"Visibility: {str(visibility)} metres \nWind: {str(wind)} km/hr \nSunrise: {sunrise} " \
f"\nSunset: {sunset}"
fun_talk(f"Gathering the weather information of {city}...")
print(f"Gathering the weather information of {city}...")
print(all_data1)
fun_talk(all_data1)
print(all_data2)
fun_talk(all_data2)
except Exception as e:
pass
elif 'month' in query or 'month is going' in query:
def tell_month():
localtime = time.asctime(time.localtime(time.time()))
m_onth = localtime[4:7]
if m_onth == "Jan":
fun_talk("it's january")
if m_onth == "Feb":
fun_talk("it's february")
if m_onth == "Mar":
fun_talk("it's march")
if m_onth == "Apr":
fun_talk("it's april")
if m_onth == "May":
fun_talk("it's may")
if m_onth == "Jun":
fun_talk("it's june")
if m_onth == "Jul":
fun_talk("it's july")
if m_onth == "Aug":
fun_talk("it's august")
if m_onth == "Sep":
fun_talk("it's september")
if m_onth == "Oct":
fun_talk("it's october")
if m_onth == "Nov":
fun_talk("it's november")
if m_onth == "Dec":
fun_talk("it's december")
tell_month()
elif 'day' in query or 'day today' in query:
def tell_day():
localtime = time.asctime(time.localtime(time.time()))
day = localtime[0:3]
if day == "Sun":
fun_talk("it's sunday")
if day == "Mon":
fun_talk("it's monday")
if day == "Tue":
fun_talk("it's tuesday")
if day == "Wed":
fun_talk("it's wednesday")
if day == "Thu":
fun_talk("it's thursday")
if day == "Fri":
fun_talk("it's friday")
if day == "Sat":
fun_talk("it's saturday")
tell_day()
elif "calculate" in query:
try:
app_id = "JUGV8R-RXJ4RP7HAG"
client = wolframalpha.Client(app_id)
indx = query.lower().split().index('calculate')
query = query.split()[indx + 1:]
res = client.query(' '.join(query))
answer = next(res.results).text
print("The answer is " + answer)
fun_talk("The answer is " + answer)
except Exception as e:
print("Couldn't get what you have said, Can you say it again??")
elif 'quote' in query or 'quotes' in query:
fun_talk("Tell me the author or person name.")
q_author = get_command()
quotes = quote(q_author)
quote_no = random.randint(1, len(quotes))
# print(len(quotes))
# print(quotes)
print("Author: ", quotes[quote_no]['author'])
print("-->", quotes[quote_no]['quote'])
fun_talk(f"Author: {quotes[quote_no]['author']}")
fun_talk(f"He said {quotes[quote_no]['quote']}")
elif 'what' in query or 'who' in query: # or 'where' in query:
client = wolframalpha.Client("JUGV8R-RXJ4RP7HAG")
res = client.query(query)
try:
print(next(res.results).text)
fun_talk(next(res.results).text)
except StopIteration:
print("No results found!!")
elif 'empty recycle bin' in query or 'clear recycle bin' in query:
try:
winshell.recycle_bin().empty(confirm=False, show_progress=False, sound=True)
print("Recycle Bin is cleaned successfully.")
fun_talk("Recycle Bin is cleaned successfully.")
except Exception as e:
print("Recycle bin is already Empty.")
fun_talk("Recycle bin is already Empty.")
elif 'write a note' in query or 'make a note' in query:
fun_talk("What should I write, sir??")
note = get_command()
file = open('Notes.txt', 'a')
fun_talk("Should I include the date and time??")
n_conf = get_command()
if 'yes' in n_conf:
str_time = datetime.datetime.now().strftime("%H:%M:%S")
file.write(str_time)
file.write(" --> ")
file.write(note)
fun_talk("Point noted successfully.")
else:
file.write("\n")
file.write(note)
fun_talk("Point noted successfully.")
elif 'show me the notes' in query or 'read notes' in query:
fun_talk("Reading Notes")
file = open("Notes.txt", "r")
data_note = file.readlines()
# for points in data_note:
print(data_note)
fun_talk(data_note)
elif 'distance' in query:
geocoder = Nominatim(user_agent="Singh")
fun_talk("Tell me the first city name??")
location1 = get_command()
fun_talk("Tell me the second city name??")
location2 = get_command()
coordinates1 = geocoder.geocode(location1)
coordinates2 = geocoder.geocode(location2)
lat1, long1 = coordinates1.latitude, coordinates1.longitude
lat2, long2 = coordinates2.latitude, coordinates2.longitude
place1 = (lat1, long1)
place2 = (lat2, long2)
distance_places = distance.distance(place1, place2)
print(f"The distance between {location1} and {location2} is {distance_places}.")
fun_talk(f"The distance between {location1} and {location2} is {distance_places}")
elif 'screenshot' in query:
sc = pyautogui.screenshot()
sc.save('pa_ss.png')
print("Screenshot taken successfully.")
fun_talk("Screenshot taken successfully.")
elif 'volume up' in query:
pyautogui.press("volumeup")
elif 'volume down' in query:
pyautogui.press("volumedown")
elif 'mute volume' in query:
pyautogui.press("volumemute")
elif 'joke' in query:
joke = pyjokes.get_joke()
print(joke)
fun_talk(joke)
app = 0
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
app.exec_()