-
Notifications
You must be signed in to change notification settings - Fork 1
/
jarvis.py
134 lines (102 loc) · 3.85 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
import pyttsx3
import webbrowser
import smtplib
import random
import urllib.request
import urllib.parse
import speech_recognition as sr
import wikipedia
import datetime
import wolframalpha
import os
import re
import sys
engine = pyttsx3.init('sapi5')
#client = wolframalpha.Client('XLXUE2-AUUG43RE27')
voices = engine.getProperty('voices')
#print(voices)
engine.setProperty('voice', voices[0].id)
def speak(audio):
print('Computer: ' + audio)
engine.say(audio)
engine.runAndWait()
def greetMe():
currentH = int(datetime.datetime.now().hour)
if currentH >= 0 and currentH < 12:
speak('Good Morning!')
if currentH >= 12 and currentH < 18:
speak('Good Afternoon!')
if currentH >= 18 and currentH !=0:
speak('Good Evening!')
greetMe()
speak('Hello Sir, I am your assistant')
speak('How may I help you?')
def myCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 1
audio = r.listen(source)
try:
query = r.recognize_google(audio, language='en-in')
print('User: ' + query + '\n')
except sr.UnknownValueError:
speak('Sorry sir! I didn\'t get that! Try typing the command!')
query = str(input('Command: '))
return query
if __name__ == '__main__':
while True:
query = myCommand();
query = query.lower()
if 'open youtube' in query:
speak('okay')
webbrowser.open('www.youtube.com')
elif 'play' in query:
speak('okay')
query_string = urllib.parse.urlencode({"search_query" : query.split()[1:len(query)-1]})
html_cont = urllib.request.urlopen("http://www.youtube.com/results?"+query_string)
search_res = re.findall(r'href=\"\/watch\?v=(.{11})', html_cont.read().decode())
#print("http://www.youtube.com/watch?v=" + search_res[0])
webbrowser.open_new("http://www.youtube.com/watch?v={}".format(search_res[0]))
elif 'open google' in query:
speak('okay')
webbrowser.open('www.google.co.in')
elif 'where is' in query:
speak('okay')
s = "".join(query.split()[2:len(query)-1])
print(s)
webbrowser.open('https://www.google.com/maps/place/'+s)
elif 'open gmail' in query:
speak('okay')
webbrowser.open('www.gmail.com')
elif query in ["what\'s up",'how are you']:
stMsgs = ['Just doing my thing!', 'I am fine!', 'Nice!', 'I am nice and full of energy']
speak(random.choice(stMsgs))
elif query in ['nothing','abort','stop','exit','bye','goodbye']:
speak('That\'s a nice talk with you ')
speak('Bye Sir, have a good day.')
sys.exit()
elif query in ['hello','hi','hello']:
speak('Hello Sir')
else:
query = query
speak('Searching...')
try:
results = wikipedia.summary(query, sentences=2)
speak('Got it.')
#speak('WIKIPEDIA says - ')
speak(results)
'''try:
res = client.query(query)
results = next(res.results).text
speak('WOLFRAM-ALPHA says - ')
speak('Got it.')
speak(results)
except:
results = wikipedia.summary(query, sentences=2)
speak('Got it.')
speak('WIKIPEDIA says - ')
speak(results)'''
except:
webbrowser.open('https://www.google.co.in/search?ei=EZXCXMHXFavEz7sP-LSL8Ag&q='+query)
speak('Next Command! Sir!')