-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFridayDavid.py
310 lines (244 loc) · 12.1 KB
/
FridayDavid.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
import pyttsx3
import datetime
import speech_recognition as sr
import wikipedia
import webbrowser
import os
import random
import requests
import time
from youtube_search import YoutubeSearch
import playsound
wakeWords = ["hey friday", "ok friday", "you there friday", "wake up friday", "hai friday", "friday are you up", "you up friday"]
shutdownWords = ["shutdown", "power off", "back to sleep", "go back to sleep", "go to sleep", "exit", "quit"]
wikiWords = ["wikipedia", "according to wikipedia", "search wikipedia for", "who is", "tell me about"]
searchGoogle = ["search google"," find in google"]
findLocation = ["find location", "find this location", "locate a place", "find a location", "locate this place", "find this place"]
months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
weatherCheck = [ "get weather", "what is the weather", "check weather", "weather information", "wetaher details", "how is the weather"]
yesWords = ["yeah","ok","yes","go on","fine", "yup", "go for it"]
noWords = ["no","no need","naco", "nope", "nop", "not now"]
musicWords = ["sing a song", "play music", "music friday", "i am feeling groovy"]
edge_path="C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe"
webbrowser.register('edge', None,webbrowser.BackgroundBrowser(edge_path))
engine = pyttsx3.init("sapi5")
voices = engine.getProperty("voices")
#print(voices)
# for voice in voices:
# # to get the info. about various voices in our PC
# print("Voice:")
# print("ID: %s" %voice.id)
# print("Name: %s" %voice.name)
# print("Age: %s" %voice.age)
# print("Gender: %s" %voice.gender)
# print("Languages Known: %s" %voice.languages)
engine.setProperty("voice", voices[0].id)
# volume = engine.getProperty("volume")
# engine.setProperty("volume", 0.5)
#engine.setProperty("rate",180)
def Speak(audio):
'''
Parameters
----------
audio : TYPE
DESCRIPTION.
Enables Jarvis to speak
Returns
-------
None.
'''
print("Friday: " + audio)
engine.say(audio)
engine.runAndWait()
def WishMe():
'''
Enables Jarvis to wish according to hour of the day
Returns
-------
None.
'''
Speak("Initializing all systems")
playsound.playsound("D:\Coding\Python\Projects\Virtual_Assistant_Version_1.0\HUD Activation Sound Effect.mp3")
playsound.playsound("D:\Coding\Python\Projects\Virtual_Assistant_Version_1.0\FridayBoot.mp3")
Speak("All systems loaded")
Speak("I have indeed been uploaded sir. We're on-line and ready.")
currentDay = datetime.datetime.now().strftime("%a, %b %d, %Y")
Speak("Today is " + str(currentDay))
strTime=datetime.datetime.now().strftime("%I:%M %p")
Speak(f"It is {strTime}")
hour=int(datetime.datetime.now().hour)
if hour>=4 and hour<12:
Speak("Good Morning boss!")
elif hour>=12 and hour<18:
Speak("Good Afternoon boss!")
elif hour>=18 and hour<22:
Speak("Good evening boss")
else:
Speak("Good evening boss, or, should i say good night!")
#Speak("Friday. Version 5.0 . At your service sir")
def TakeCommand():
'''
Enables Jarvis to take commands from the user(specifically
takes microphone input and returns string output)
Returns
-------
String.
'''
r=sr.Recognizer()
r.energy_threshold=275
with sr.Microphone() as source:
print("Listening Boss...")
r.pause_threshold=1
audio=r.listen(source)
try:
query = r.recognize_google(audio,language="en-US")
print(f"Boss: {query}\n")
except Exception:
print("Sorry I didn't catch that boss")
return ""
return query
def GetWeather(city = "kochi"):
try:
wquery = "q=" + city
res=requests.get('http://api.openweathermap.org/data/2.5/weather?'+wquery+'&APPID=6efd2ff32715ec6f8058f256c6535fec&units=metric')
result = res.json()
tempStr = "The temperature in " + city + " as of now is " + str(result["main"]["temp"]) + " degree celsius"
tempStr2 = "But it may vary between " + str(result["main"]["temp_min"]) + " degree celsius" + " and " + str(result["main"]["temp_max"]) + " degree celsius"
weatherStr = "Weather : " + str(result["weather"][0]["description"])
windSpeedStr = "The wind speed is " + str(result["wind"]["speed"]) + " metre per second"
humidityStr = "The humidity is " + str(result["main"]["humidity"])
Speak(tempStr)
if not result["main"]["temp_min"] == result["main"]["temp_max"]:
Speak(tempStr2)
Speak(weatherStr)
Speak(windSpeedStr)
Speak(humidityStr)
except:
Speak("Sorry boss but this location seems to be off the map")
if __name__ == "__main__":
WishMe()
while True:
query = TakeCommand().lower()
#Logic for executing tasks based on query(command)
for phrase in wikiWords:
if phrase in query:
Speak("Hang on for a second sir...")
try:
query=query.replace(phrase, "")
results=wikipedia.summary(query,sentences=2,auto_suggest=True,redirect=True)
Speak(results)
except wikipedia.exceptions.PageError:
Speak("Sorry boss, got nothing on that in my database")
Speak("Would you like me to search the web boss?")
answer = TakeCommand().lower()
for phrase in yesWords:
if phrase in answer:
url = "https://google.com/search?q=" + query
webbrowser.get("edge").open_new_tab(url)
Speak("This is what i found boss")
for phrase in noWords:
if phrase in answer:
Speak("Alright boss")
if "the time" in query:
strTime=datetime.datetime.now().strftime("%I:%M:%S %p")
Speak(f"It is {strTime} boss")
if "open youtube" in query:
Speak("Just a second sir")
webbrowser.get('edge').open_new_tab("youtube.com")
Speak("And yup. There you go sir")
if "open google" in query:
Speak("Well don't we all just love google? Just a second sir")
webbrowser.get('edge').open_new_tab("google.com")
Speak("Search away boss")
if "python module" in query:
Speak("These python modules remind me of my source code days")
webbrowser.get("edge").open_new_tab("pypi.org")
Speak("As always sir, a great pleasure watching you work.")
for phrase in musicWords:
if phrase in query:
Speak("Do you have anything specific in mind or do you want me to surprise you?")
wish = TakeCommand().lower()
if "surprise me" in wish:
url = "https://ytroulette.com/?c=1&l=en"
webbrowser.get("edge").open_new_tab(url)
Speak("Alright boss")
break
else:
Speak("Sorry boss but i don't think i can really sing this one")
Speak("Would you like me to search the web boss?")
answer = TakeCommand().lower()
for phrase in yesWords:
if phrase in answer:
results = YoutubeSearch(wish , max_results=3).to_dict()
url = "http://www.youtube.com" + results[0]["url_suffix"]
webbrowser.get("edge").open_new_tab(url)
Speak("This is what i got boss")
break
Speak("Alright boss")
if "command prompt" in query:
cPromptPath='C:\\Windows\\System32\\cmd.exe'
Speak("Accessing Commandline Prompt")
os.startfile(cPromptPath)
Speak("This never gets old boss.")
if "who are you" in query:
Speak("I am Friday, a virtual assistant created by master Sagar Paul.")
Speak(("My source code is written in python."))
Speak("Since i am in my initial stages of development, my capabilities as of now, are quite limited.")
Speak("But i will be glad to serve you in every way i can, within the limits of my capabilities boss.")
for phrase in wakeWords:
if phrase in query:
Speak("Right here boss")
for phrase in shutdownWords:
if phrase in query:
Speak("It's been a pleasure serving you boss.")
Speak("Initializing shutdown sequence.")
playsound.playsound("D:\\Coding\\Python\\Projects\\Virtual_Assistant_Version_1.0\\system shut down sound FX.mp3")
Speak("All systems offline sir")
exit()
for phrase in searchGoogle:
if phrase in query:
Speak("What do you wanna search boss?")
search = TakeCommand().lower()
url = "https://google.com/search?q=" + search
webbrowser.get("edge").open_new_tab(url)
Speak("This is what i found boss")
for phrase in findLocation:
if phrase in query:
Speak("What do you want me to locate boss?")
search = TakeCommand().lower()
url = "https://google.nl/maps/place/" +search + "/&"
webbrowser.get("edge").open_new_tab(url)
Speak("This is what i got sir")
if "what is the month" in query:
currentTime = datetime.datetime.now()
Speak("It is "+ months[currentTime.month - 1] + " boss")
if "what is this month" in query:
currentTime = datetime.datetime.now()
Speak("It is "+ months[currentTime.month - 1] + " boss")
if "what is the year" in query:
currentTime = datetime.datetime.now()
Speak("It is " + str(currentTime.year) + " boss")
if "what is this year" in query:
currentTime = datetime.datetime.now()
Speak("It is " + str(currentTime.year) + " boss")
if "what is the day" in query:
currentDay = datetime.datetime.now().strftime("%a, %b %d, %Y")
Speak("Today is " + str(currentDay) + " boss")
if "what is this day" in query:
currentDay = datetime.datetime.now().strftime("%a, %b %d, %Y")
Speak("Today is " + str(currentDay) + " boss")
if "how is the weather" in query:
GetWeather()
for phrase in weatherCheck:
if phrase in query:
Speak("What is the location boss?")
search = TakeCommand().lower()
Speak("This is what i got boss")
GetWeather(search)
if "search youtube" in query:
Speak("What would you like to search for boss?")
uwish = TakeCommand().lower()
url = "https://www.youtube.com/results?search_query=" + uwish
webbrowser.get("edge").open_new_tab(url)
Speak("Hang on a second boss")
Speak("And there you go")