-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathErick.py
267 lines (237 loc) · 8.58 KB
/
Erick.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
import speech_recognition as sr
import pyttsx3
import sys
import re
import webbrowser
import smtplib
import requests
import subprocess
from bs4 import BeautifulSoup
from urllib.request import urlopen
import wikipedia
import wolframalpha
import ctypes
import datetime
from time import strftime
engine = pyttsx3.init()
# RATE
rate = engine.getProperty('rate') # Getting details of current speaking rate
engine.setProperty('rate', 170) # Setting up new voice rate
# VOICE
voices = engine.getProperty('voices') # Getting details of current voice
# Changing index, changes voices. o for male, 1 for female
engine.setProperty('voice', voices[0].id)
welcomeMessage = 'Hi, I am Erick, your intelligent virtual assistant. How can I help you?'
print(welcomeMessage)
engine.say(welcomeMessage)
engine.runAndWait()
headers = {
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36'}
app_id = 'wolfram_API' # wolfram_API_here
def newCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
print('Say something...')
r.pause_threshold = 1
r.adjust_for_ambient_noise(source, duration=1)
audio = r.listen(source)
try:
command = r.recognize_google(audio).lower()
print('You said: ' + command + '\n')
# loop back to continue to listen for commands if unrecognizable speech is received
except sr.UnknownValueError:
print('Sorry I can\'t understand')
command = newCommand()
return command
def erickResponse(audio):
print(audio)
engine.say(audio)
engine.runAndWait()
def assistant(command):
# if statements for executing commands
# Questions about Erick
if 'your name' in command:
erickResponse('My name is Erick. Nice to meet you!')
elif 'who are you' in command:
erickResponse('I\'m Erick, your intelligent virtual assistant!')
elif 'do you feel' in command:
erickResponse('I\'m doing great, thanks for asking.')
elif 'old are you' in command:
erickResponse('I was launched in June of 2019, but growing fastly.')
elif 'who built you' in command:
erickResponse(
'Itiel Maimon, the greatest programmer of all time built me.')
elif 'what can you do' in command:
erickResponse(
'I can do a lot of things, to help you throughout your day.')
elif 'help me' in command:
erickResponse('I\'m here to help, you can ask me what I can do.')
elif 'like siri' in command:
erickResponse('I like Siri, she\'s very nice.')
# Greet Erick
elif 'hello' in command:
day_time = int(strftime('%H'))
if day_time < 12:
erickResponse('Hello, Good morning!')
elif 12 <= day_time < 18:
erickResponse('Hello, Good afternoon!')
else:
erickResponse('Hello, Good evening!')
elif 'thank you' in command:
erickResponse('You\'re Welcome!')
# Make Erick stop
elif 'shut down' in command:
erickResponse('Bye bye. Have a nice day!')
sys.exit()
# Open Twitter
elif 'open twitter' in command:
reg_ex = re.search('open twitter (.*)', command)
url = 'https://www.twitter.com/'
if reg_ex:
handle = reg_ex.group(1)
url = url + handle
webbrowser.open(url)
erickResponse(
'Opening Twitter.')
# Open Instagram
elif 'open instagram' in command:
reg_ex = re.search('open instagram (.*)', command)
url = 'https://www.instagram.com/'
if reg_ex:
handle = reg_ex.group(1)
url = url + handle
webbrowser.open(url)
erickResponse(
'Opening Instagram.')
# Open subreddit Reddit
elif 'open reddit' in command:
reg_ex = re.search('open reddit (.*)', command)
url = 'https://www.reddit.com/'
if reg_ex:
subreddit = reg_ex.group(1)
url = url + 'r/' + subreddit
webbrowser.open(url)
erickResponse(
'Opening Reddit.')
# Open any website
elif 'open' in command:
reg_ex = re.search('open (.+)', command)
if reg_ex:
domain = reg_ex.group(1)
url = 'https://www.' + domain
webbrowser.open(url)
erickResponse(
'Opening ' + domain)
# Make a search on Google
elif 'search' in command:
reg_ex = re.search('search (.+)', command)
if reg_ex:
subject = reg_ex.group(1)
url = 'https://www.google.com/search?q=' + subject
webbrowser.open(url)
erickResponse(
'Searching for ' + subject + ' on Google.')
# Play a song on Youtube
elif 'play' in command:
reg_ex = re.search('play (.+)', command)
if reg_ex:
searchedSong = reg_ex.group(1)
url = 'https://www.youtube.com/results?q=' + searchedSong
try:
source_code = requests.get(url, headers=headers, timeout=15)
plain_text = source_code.text
soup = BeautifulSoup(plain_text, "html.parser")
songs = soup.findAll('div', {'class': 'yt-lockup-video'})
song = songs[0].contents[0].contents[0].contents[0]
hit = song['href']
webbrowser.open('https://www.youtube.com' + hit)
erickResponse('Playing ' + searchedSong + ' on Youtube.')
except Exception as e:
webbrowser.open(url)
erickResponse('Searching for ' + searchedSong + ' on Youtube.')
# Send Email
elif 'email' in command:
erickResponse('Who is the recipient?')
recipient = newCommand()
if 'someone' in recipient:
erickResponse('What should I say to him?')
content = newCommand()
try:
mail = smtplib.SMTP('smtp.gmail.com', 587)
mail.ehlo()
mail.starttls()
mail.login('sender_email', 'sender_password')
mail.sendmail('sender_email', 'receiver_email', content)
mail.close()
erickResponse(
'Email has been sent successfuly.')
except Exception as e:
print(e)
else:
erickResponse('I don\'t know anyone named ' + recipient + '.')
# Launch apps
elif 'launch' in command:
reg_ex = re.search('launch (.*)', command)
if reg_ex:
appname = reg_ex.group(1)
appname1 = appname+".exe"
subprocess.call([appname1])
erickResponse('Launching ' + appname + '.')
# Get current time
elif 'time' in command:
now = datetime.datetime.now()
erickResponse('Current time is %d:%d.' %
(now.hour, now.minute))
# Get recent news
elif 'news' in command:
try:
news_url = "https://news.google.com/news/rss"
Client = urlopen(news_url)
xml_page = Client.read()
Client.close()
soup_page = BeautifulSoup(xml_page, "html.parser")
news_list = soup_page.findAll("item")
for news in news_list[:5]:
erickResponse(news.title.text)
except Exception as e:
print(e)
# Lock the device
elif 'lock' in command:
try:
erickResponse("Locking the device.")
ctypes.windll.user32.LockWorkStation()
except Exception as e:
print(str(e))
# Ask general questions
elif 'tell me about' in command:
reg_ex = re.search('tell me about (.*)', command)
try:
if reg_ex:
topic = reg_ex.group(1)
erickResponse(wikipedia.summary(topic, sentences=3))
except Exception as e:
erickResponse(e)
elif any(c in command for c in ("what is", "what\'s")):
reg_ex = re.search(' (.*)', command)
try:
if reg_ex:
topic = reg_ex.group(1)
erickResponse(wikipedia.summary(topic, sentences=2))
except Exception as e:
erickResponse(e)
# All other cases
else:
try:
# wolframalpha
client = wolframalpha.Client(app_id)
res = client.query(command)
answer = next(res.results).text
erickResponse(answer)
except:
try:
# wikipedia
erickResponse(wikipedia.summary(command, sentences=2))
except Exception as e:
erickResponse(e)
while True:
assistant(newCommand())