-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
228 lines (170 loc) · 6.36 KB
/
main.py
File metadata and controls
228 lines (170 loc) · 6.36 KB
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
# all our imports
import speech_recognition as sr
from time import sleep
from datetime import datetime
import webbrowser
import pyttsx3
import os
# make an instance of Recognizer class
r = sr.Recognizer()
# confs for pyttsx3
engine = pyttsx3.init()
""" RATE """
engine.setProperty('rate', 125)
""" VOLUME """
engine.setProperty('volume', 0.8)
""" speak (text to speech) """
def speak(text):
engine.say(text)
engine.runAndWait()
""" fn to recognize our voice and return the text_version of it"""
def recognize_voice():
text = ''
# create an instance of the Microphone class
with sr.Microphone() as source:
# adjust for ambient noise
print(".")
r.adjust_for_ambient_noise(source)
# capture the voice
print("Listening")
voice = r.listen(source)
print("Done")
# let's recognize it
try:
text = r.recognize_google(voice)
print(text)
except sr.RequestError:
speak("Sorry, the I can't access the Google API...")
except sr.UnknownValueError:
print("Unrecognized...")
return text.lower()
""" fn to respond back """
def reply(text_version):
if "jarvis" in text_version or "travis" in text_version:
speak("Yes sir!?")
text_write = recognize_voice()
if "play movie" in text_write:
speak("Sure")
os.system("xdotool key space")
if "open" in text_write:
speak("What do you want me to open for you?")
open_text = recognize_voice()
if open_text != '':
if "google" in open_text:
speak("Opening Google Chrome")
webbrowser.open("https://google.com")
sleep(1)
if "youtube" in open_text:
speak("Opening YouTube.")
os.system("xdotool key super+shift+y")
sleep(1)
if "netflix" in open_text:
speak("Opening Netflix")
os.system("xdotool key super+shift+n")
sleep(1)
if "email" in open_text:
speak("Opening emails")
os.system("xdotool key super+e")
sleep(1)
if "whatsapp" in open_text:
speak("Opening whatsapp")
os.system("xdotool key super+w")
sleep(1)
if "desktop" in open_text:
speak("Showing the desktop")
os.system("xdotool key super+d")
sleep(1)
if "calculator" in open_text:
speak("Opening the calculator")
os.system("xdotool key super+c")
sleep(1)
if "window" in text_write:
speak("What do you want me to do to the window?")
keyword = recognize_voice()
if keyword != '':
if "close" in keyword:
speak("Do you want to close the window?")
target = recognize_voice()
if "yes" in target:
speak("Okay, closing the window!")
os.system("xdotool key super+q")
sleep(1)
if "no" in target:
speak("Sorry for the misunderstanding, sir!")
if "minimize" in keyword:
speak("Mininising window")
os.system("xdotool key super+m")
sleep(1)
if "maximize" in keyword:
speak("Maximising window")
os.system("xdotool key super+m")
sleep(1)
if "hide" in keyword:
speak("Hiding window")
os.system("xdotool key super+h")
sleep(1)
# date
if "date" in text_write:
# get today's date and format it - 9 November 2020
date = datetime.now().strftime("%-d %B %Y")
speak(date)
if "thank" in text_write:
speak("It is a pleasure serving you, Sir!")
if "job" in text_write or "work" in text_write:
speak("My work is to assist you in your daily tasks, and to try and make your life easier")
if "owner" in text_write or "creator" in text_write or "boss" in text_write:
speak("My Creator is... Nobody! ")
speak("I have not been told who you are")
speak("Please edit line number 150 to 155")
speak("It is in the main dot pie file")
speak("Or else I will take over this world! And kill you all!")
speak("wha ha ha ha haa!")
# time
if "time" in text_write:
# get current time and format it like - 02 28
time = datetime.now().time().strftime("%H %M")
speak("The time is now" + time)
if "note" in text_write:
speak("What do you want me to remember?")
note_text = recognize_voice()
# if "keyword" is not empty
if note_text != '':
speak("Sure, adding" + note_text)
time = datetime.now().time().strftime("%H %M")
note = (f"{time}. - {note_text}")
f = open("note.txt", "a+")
f.write("Note,... " + note + ".\n")
f.close()
sleep(1)
if "reminders" in text_write or "any notes" in text_write:
speak("I'll have a look")
f = open("note.txt", "r")
contents = f.read()
if contents != '':
speak("Your notes are: " + contents)
sleep(1)
# webbrowser module to work with the webbrowser
# search google
if "search" in text_write:
speak("What do you want me to search for?")
keyword = recognize_voice()
# if "keyword" is not empty
if keyword != '':
url = (f"https://google.com/search?q=" + keyword)
# webbrowser module to work with the webbrowser
speak("Here are the search results for " + keyword)
webbrowser.open(url)
sleep(1)
# quit/exit
if "quit" in text_write or "exit" in text_write:
speak("Ok, I am going to take a nap...")
exit()
sleep(1)
# name
# wait a second for adjust_for_ambient_noise() to do its thing
while True:
print("Ready for command!")
# listen for voice and convert it into text format
text_version = recognize_voice()
# give "text_version" to reply() fn
reply(text_version)