-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJARVIS.py
More file actions
596 lines (518 loc) · 19.1 KB
/
JARVIS.py
File metadata and controls
596 lines (518 loc) · 19.1 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
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
import datetime
import pickle
import smtplib
import subprocess
from GoogleNews import GoogleNews
import pywhatkit as kit
import tkinter as tk
from bs4 import BeautifulSoup
from getpass import getpass
import pytz
import time as tm
import speech_recognition as sr # importing speech recognition package from google api
import requests
import playsound # to play saved mp3 file
import wikipedia
from gtts import gTTS # google text to speech
import os # to save/open files
import wolframalpha # to calculate strings into formula, its a website which provides api, 100 times per day
from selenium import webdriver # to control browser operations
from selenium.webdriver.common.keys import Keys
from io import BytesIO
from io import StringIO
import webbrowser
# from webdriver_manager.chrome import ChromeDriverManager
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
import pyautogui
import psutil
import pyjokes
import numpy as np
driver = webdriver.Chrome(
executable_path="your path")
SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']
MONTHS = ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november",
"december"]
DAYS = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]
DAY_EXT = ["rd", "th", "st", "nd"]
WAKE = "jarvis"
num = 1
webbrowser.Mozilla()
date = datetime.datetime.today().date().strftime('%d/%m/%Y')
googlenews = GoogleNews(lang='en', period='d', start=date, end=date)
root = tk.Tk()
peo = {
your contacts
}
pyautogui.hotkey('winleft', 'down')
def speak(output):
global num
num += 1
print("JARVIS: ", output)
toSpeak = gTTS(text=output, lang='en-US', slow=False)
file = str(num) + ".mp3"
toSpeak.save(file)
playsound.playsound(file, True)
os.remove(file)
def get_audio():
r = sr.Recognizer()
audio = ''
with sr.Microphone() as source:
print("Speak...")
audio = r.listen(source, phrase_time_limit=20)
print("Stop.")
try:
text = r.recognize_google(audio, language='en-in')
print("You: ", text)
return text.lower()
except:
# speak("Could not understand your audio, PLease try again!")
return '0'
def get_alarm_audio():
r = sr.Recognizer()
audio = ''
with sr.Microphone() as source:
print("Speak...")
audio = r.listen(source, phrase_time_limit=1)
print("Stop.")
try:
text = r.recognize_google(audio, language='en-in')
print("You: ", text)
return text.lower()
except:
# speak("Could not understand your audio, PLease try again!")
return '0'
def send_mail():
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
speak('What is the content?')
body = input('What is the content?')
server.login('your login')
subject = 'From JARVIS'
speak('To whom are you sending this? Please write below.')
receiver = input('To whom are you sending this?')
msg = f"Subject: {subject}\n\n{body}"
server.sendmail(
'JARVIS',
receiver,
msg
)
def search_web(input):
""" driver = webdriver.Edge()
driver.implicitly_wait(1)"""
global indx
if 'youtube' in input.lower():
speak("Opening in youtube")
indx = input.lower().split().index('youtube')
query = input.split()[indx + 1:]
if query[0] == 'for':
del query[0]
else:
pass
driver.get(url="http://www.youtube.com/results?search_query=" + ''.join(query))
""" speak('What do you want to search?')
search_que = get_audio()
search_box = driver.find_element_by_xpath(
'//*[@id="search"]')
search_box.send_keys(search_que)
search_button = driver.find_element_by_xpath(
'//*[@id="search-icon-legacy"]')
search_button.click()"""
return
elif 'wikipedia' in input.lower() or 'who is' in input.lower() or 'what is' in input.lower():
speak("searching in wikipedia")
query = input.replace("wikipedia", "")
query = input.replace("who is", "")
query = input.replace("what is", "")
results = wikipedia.summary(query, sentences=2)
speak("According to wikipedia")
speak(results)
return
else:
if 'google' or 'internet' or 'web' in input:
try:
indx = input.lower().split().index('google for')
query = input.split()[indx + 1:]
if query[0] == 'for':
del query[0]
else:
pass
webbrowser.open(url="https://www.google.com/search?q=" + ''.join(query))
except ValueError:
webbrowser.open(url="https://www.google.com/search?q=" + ''.join(input.split()))
elif 'search' in input:
try:
indx = input.lower().split().index('google')
except ValueError:
webbrowser.open(url="https://www.google.com/search?q=" + ''.join(input.split()))
query = input.split()[indx + 1:]
if query[0] == 'for':
del query[0]
else:
pass
webbrowser.open(url="https://www.google.com/search?q=" + ''.join(query))
else:
webbrowser.open(url="https://www.google.com/search?q=" + ''.join(input.split()))
return
def note(txr):
da = datetime.datetime.now()
file_name = 'your location' + str(da).replace(":", "-") + "-note.txt"
with open(file_name, "w") as f:
f.write(txr)
subprocess.Popen(["notepad.exe", file_name])
def authenticate_google():
"""Shows basic usage of the Google Calendar API.
Prints the start and name of the next 10 events on the user's calendar.
"""
creds = None
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('calendar', 'v3', credentials=creds)
return service
def get_events(day, service):
# Call the Calendar API
date = datetime.datetime.combine(day, datetime.datetime.min.time())
end_date = datetime.datetime.combine(day, datetime.datetime.max.time())
utc = pytz.UTC
date = date.astimezone(utc)
end_date = end_date.astimezone(utc)
events_result = service.events().list(calendarId='primary', timeMin=date.isoformat(), timeMax=end_date.isoformat(),
singleEvents=True,
orderBy='startTime').execute()
events = events_result.get('items', [])
if not events:
speak('No upcoming events found.')
else:
speak(f"You have {len(events)} events on this day.")
for event in events:
start = event['start'].get('dateTime', event['start'].get('date'))
print(start, event['summary'])
start_time = str(start.split("T")[1].split("-")[0])
if int(start_time.split(":")[0]) < 12:
start_time = start_time + "am"
else:
start_time = str(int(start_time.split(":")[0]) - 12) + start_time.split(":")[1]
start_time = start_time + "pm"
speak(event["summary"] + " at " + start_time)
def get_date(txr):
txr = txr.lower()
today = datetime.date.today()
if txr.count("today") > 0:
return today
day = -1
day_of_week = -1
month = -1
year = today.year
for word in txr.split():
if word in MONTHS:
month = MONTHS.index(word) + 1
elif word in DAYS:
day_of_week = DAYS.index(word)
elif word.isdigit():
day = int(word)
else:
for ext in DAY_EXT:
found = word.find(ext)
if found > 0:
try:
day = int(word[:found])
except Exception as e:
print(e)
pass
# THE NEW PART STARTS HERE
if month < today.month and month != -1: # if the month mentioned is before the current month set the year to the
# next
year = year + 1
# This is slightly different from the video but the correct version
if month == -1 and day != -1: # if we didn't find a month, but we have a day
if day < today.day:
month = today.month + 1
else:
month = today.month
# if we only found a dta of the week
if month == -1 and day == -1 and day_of_week != -1:
current_day_of_week = today.weekday()
dif = day_of_week - current_day_of_week
if dif < 0:
dif += 7
if txr.count("next") >= 1:
dif += 7
return today + datetime.timedelta(dif)
if day != -1: # FIXED FROM VIDEO
return datetime.date(month=month, day=day, year=year)
def open_application(inp):
if "chrome" in inp:
speak("Google Chrome")
os.startfile(r'your location\chrome.exe')
return
elif "firefox" in inp or "mozilla" in inp:
speak("Opening Mozilla Firefox")
os.startfile(r'yorlocation\firefox.exe')
return
elif "word" in inp:
speak("Opening Microsoft Word")
os.startfile(r'your location\Word')
return
elif "excel" in inp:
speak("Opening Microsoft Excel")
os.startfile(r'your location\Excel')
return
else:
speak("Application not available")
return
def open_link(url):
driver.get(url)
"""def get_news(news):
googlenews.clear()
googlenews.search(str(news))
googlenews.getpage(1)
result = googlenews.result()
art = dict()
root.geometry('400x400')
for i in range(len(result)):
title = result[i]['title']
desc = result[i]['desc']
url = result[i]['link']
source = result[i]['media']
a_date = result[i]['date']
art[title] = title, desc, source, a_date, url
list_temp = [title, desc, a_date, source]
tk.Button(root, text='\n'.join(list_temp), command=lambda: open_link(url=url)).pack()
tk.Scrollbar(root).pack(side='right', fill='y')
root.mainloop()
root.title('News')"""
def time():
ct = [int(datetime.datetime.now().strftime('%I')), int(datetime.datetime.now().strftime('%M')),
datetime.datetime.now().strftime('%p')]
speak('The current time is: ')
speak(' '.join(map(str, ct)))
def date():
dt = [int(datetime.datetime.now().day), int(datetime.datetime.now().month), int(datetime.datetime.now().year)]
speak('The current date is: ')
speak('/'.join(map(str, dt)))
def play_song(song_name):
print('d')
song_name = song_name.lower()
print('q')
song = song_name.replace('play', '')
print(song)
kit.playonyt(song)
print('r')
def satw(path, number):
tm.sleep(6)
webbrowser.open(f'https://web.whatsapp.com/send?phone={number}')
pyautogui.hotkey('winleft', 'up')
pyautogui.click(2839, 173)
pyautogui.click(2858, 281)
pyautogui.typewrite(['*', '.', '*', 'enter'])
pyautogui.typewrite(path)
pyautogui.typewrite(['enter'])
tm.sleep(2)
pyautogui.click(2836, 1869)
def wish_me(name):
hour = datetime.datetime.now().hour
if 6 <= hour < 12:
speak(f'Good Morning! Welcome back, {name}!')
elif 12 <= hour < 18:
speak(f'Good Afternoon! Welcome back, {name}!')
elif 18 <= hour < 22:
speak(f'Good Evening! Welcome Back, {name}!')
else:
speak('Good Night! Welcome Back!')
speak('I am Jarvis your Personal Assistant.')
def screenshot():
da = datetime.datetime.now()
file_name = 'your location' + str(da).replace(":", "-") + "-screenshot.png"
img = pyautogui.screenshot()
img.save(file_name)
def cpu():
usage = str(psutil.cpu_percent())
speak('CPU is at ' + usage + ' percent.')
battery = str(psutil.sensors_battery().percent)
if psutil.sensors_battery().power_plugged:
speak('Battery is at ' + battery + ' percent and is plugged in.')
else:
speak('Battery is at ' + battery + ' percent and is not plugged in.')
def jokes():
speak(pyjokes.get_joke())
def process_text(inp):
service = authenticate_google()
inp = inp.lower()
try:
if "who are you" in inp or "define yourself" in inp:
te = '''Hello, I am Jarvis. Your personal Assistant.
I am here to make your life easier.
You can command me to perform various tasks such as calculating sums or opening applications etcetra'''
speak(te)
return
elif "who made you" in inp or "created you" in inp:
te = "I have been created by Aryan Anand."
speak(te)
return
elif "crazy" in inp:
te = """Well, there are many mental asylums in the world."""
speak(te)
return
elif 'news' in inp:
speak("I didn't quite understand what I should search for. Could you say what exactly I should search for?")
nsew = get_audio()
driver.get('https://news.google.com/topstories?hl=en-US&gl=US&ceid=US:en+')
search = driver.find_element_by_xpath('//*[@id="gb"]/div[2]/div[2]/div/form/div['
'1]/div/div/div/div/div[1]/input[2]')
search.send_keys(nsew)
search.send_keys(Keys.ENTER)
elif "calculate" in inp.lower():
app_id = "your key"
client = wolframalpha.Client(app_id)
inn = inp.lower().split().index('calculate')
query = inp.split()[inn + 1:]
res = client.query(' '.join(query))
answer = next(res.results).text
speak("The answer is " + answer)
return
elif 'cpu' in inp:
cpu()
if 'joke' in inp and 'what' not in inp:
jokes()
elif 'open' in inp:
pyautogui.press('winleft')
pyautogui.typewrite(inp.lower())
pyautogui.typewrite(['enter'])
return
elif 'send' in inp and 'mail' in inp:
send_mail()
elif 'send' in inp and 'message' in inp:
rec = inp.replace('send', '')
rec = rec.replace('whatsapp', '')
rec = rec.replace('to', '', 1)
rec = rec.replace('with', '')
rec = rec.replace('in', '')
rec = rec.replace('saying', '')
rec = rec.replace('message', '')
rec = rec.replace('through', '')
rec = rec.replace('a', '', 1)
rec = rec.replace(' ', '')
speak('what is the content?')
message = get_audio()
webbrowser.open('https://web.whatsapp.com/send?phone=' + peo[rec] + '&text=' + message)
tm.sleep(30)
pyautogui.typewrite(['enter'])
elif 'attachment' and 'message' in inp:
speak('Please enter the path')
a = input()
rec = inp.replace('send', '')
rec = rec.replace('whatsapp', '')
rec = rec.replace('to', '', 1)
rec = rec.replace('with', '')
rec = rec.replace('in', '')
rec = rec.replace('saying', '')
rec = rec.replace('message', '')
rec = rec.replace('through', '')
rec = rec.replace('a', '', 1)
rec = rec.replace('an', '', 1)
rec = rec.replace('attachment', '')
rec = rec.replace('ttchment', '')
rec = rec.replace(' ', '')
satw(a, peo[rec])
elif 'play' in inp and 'game' not in inp and 'games' not in inp:
play_song(inp)
elif 'weather' in inp:
info = requests.get('https://ipinfo.io')
data = info.json()
city = data['city']
region = data['region']
country = data['country']
app_id = "your key"
client = wolframalpha.Client(app_id)
result = client.query('weather forecast for' + city + ', ' + country)
weather = next(result.results).text
w = weather.split('\n')
speak('The weather is ' + w[0] + ' and is ' + w[1])
elif "make a note" in inp or "write this down" in inp or "remember this" in inp:
speak('What do you want to make a note of?')
note_text = get_audio()
note(note_text)
speak('Saved in notes folder!')
elif "what do i have" in inp or "do i have plans" in inp or "am i busy" in inp:
da = get_date(text)
if da:
get_events(da, service)
else:
speak("I don't understand")
elif 'search' in inp or 'play' in inp or 'who is' in inp or 'what is' in inp or 'what' in inp and \
'news' not in inp:
search_web(inp.lower())
return
elif 'the time' in inp:
time()
elif 'logout' in inp:
os.system('shutdown -l')
elif 'shutdown' in inp:
os.system('shutdown /s /t 1')
elif 'restart' in inp:
os.system('shutdown /r /t 1')
elif 'screenshot' in inp:
screenshot()
speak('Saved in photos folder!')
else:
speak("I can search the web for you, Do you want to continue?")
ans = get_audio()
if 'yes' in str(ans) or 'yeah' in str(ans):
search_web(inp)
else:
return
except Exception as e:
print(e
password = {'margi123': 'Aryan'}
pa = ''
if __name__ == "__main__":
info = requests.get('https://ipinfo.io')
data = info.json()
city = data['city']
country = data['country']
app_id = "kor key"
client = wolframalpha.Client(app_id)
result = client.query('weather forecast for' + city + ', ' + country)
weather = next(result.results).text
w = weather.split('\n')
text = '10000'
while text.count(WAKE) < 1:
speak('Get up!!')
text = get_alarm_audio()
wish_me('Aryan')
speak('The weather is ' + w[0] + ' and is ' + w[1])
while True:
text = get_audio()
try:
if text.count(WAKE) > 0:
speak("What can i do for you?")
text = get_audio()
if text == 0:
continue
# speak(text)
if "exit" in str(text) or "bye" in str(text) or "sleep" in str(text) or "stop" in str(
text) or 'offline' in \
str(text):
speak("Ok bye, " + 'Aryan' + '.')
quit()
exit()
break
process_text(text)
else:
pass
except AttributeError:
pass