-
Notifications
You must be signed in to change notification settings - Fork 1
/
microphone_recognition.py
executable file
·119 lines (97 loc) · 3.78 KB
/
microphone_recognition.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
#!/usr/env/python3
import speech_recognition as sr
r = sr.Recognizer()
m = sr.Microphone()
try:
print("A moment of silence, please...")
with m as source: r.adjust_for_ambient_noise(source)
print("Set minimum energy threshold to {}".format(r.energy_threshold))
while True:
print("Say something!")
with m as source: audio = r.listen(source)
print("Got it! Now to recognize it...")
try:
value = r.recognize_google(audio)
if str is bytes:
print(u"You said {}".format(value).encode("utf-8"))
else:
print("You said {}".format(value))
except sr.UnknownValueError:
print("Oops! Didn't catch that")
except sr.RequestError as e:
print("Uh oh! Couldn't request results from Google Speech Recognition service; {0}".format(e))
except KeyboardInterrupt:
pass
def assistant(command):
"if statements for executing commands"
if '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)
print('Done!')
elif 'open website' in command:
reg_ex = re.search('open website (.+)', command)
if reg_ex:
domain = reg_ex.group(1)
url = 'https://www.' + domain
webbrowser.open(url)
print('Done!')
else:
pass
elif 'what\'s up' in command:
talkToMe('Just doing my thing')
elif 'joke' in command:
res = requests.get(
'https://icanhazdadjoke.com/',
headers={"Accept":"application/json"}
)
if res.status_code == requests.codes.ok:
talkToMe(str(res.json()['joke']))
else:
talkToMe('oops!I ran out of jokes')
elif 'current weather in' in command:
reg_ex = re.search('current weather in (.*)', command)
if reg_ex:
city = reg_ex.group(1)
weather = Weather()
location = weather.lookup_by_location(city)
condition = location.condition()
talkToMe('The Current weather in %s is %s The tempeture is %.1f degree' % (city, condition.text(), (int(condition.temp())-32)/1.8))
elif 'weather forecast in' in command:
reg_ex = re.search('weather forecast in (.*)', command)
if reg_ex:
city = reg_ex.group(1)
weather = Weather()
location = weather.lookup_by_location(city)
forecasts = location.forecast()
for i in range(0,3):
talkToMe('On %s will it %s. The maximum temperture will be %.1f degree.'
'The lowest temperature will be %.1f degrees.' % (forecasts[i].date(), forecasts[i].text(), (int(forecasts[i].high())-32)/1.8, (int(forecasts[i].low())-32)/1.8))
elif 'email' in command:
talkToMe('Who is the recipient?')
recipient = myCommand()
if 'John' in recipient:
talkToMe('What should I say?')
content = myCommand()
#init gmail SMTP
mail = smtplib.SMTP('smtp.gmail.com', 587)
#identify to server
mail.ehlo()
#encrypt session
mail.starttls()
#login
mail.login('username', 'password')
#send message
mail.sendmail('John Fisher', 'JARVIS2.0@protonmail.com', content)
#end mail connection
mail.close()
talkToMe('Email sent.')
else:
talkToMe('I don\'t know what you mean!')
talkToMe('I am ready for your command')
#loop to continue executing multiple commands
while True:
assistant(myCommand())