-
Notifications
You must be signed in to change notification settings - Fork 20
/
jarvis.py
276 lines (215 loc) · 7.76 KB
/
jarvis.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
import pyttsx3
import wikipedia
import speech_recognition as sr
import webbrowser
import datetime
import os
import sys
import smtplib
import psutil
import pyjokes
import pyautogui
from news import speak_news, getNewsUrl
from diction import translate
from loc import weather
from youtube import youtube
from quote import tell_quote
import psutil
import pyjokes
from sys import platform
import os
import getpass
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)
# print(voices[0].id)
def speak(audio):
engine.say(audio)
engine.runAndWait()
def screenshot():
img = pyautogui.screenshot()
img.save('path of folder you want to save/screenshot.png')
def cpu():
usage = str(psutil.cpu_percent())
speak("CPU is at"+usage)
battery = psutil.sensors_battery()
speak("battery is at")
speak(battery.percent)
def joke():
for i in range(5):
speak(pyjokes.get_jokes()[i])
def takeCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
print('Listening...')
r.pause_threshold = 1
r.energy_threshold = 494
r.adjust_for_ambient_noise(source, duration=1.5)
audio = r.listen(source)
try:
print('Recognizing..')
query = r.recognize_google(audio, language='en-in')
print(f'User said: {query}\n')
except Exception as e:
# print(e)
print('Say that again please...')
return 'None'
return query
def wishMe():
hour = int(datetime.datetime.now().hour)
if hour >= 0 and hour < 12:
speak("Good Morning SIR")
elif hour >= 12 and hour < 18:
speak("Good Afternoon SIR")
else:
speak('Good Evening SIR')
weather()
speak('I am JARVIS. Please tell me how can I help you SIR?')
def sendEmail(to, content):
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login('email', 'password')
server.sendmail('email', to, content)
server.close()
def cpu():
usage = str(psutil.cpu_percent())
speak("CPU is at"+usage)
battery = psutil.sensors_battery()
speak("battery is at")
speak(battery.percent)
def joke():
for i in range(5):
speak(pyjokes.get_jokes()[i])
def screenshot():
img = pyautogui.screenshot()
img.save('path of folder you want to save/screenshot.png')
if __name__ == '__main__':
wishMe()
while True:
query = takeCommand().lower()
if 'wikipedia' in query:
speak('Searching Wikipedia....')
query = query.replace('wikipedia', '')
results = wikipedia.summary(query, sentences=2)
speak('According to Wikipedia')
print(results)
speak(results)
elif 'youtube downloader' in query:
exec(open('youtube_downloader.py').read())
elif 'voice' in query:
if 'female' in query:
engine.setProperty('voice', voices[1].id)
else:
engine.setProperty('voice', voices[0].id)
speak("Hello Sir, I have switched my voice. How is it?")
if 'inspirational quote' in query:
tell_quote()
if 'jarvis are you there' in query:
speak("Yes Sir, at your service")
elif 'open youtube' in query:
webbrowser.open_new_tab('https://youtube.com')
elif 'cpu' in query:
cpu()
elif 'joke' in query:
joke()
elif 'screenshot' in query:
speak("taking screenshot")
screenshot()
elif 'open google' in query:
webbrowser.open_new_tab('https://google.com')
elif 'open stackoverflow' in query:
webbrowser.open_new_tab('https://stackoverflow.com')
elif 'play music' in query:
os.startfile("D:\\RoiNa.mp3")
elif 'search youtube' in query:
speak('What you want to search on Youtube?')
youtube(takeCommand())
elif 'the time' in query:
strTime = datetime.datetime.now().strftime("%H:%M:%S")
speak(f'Sir, the time is {strTime}')
elif 'search' in query:
speak('What do you want to search for?')
search = takeCommand()
url = 'https://google.com/search?q=' + search
webbrowser.open_new_tab(
url)
speak('Here is What I found for' + search)
elif 'location' in query:
speak('What is the location?')
location = takeCommand()
url = 'https://google.nl/maps/place/' + location + '/&'
webbrowser.open_new_tab(url)
speak('Here is the location ' + location)
elif 'your master' in query:
if platform == "win32" or "darwin":
speak('Gaurav is my master. He created me couple of days ago')
elif platform == "linux" or platform == "linux2":
name = getpass.getuser()
speak(name, 'is my master. He is running me right now')
elif 'your name' in query:
speak('My name is JARVIS')
elif 'stands for' in query:
speak('J.A.R.V.I.S stands for JUST A RATHER VERY INTELLIGENT SYSTEM')
elif 'open code' in query:
if platform == "win32":
os.startfile(
"C:\\Users\\gs935\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe")
elif platform == "linux" or platform == "linux2" or "darwin":
os.system('code .')
elif 'shutdown' in query:
if platform == "win32":
os.system('shutdown /p /f')
elif platform == "linux" or platform == "linux2" or "darwin":
os.system('poweroff')
elif 'cpu' in query:
cpu()
elif 'joke' in query:
joke()
elif 'screenshot' in query:
speak("taking screenshot")
screenshot()
elif 'github' in query:
webbrowser.open_new_tab(
'https://github.com/gauravsingh9356')
elif 'remember that' in query:
speak("what should i remember sir")
rememberMessage = takeCommand()
speak("you said me to remember"+rememberMessage)
remember = open('data.txt', 'w')
remember.write(rememberMessage)
remember.close()
elif 'do you remember anything' in query:
remember = open('data.txt', 'r')
speak("you said me to remember that" + remember.read())
elif 'sleep' in query:
sys.exit()
elif 'dictionary' in query:
speak('What you want to search in your intelligent dictionary?')
translate(takeCommand())
elif 'news' in query:
speak('Ofcourse sir..')
speak_news()
speak('Would you like to read the while article?')
test = takeCommand()
if 'yes' in test:
speak('Ok Sir, Opening browser...')
webbrowser.open(getNewsUrl())
speak('You can now read the full news from this website.')
else:
speak('No Problem Sir')
elif 'voice' in query:
if 'female' in query:
engine.setProperty('voice', voices[0].id)
else:
engine.setProperty('voice', voices[1].id)
speak("Hello sir, I have switched my voice. How do you like it?")
elif 'email to gaurav' in query:
try:
speak('What should I say?')
content = takeCommand()
to = 'email'
sendEmail(to, content)
speak('Email has been sent!')
except Exception as e:
speak('Sorry sir, but I am not able to send an email at the moment.')