-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmain.py
657 lines (575 loc) · 22.4 KB
/
main.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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
import os
import subprocess
from ecapture import ecapture as ec
import datetime
import json
import os
import time
import weakref
import webbrowser
import requests
import base64
import pyttsx3
import playsound # pip install playsound
import requests
import speech_recognition as sr # pip install SpeechRecognition
from bs4 import BeautifulSoup
from gtts import gTTS # pip install gTTS
import pyaudio # pip install PyAudio
import speedtest # for speedtest application
import pygame
import pywhatkit # Whatsapp messaging
import email
import imaplib
import psutil, math
import wolframalpha
wolframalpha_id="WRAW5Y-K8KXUJQQKQ"
app_id=wolframalpha_id
import psutil, math
from twilio.rest import Client
import random
pygame.mixer.init()
pygame.init()
from newsapi import NewsApiClient # for latest news api
import credentials
from events_api import events
#if pyaudio installation is failed, try installing it with pywin
import randfacts as rf #pip install randfacts
from quote import quote
# function to accept audio input from user
# get_audio()
def get_audio():
r = sr.Recognizer()
engine = pyttsx3.init()
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
said = ""
try:
said = r.recognize_google(audio)
print(said)
except Exception as e:
print("Exception: " + str(e))
return said
# for playing music
def playmusic():
# Here you can provide the mp3 file name.
pygame.mixer.music.load("Tobu Itro Sunburst NCS Release.mp3")
pygame.mixer.music.play()
# To stop the music
def stopmusic():
pygame.mixer.music.stop()
# function to convert text_to_speech
def speak(text):
tts = gTTS(text=text, lang="en-in")
filename = "voice2.mp3"
tts.save(filename)
playsound.playsound(filename)
os.remove(filename)
# Voice_assistant skills#
# function for News
def news():
# for latest news
newsapi = NewsApiClient(api_key='{}'.format(credentials.newsapikey))
# /v2/top-headlines
top_headlines = newsapi.get_top_headlines(country='in')
ranks = ["First", 'Sceond', 'Third', 'Fourth', 'Fifth']
for i in range(5):
# first five news
print(f"{ranks[i]} News : {top_headlines['articles'][i]['title']}")
speak(f"{ranks[i]} News : {top_headlines['articles'][i]['title']}".replace(" - ", "news by"))
# function to return joke from api
def get_joke():
response = requests.get("https://icanhazdadjoke.com/", headers={"Accept": "application/json"})
json_data = json.loads(response.text)
joke = json_data['joke']
return joke
def check_command_is_for_covid_cases(command):
country = get_country(command).capitalize()
cases = get_covid_cases(country)
speak(f"The current active cases in {country} are {cases}")
def get_country(command):
country = command.split()[-1]
if country == "?":
country = command.split()[-2]
return country
def computational_intelligence(question):
try:
client = wolframalpha.Client(app_id)
answer = client.query(question)
answer = next(answer.results).text
print(answer)
return answer
except:
speak("Sorry sir I couldn't fetch your question's answer. Please try again ")
def get_covid_cases(country):
totalActiveCases = 0
response = requests.get('https://api.covid19api.com/live/country/' + country + '/status/confirmed').json()
for data in response:
totalActiveCases += data.get('Active')
return totalActiveCases
def get_trending_topics(trending_topics):
consumer_key = os.getenv('consumer_key') #set consumer key using twitter developer account api in environment variables
consumer_secret_key = os.environ.get('consumer_secret_key') #set consumer secret key using twitter developer account api in environment variables
# Reformat the keys and encode them
key_secret = '{}:{}'.format(consumer_key, consumer_secret_key).encode('ascii')
# Transform from bytes to bytes that can be printed
b64_encoded_key = base64.b64encode(key_secret)
# Transform from bytes back into Unicode
b64_encoded_key = b64_encoded_key.decode('ascii')
base_url = 'https://api.twitter.com/'
auth_url = '{}oauth2/token'.format(base_url)
auth_headers = {
'Authorization': 'Basic {}'.format(b64_encoded_key),
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
}
auth_data = {
'grant_type': 'client_credentials'
}
auth_resp = requests.post(auth_url, headers=auth_headers, data=auth_data)
access_token = auth_resp.json()['access_token']
trend_headers = {
'Authorization': 'Bearer {}'.format(access_token)
}
trend_params = {
'id': 2295411, # set to 1 if you want global trending topics, currently set to WOEID of mumbai
}
trend_url = 'https://api.twitter.com/1.1/trends/place.json'
trend_resp = requests.get(trend_url, headers=trend_headers, params=trend_params)
tweet_data = trend_resp.json()
for i in range(0, 5):
tt = tweet_data[0]['trends'][i]['name']
trending_topics.append(tt[1:])
# function to get meaning of a word
def get_meaning():
speak("Please say the word you want to know meaning of")
word = get_audio().lower()
url = "https://api.dictionaryapi.dev/api/v2/entries/en/" + word
response = requests.get(url)
json_data = json.loads(response.text)
meaning = json_data[0]["meanings"][0]["definitions"][0]["definition"]
return meaning
def get_facts():
speak(rf.get_fact())
def get_quote():
qt = quote(limit=1)
for i in range(len(qt)):
qt_say = qt[i]['quote'] + ' given by ' + qt[i]['author']
speak(qt_say)
def movie():
page= requests.get('https://www.imdb.com/chart/top/') #webscrapped imdb's top charts
soup= BeautifulSoup(page.content, 'html.parser')
links= soup.select("table tbody tr td.titleColumn a")
first10= links[:10]
for anchor in first10:
print(anchor.text)
speak(anchor.text)
# for importing 5 emails
def mails():
unreads = []
username = credentials.email
password = credentials.password
# creata a imap object
imap = imaplib.IMAP4_SSL("imap.gmail.com")
# login
result = imap.login(username, password)
# Use "[Gmail]/Sent Mails" for fetching
# mails from Sent Mails.
imap.select('"[Gmail]/All Mail"',readonly=True)
response, messages = imap.search(None,'UnSeen')
messages = messages[0].split()
latest = int(messages[-1])
# take it from start
oldest = int(messages[0])
for i in range(latest, latest - 5, -1):
res, msg = imap.fetch(str(i), "(RFC822)")
for response in msg:
mail = {}
if isinstance(response, tuple):
msg = email.message_from_bytes(response[1])
# print required information
# print(msg)
mail['date'] = msg["Date"]
mail['sender'] = msg["From"].split('<')[0]
mail['subject'] = msg["Subject"]
for part in msg.walk():
if part.get_content_type() == "text/plain":
body = part.get_payload(decode=True)
mail['body'] = body.decode("UTF-8")
unreads.append(mail)
return unreads
# for reading fetched emails
def read_mail():
# subject of unread emails
result = mails()
print(result)
for i in range(5):
try:
print(f"you have a mail from {result[i]['sender']}.Subject is {result[i]['subject']}.on {''.join(result[i]['date'].split()[1:4])}")
speak(f"you have a mail from {result[i]['sender']}.Subject is {result[i]['subject']}.on {''.join(result[i]['date'].split()[1:4])}")
except:
pass
def convert_size(size_bytes):
if size_bytes == 0:
return "0B"
size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
i = int(math.floor(math.log(size_bytes, 1024)))
p = math.pow(1024, i)
s = round(size_bytes / p, 2)
print("%s %s" % (s, size_name[i]))
return "%s %s" % (s, size_name[i])
def system_stats():
cpu_stats = str(psutil.cpu_percent())
battery_percent = psutil.sensors_battery().percent
memory_in_use = convert_size(psutil.virtual_memory().used)
total_memory = convert_size(psutil.virtual_memory().total)
final_res = f"Currently {cpu_stats} percent of CPU, {memory_in_use} of RAM out of total {total_memory} is being used and battery level is at {battery_percent} percent"
return final_res
def wish():
hour = int(datetime.datetime.now().hour)
if hour>=22 and hour<5:
print('\n\tGood Night Sir!')
speak('Good Night Sir')
elif hour>=5 and hour<12:
print('\n\tGood Morning Sir!')
speak(f'Good Morning Buddy')
elif hour>=12 and hour<18:
print('\n\tGood Afternoon Sir')
speak(f'Good Afternoon Sir')
else:
print('\n\tGood Evening Sir')
speak('Good Evening Sir')
speak('I am Friday, Your Personal AI Assitant sir')
# weather report
def weather():
# Current Weather report
api_request = requests.get("https://api.openweathermap.org/data/2.5/weather?q=Pune&appid={}".format(credentials.weatherapikey))
api = json.loads(api_request.content)
y = api['main']
current_temprature = round(y['temp'] - 273, 2)
humidity = y['humidity']
tempmin = round(y['temp_min'] - 273, 2)
tempmax = round(y['temp_max'] - 273, 2)
report = f"""current temprature is {current_temprature} degree celcius ,
Humidity is {humidity} ,
Minimum temprature is {tempmin} degree celcius
and maximum temprature is {tempmax} degree celcius """
speak(report)
print(report)
time.sleep(2)
speak("Hi what can i do for you?")
last_query = None
while True:
query = get_audio().lower()
if 'the time' in query:
strTime = datetime.datetime.now().strftime("%H:%M:%S")
speak(f"Sir, the time is {strTime}")
elif 'active cases of covid-19' in query:
check_command_is_for_covid_cases(query)
elif 'joke' in query:
speak(get_joke())
# Tells The Time.
elif ' time' in query:
strTime = datetime.datetime.now().strftime("%I:%M %p")
print(f"\n\tIt is {strTime}")
speak(f"It's {strTime}")
# Tells The Date.
elif 'the date' in query or "today's date" in query:
strDate = datetime.datetime.now().strftime("%m/%d/%y")
print(f"\n\tToday is {strDate}")
speak(f"Today is, {strDate}")
# Greets the User.
elif 'greet me' in query or 'wish me' in query:
wish()
if last_query:
if last_query == 'open notepad':
with open('notepad.txt', 'w+') as file:
file.write(query)
elif 'open notepad' in query:
last_query = 'open notepad'
elif 'weather' in query:
speak("Please tell your city name?")
city = get_audio().lower()
# creating url and requests instance
url = "https://www.google.com/search?q=" + "weather" + city
html = requests.get(url).content
soup = BeautifulSoup(html, 'html.parser')
temp = soup.find('div', attrs={'class': 'BNeawe iBp4i AP7Wnd'}).text
str = soup.find('div', attrs={'class': 'BNeawe tAd8D AP7Wnd'}).text
data = str.split('\n')
sky = data[1]
speak(f"Temperature for {city} today is {temp} °C")
speak(f"And the sky will be {sky}")
elif 'trending' in query:
trending_topics = []
get_trending_topics(trending_topics)
for i in trending_topics:
speak(i)
elif 'meaning' in query:
speak(get_meaning())
elif 'news' in query:
news()
elif "what is" in query or "who is" in query or "where is" in query:
question = query
answer = computational_intelligence(question)
speak(answer)
elif 'business quote' in query or 'say about business' in query:
response=requests.get("https://efflux.herokuapp.com/business")
json_data=json.loads(response.text)
quote =json_data['q'] + " by "+json_data["a"]
print(quote)
speak(quote)
elif 'love quote' in query or 'say about love' in query:
response=requests.get("https://efflux.herokuapp.com/love")
json_data=json.loads(response.text)
quote =json_data['q'] + " by "+json_data["a"]
print(quote)
speak(quote)
elif 'hustle quote' in query :
response=requests.get("https://efflux.herokuapp.com/hustle")
json_data=json.loads(response.text)
quote =json_data['q'] + " by "+json_data["a"]
print(quote)
speak(quote)
elif 'life quote' in query or 'say about life' in query:
response=requests.get("https://efflux.herokuapp.com/life")
json_data=json.loads(response.text)
quote =json_data['q'] + " by "+json_data["a"]
print(quote)
speak(quote)
elif 'friendship quote' in query or 'say about friendship' in query:
response=requests.get("https://efflux.herokuapp.com/friendship")
json_data=json.loads(response.text)
quote =json_data['q'] + " by "+json_data["a"]
print(quote)
speak(quote)
elif 'war quote' in query or 'say about war' in query:
response=requests.get("https://efflux.herokuapp.com/war")
json_data=json.loads(response.text)
quote =json_data['q'] + " by "+json_data["a"]
print(quote)
speak(quote)
elif 'game quote' in query or 'gaming quote' in query :
response=requests.get("https://videogame-quotes-api.herokuapp.com/quotes")
json_data=json.loads(response.text)
quote =json_data['quote'] + " from "+json_data["game"]
print(quote)
speak(quote)
elif "internet" in query and "speed" in query:
speak("Wait for while...")
st = speedtest.Speedtest()
up = round(st.upload() / 10 ** 6, 2)
down = round(st.download() / 10 ** 6, 2)
print(f"Download Speed is {down} MB/s")
speak(f"Download Speed is {down} MB per Sceond")
print(f"Upload Speed is {up} MB/s")
speak(f"Upload Speed is {up} Mb per Sceond")
elif "suggest me movies" in query:
speak("I scoured the internet and found the top 10 films according to imdb")
print(f"I scoured the internet and found the top 10 films according to imdb")
movie()
time.sleep(0.2)
speak("now redirecting you to imdb site you can check more reviews there")
webbrowser.open("https://www.imdb.com/chart/top/")
elif "play music" in query:
playmusic()
elif "stop music" in query:
stopmusic()
elif 'fact' in query or 'facts' in query:
get_facts()
elif 'quote' in query:
get_quote()
# whatsapp messaging
elif "message" in command:
hr, min = datetime.datetime.now().hour, datetime.datetime.now().minute
phone = credentials.contacts
speak("whom did you want to send message")
user = take_command()
speak("What is message")
msg = take_command()
id = False
for i in phone.keys():
if i in user:
user = i
id = True
break
if id:
speak(f"sending {msg} to {user}")
pywhatkit.sendwhatmsg(f"{phone[user]}", f"{msg}", hr, min + 2)
else:
speak(f"We don't have {user} phone no.")
# Exit the program
elif "no thanks" in query or "exit" in query or "close" in query :
speak("Thanks For using Me,Have a nice day")
exit(0)
elif "sleep" in query or "wait" in query:
time.sleep(3)
# for unread emails
elif 'unread' or 'new' in query and 'mail' in query:
read_mail()
# shopping list
elif "shopping" or "list" in query:
if 'add' in query:
speak("What do you want to add")
item = get_audio().lower()
file = open("shopping_list.txt",'a')
file.write(item)
file.close()
elif 'remove' in query:
speak("what do you want to remove")
item = get_audio().lower()
try:
with open("shopping_list.txt", "r") as fp:
lines = fp.readlines()
with open("shopping_list.txt", "w") as fp:
for line in lines:
if line.strip("\n") != f"{item}":
fp.write(line)
except:
speak("Your shopping list empty")
elif 'open' or 'read' in query:
speak('items in list are ')
try:
file = open("shopping_list.txt", 'r')
except:
speak("Your shopping list empty")
for item in file:
speak(item)
file.close()
elif 'search' in query or 'find' in query :
query = query.replace("search", "")
query = query.replace("find", "")
if "youtube" in query or "yt" in query:
query = query.replace("youtube", "")
query = query.replace("yt", "")
webbrowser.open(f'https://www.youtube.com/results?search_query={query}')
elif "google" in query:
query = query.replace("google", "")
webbrowser.open(f'https://www.google.com/search?q={query}')
else:
webbrowser.open(f'{query}')
elif "system" in query:
sys_info = system_stats()
print(sys_info)
speak(sys_info)
#time.sleep(5)
#random talks
elif 'ok' in query or 'okay' in query:
print("\n\tThat's It")
speak("That's It.")
# It's Creator.
elif 'who made you' in query or 'who created you' in query:
print('\n\tI Was Made by Team Pccoe and many other Contributors ')
speak("I Was Made by Team Pccoe and many other Contributors")
elif 'thanks' in query or 'thank you' in query:
thanksGiving = [
'Nevermind!',
'You are Always Welcome!',
'Mention Not!',
"That's My Duty!"
]
thanksGiving = random.choice(thanksGiving)
print(f'\n\t{thanksGiving}')
speak(thanksGiving)
# Replies Welcome.
elif 'welcome' in query:
print('\n\tI Feel Honored!')
speak('I Feel Honored!')
# Replies Sorry.
elif 'sorry' in query or 'my fault' in query or 'my mistake' in query:
print('\n\tHey! Never Repeat This.')
speak('Hey! Please Never Repeat This.')
# Says It's Condition.
elif 'how are you' in query or 'how do you do' in query:
as_i_am = [
'I am Fine,',
'I am Doing Well,',
'I am Great,'
]
as_i_am = random.choice(as_i_am)
print(f'\n\t{as_i_am} Thanks For Asking!')
speak(as_i_am + ' Thanks For Asking!')
elif "bot" == query or 'boat' in query or 'friday' in query :
toReply = [
'Ready to Help You!',
'How Can I Help You?',
'I am Here'
]
toReply = random.choice(toReply)
print(f"\n\t{toReply}")
speak(toReply)
# Reacts If User Says Hey.
elif "hey" in query or "hi" in query:
hey_ans = [
'Ready to Help You!',
'How Can I Help You?',
'I am Here to Help You!'
]
hey_ans = random.choice(hey_ans)
print(f'\n\t{hey_ans}')
speak(hey_ans)
elif f'hello' in query or f'hi' in query:
hello_ans = [
'Hi Sir!',
'Hey Sir!',
'Hello Sir!',
'Heyy Whatsupp!',
'Hey There !',
'Hello There !'
]
hello_ans = random.choice(hello_ans)
print(f'\n\t{hello_ans}! How Can I Help You?')
speak(f'{hello_ans}! How Can I Help You?')
elif '©empty_^_^_self.queryª' in query:
print(f" Did Not Get It...\n\n")
speak('Did Not Get it!')
elif "restart" in query:
subprocess.call(["shutdown", "/r"])
elif "hibernate" in query or "sleep" in query:
speak("Hibernating")
subprocess.call("shutdown / h")
elif "log off" in query or "sign out" in query:
speak("Make sure all the application are closed before sign-out")
time.sleep(5)
subprocess.call(["shutdown", "/l"])
elif 'open' in query or 'start' in query:
for app in credentials.application.keys():
if app in query:
speak(f"Wait we are starting {app}")
print(f"start {credentials.application[app]}")
os.system(f"start {credentials.application[app]}")
elif 'close' in query or 'end' in query:
for app in credentials.application.keys():
if app in query:
speak(f"Wait we are closing {app}")
print(f"start {credentials.application[app]}")
os.system(f"taskkill /f /im {credentials.application[app]}")
# REMEMBER WE SAID TO REMEMBER
elif 'remember that' in query:
speak("What should i remember")
data = get_audio()
speak("Okay. I will remember that ")
remember = open('data.txt', 'w')
remember.write(data)
remember.close()
# TO REMEMBER THE PREVIOUS TEXT
elif 'do you remember' in query:
remember = open('data.txt', 'r')
speak("you said me to remember that"+remember.read())
elif "camera" in query or "take a photo" in query:
ec.capture(0, "friday Camera ", "img.jpg")
# weather report
elif "weather" in query:
weather()
elif "what can you do" in command:
speak("I can make you laugh with jokes, motivate you through quotes, kill your boredom with interesting facts")
speak(" I can say the latest news, weather report, internet speed, system info, covid stats")
speak("I can open and close applications")
speak(" I can play youtube videos, songs and search for things on the internet, suggest a movie and do a lot")
elif "event" in query or "schedule" in query:
speak("Upcoming first 10 events")
for i in events():
speak(f"you have {i[1]} on {i[0]}")
else:
speak("there is problem with command ,please say again..")
continue