-
Notifications
You must be signed in to change notification settings - Fork 0
/
jarvis.py
274 lines (229 loc) · 9.21 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
import pyttsx3
import speech_recognition as sr
import datetime
import wikipedia
import webbrowser
import os
import cv2
import time
import random
import smtplib
import pywhatkit as kit
import pyjokes
import pyautogui
import requests
from bs4 import BeautifulSoup
from pywikihow import search_wikihow
mail_ids = {"suryansh": "[email protected]", "dad": "[email protected]", "brother": "[email protected]"}
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
print(voices[3].id)
engine.setProperty('voice', voices[3].id)
def speak(audio):
engine.say(audio)
engine.runAndWait()
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!")
speak("I am Jarvis. How may I help you?")
def takeCommand():
"""It takes microphone as input and returns string as output"""
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening....")
r.pause_threshold = 1
audio = r.listen(source, timeout=10, phrase_time_limit=8)
try:
print("Recognizing....")
query = r.recognize_google(audio, language='en-in')
print(f"User said: {query}\n")
except Exception as e:
print("Say that again please....")
return "None"
return query
def sendEmail(to, content):
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login('Users email id', 'Users password')
server.sendmail('Receipents email', to, content)
server.close()
def news():
news_link = "https://newsapi.org/v2/top-headlines?sources=techcrunch&apiKey=f25d2258e66d4a788992cf6b8d7fc495"
main_page = requests.get(news_link).json()
articles = main_page["articles"]
head = []
day = ["first", "second", "third", "fourth", "fifth",
"sixth", "seventh", "eighth", "ninth", "tenth"]
for ar in articles:
head.append(ar["title"])
for i in range(len(day)):
print(f"Todays {day[i]} news is: ", head[i])
speak(f"Todays {day[i]} news is: {head[i]}")
if __name__ == "__main__":
wishMe()
while True:
query = takeCommand().lower()
chrome = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"
webbrowser.register(
'chrome', None, webbrowser.BackgroundBrowser(chrome))
# Logic to take command based on query
if 'wikipedia' in query:
print("Searching wikipedia....")
query = query.replace("wikipedia", "")
results = wikipedia.summary(query, sentences=2)
speak("According to wikipedia")
print(results)
speak(results)
elif 'open youtube' in query:
webbrowser.get('chrome').open('youtube.com')
speak("opening youtube")
elif 'search on youtube' in query:
speak("What would you like to see sir?")
search = input("What would you like to see sir? ")
webbrowser.open(f"https://www.youtube.com/search?q={search}")
speak(f"opening {search} in youtube")
elif 'close notepad plus plus' in query:
speak('closing notepad++')
os.system("taskkill /f /im notepad++.exe")
elif 'open google' in query:
webbrowser.get('chrome').open('google.com')
speak("opening google")
elif 'search on google' in query:
while True:
time.sleep(2)
speak("What would you like to see sir?")
browse = input("What would you like to see sir? ")
try:
if 'exit' in browse or 'close' in browse:
speak("Ok sir")
break
else:
webbrowser.open(
f"https://www.google.com/search?q={browse}")
speak(f"opening {browse} in google")
except:
speak("Sorry sir i am unable to find this")
elif 'open amazon' in query:
webbrowser.get('chrome').open('amazon.in')
speak("opening amazon")
elif 'open flipkart' in query:
webbrowser.get('chrome').open('flipkart.com')
speak("opening flipkart")
elif 'open lms' in query:
webbrowser.get('chrome').open('lms-kjsce.somaiya.edu')
speak("opening lms")
elif 'open notebook' in query:
nb_path = "Path of Jupyter Notebook in your PC"
os.startfile(nb_path)
speak("opening jupyter notebook")
elif 'open notepad plus plus' in query:
npath = "Path of Notepad ++ in your PC"
os.startfile(npath)
speak("opening notepad ++")
elif 'close notepad plus plus' in query:
speak('closing notepad++')
os.system("taskkill /f /im notepad++.exe")
elif 'open teams' in query:
tpath = "Path of Microsoft Teams in your PC"
os.startfile(tpath)
speak("opening microsoft teams")
elif 'close ms teams' in query:
speak('closing microsoft teams')
os.system("taskkill /f /im Microsoft Teams.lnk")
elif 'open cmd' in query:
os.system('start cmd')
speak("opening command prompt")
elif 'close cmd' in query:
speak('closing command prompt')
os.system("taskkill /f /im cmd.exe ")
elif 'open camera' in query:
speak("opening camera")
cam = cv2.VideoCapture(0)
pic = 0
while True:
pic += 1
ret, img = cam.read()
print(ret)
print(img)
cv2.imshow('camera', img)
k = cv2.waitKey(10)
if k == 27:
break
print(pic)
cam.release()
cv2.destroyAllWindows()
elif 'time' in query:
strTime = datetime.datetime.now().strftime("%H:%M:%S")
speak(f"Sir, the time is {strTime}")
elif 'send mail' in query:
speak("whom should i send the mail sir?")
name = takeCommand().lower()
if name in mail_ids:
try:
speak("What should i say?")
content = takeCommand().lower()
to = mail_ids[name]
sendEmail(to, content)
speak("Email has been sent!")
except Exception as e:
print(e)
speak("Sorry! Email could not be sent")
else:
speak("User not found sir")
elif 'send message' in query:
kit.sendwhatmsg(person, msg, time_hour, time_min)
elif 'tell me a joke' in query:
joke = pyjokes.get_joke()
print(joke)
speak(joke)
elif 'switch the window' in query:
pyautogui.keyDown("alt")
pyautogui.press("tab")
time.sleep(1)
pyautogui.keyUp("alt")
speak("window has been switched")
elif "what's the news" in query:
speak("Browsing the latest news. Please wait sir")
news()
elif "check temperature" in query:
speak("Which city's temperature would you like to check sir?")
city = input(
"Which city's temperature would you like to check sir? ")
temp_url = f"https://www.google.com/search?q=temperature of {city}"
req = requests.get(temp_url)
data = BeautifulSoup(req.text, "html.parser")
temperature = data.find("div", class_="BNeawe").text
speak(f"Current temperature in {city} is {temperature}")
elif "give me some information" in query:
while True:
speak("What information would you like to know sir?")
Query = takeCommand()
try:
if 'exit' in Query or 'close' in Query:
speak("That's all for today's information sir")
break
else:
max_result = 1
Result = search_wikihow(Query, max_result)
assert len(Result) == 1
print(Result[0].summary)
speak(Result[0].summary)
except:
speak("Sorry sir i am unable to find this")
# elif 'shut down the system' in query:
# os.system("shutdown /r /t 5")
#
# elif 'restart the system' in query:
# os.system("shutdown /r /t 5")
#
# elif 'let the system sleep' in query:
# os.system("rundll32.exe powerprof.dll,SetSuspendState 0,1,0")
elif 'go to sleep' in query:
speak("goodbye sir!")
os.abort()