Skip to content

Commit fb8d63e

Browse files
authored
Create voice assistant
1 parent c9e1159 commit fb8d63e

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed

Diff for: python/voice assistant

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import datetime
2+
import os
3+
import re
4+
import smtplib
5+
import subprocess
6+
import sys
7+
import urllib
8+
import urllib.request
9+
import webbrowser
10+
from time import strftime
11+
import pafy
12+
import speech_recognition as sr
13+
import vlc
14+
from bs4 import BeautifulSoup as soup
15+
from pyowm import OWM
16+
from speech_recognition.__main__ import source
17+
from youtube_dl.extractor import reddit
18+
19+
20+
def AssistantResponse(audio):
21+
print(audio)
22+
for _ in audio.splitlines():
23+
os.system("say " + audio)
24+
25+
26+
def myCommand():
27+
r1 = sr.Recognizer()
28+
with sr.Microphone() as source1:
29+
print('Say something...')
30+
r1.pause_threshold = 1
31+
r1.adjust_for_ambient_noise(source1, duration=1)
32+
audio = r1.listen(source1)
33+
try:
34+
command = r1.recognize_google(audio).lower()
35+
print('You said: ' + command + '\n')
36+
# loop back to continue to listen for commands if unrecognizable speech is received
37+
except sr.UnknownValueError:
38+
print('....')
39+
command = myCommand()
40+
return command
41+
42+
def assistant(command, html_content=(), r1=()):
43+
if 'shutdown' in command:
44+
AssistantResponse('Bye bye Sir. Have a nice day')
45+
sys.exit()
46+
# open website
47+
elif 'open' in command:
48+
reg_ex = re.search('open (.+)', command)
49+
if reg_ex:
50+
domain = reg_ex.group(1)
51+
print(domain)
52+
url = 'https://www.' + domain
53+
webbrowser.open(url)
54+
AssistantResponse('The website you have requested has been opened for you Sir.')
55+
else:
56+
pass
57+
# greetings
58+
elif 'hello' in command:
59+
day_time = int(strftime('%H'))
60+
if day_time < 12:
61+
AssistantResponse('Hello Sir. Good morning')
62+
elif 12 <= day_time < 18:
63+
AssistantResponse('Hello Sir. Good afternoon')
64+
else:
65+
AssistantResponse("Hello Sir. Good evening")
66+
# top stories from google news
67+
elif 'news for today' in command:
68+
try:
69+
news_url = "https://news.google.com/news/rss"
70+
Client = urllib.request.urlopen(news_url)
71+
xml_page = Client.read()
72+
Client.close()
73+
soup_page = soup(xml_page, "xml")
74+
news_list = soup_page.findAll("item")
75+
for news in news_list[:15]:
76+
AssistantResponse(news.title.text.encode('utf-8'))
77+
except Exception as e:
78+
print(e)
79+
# current weather
80+
elif 'current weather' in command:
81+
reg_ex = re.search('current weather in (.*)', command)
82+
if reg_ex:
83+
city = reg_ex.group(1)
84+
owm = OWM(API_key='ab0d5e80e8dafb2cb81fa9e82431c1fa')
85+
obs = owm.weather_at_place(city)
86+
w = obs.get_weather()
87+
k = w.get_status()
88+
x = w.get_temperature(unit='celsius')
89+
AssistantResponse(
90+
'Current weather in %s is %s. The maximum temperature is %0.2f and the minimum temperature is %0.2f '
91+
'degree celsius' % (
92+
city, k, x['temp_max'], x['temp_min']))
93+
# time
94+
elif 'time' in command:
95+
now = datetime.datetime.now()
96+
AssistantResponse('Current time is %d hours %d minutes' % (now.hour, now.minute))
97+
# email
98+
elif 'email' in command:
99+
AssistantResponse('Who is the recipient?')
100+
recipient = myCommand()
101+
if 'rajat' in recipient:
102+
AssistantResponse('What should I say to him?')
103+
content = myCommand()
104+
mail = smtplib.SMTP('smtp.gmail.com', 587)
105+
mail.ehlo()
106+
mail.starttls()
107+
mail.login('your_email_address', 'your_password')
108+
mail.sendmail('sender_email', 'receiver_email', content)
109+
mail.close()
110+
AssistantResponse('Email has been sent successfully. You can check your inbox.')
111+
else:
112+
AssistantResponse('I don\'t know what you mean!')
113+
# launch reddit
114+
elif 'launch' in command:
115+
reg_ex = re.search('launch (.*)', command)
116+
if reg_ex:
117+
subprocess.Popen(["open", "-n", "/Applications/" + reddit.app], stdout=subprocess.PIPE)
118+
AssistantResponse('I have launched the desired application')
119+
# launch song
120+
elif 'play me a song' in command:
121+
Assistant_Response('What song should I play Sir?')
122+
audio2 = r1.listen(source)
123+
voice_d = r1.recognize_google(audio2)
124+
search_results = re.findall(r'href=\"/watch\?v=(.{11})', html_content.read().decode())
125+
t = ("http://www.youtube.com/watch?v=" + search_results[0])
126+
video = pafy.new(t)
127+
best = video.getbest()
128+
playurl = best.url
129+
Instance = vlc.Instance()
130+
player = Instance.media_player_new()
131+
Media = Instance.media_new(playurl)
132+
Media.get_mrl()
133+
player.set_media(Media)
134+
player.play()
135+
datetime.time.sleep(120)

0 commit comments

Comments
 (0)