Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
163 lines (127 loc) · 4.55 KB
/
Copy pathmain.py
File metadata and controls
163 lines (127 loc) · 4.55 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
from time import ctime, sleep
import pyrebase
import pyttsx3
import datetime
import speech_recognition as sr
import webbrowser
# Firebase Database
firebaseConfig = {
"apiKey": "AIzaSyAkD-Ehz2IOwx4iA3jL_dZPpzog3vaaY94",
"authDomain": "home-automation-794c1.firebaseapp.com",
"databaseURL": "https://home-automation-794c1-default-rtdb.firebaseio.com",
"projectId": "home-automation-794c1",
"storageBucket": "home-automation-794c1.appspot.com",
"messagingSenderId": "204270994860",
"appId": "1:204270994860:web:2372c400f7c7f0cd272609",
}
firebase = pyrebase.initialize_app(firebaseConfig)
fb_db = firebase.database()
# Initialize the recognizer
r = sr.Recognizer()
# Initialize the Text to Speech _engine
speech_engine = pyttsx3.init()
def website_input(first=True):
myGetResults = fb_db.child('/command').get()
myGetResults = myGetResults.val()
mostrecentKeyID = 0
mostrecentTimestamp = 0
if myGetResults == None:
pass
else:
# To get the most recent input
for keyID in myGetResults:
if int(int(myGetResults[keyID]['Time']) > mostrecentTimestamp):
mostrecentTimestamp = int(myGetResults[keyID]['Time'])
mostrecentKeyID = myGetResults[keyID]
out = str(myGetResults[keyID]['Input'])
if ('add' in out) and (first == True):
fb_db.child('/command').remove()
speak('What do you want to add to your to do list?')
sleep(3)
website_input(first=False)
elif first == False:
to_do_dict = {
'To Do': out,
}
fb_db.child('/to-do').push(to_do_dict)
speak('Added ' + out + ' to the to do list.')
fb_db.child('/command').remove()
else:
respond(voice_data=out)
fb_db.child('/command').remove()
def upload(voice_data, person):
if person == True:
now = int(datetime.datetime.today().strftime("%Y%m%d%H%M%S"))
voice_log = {
'Output': 'Person: ' + voice_data,
'Time': now,
}
fb_db.child('/virtual_assistant/voice_log').push(voice_log)
else:
now = int(datetime.datetime.today().strftime("%Y%m%d%H%M%S"))
voice_log = {
'Output': 'Bot: ' + voice_data,
'Time': now,
}
fb_db.child('/virtual_assistant/voice_log').push(voice_log)
def speak(audio_string):
voices = speech_engine.getProperty("voices")
speech_engine.setProperty("voice", voices[1].id)
speech_engine.setProperty("rate", 160)
speech_engine.say(audio_string)
speech_engine.runAndWait()
upload(audio_string, False)
def record_audio(ask=False):
with sr.Microphone() as source:
if ask:
speak(ask)
audio = r.listen(source)
voice_data = ''
try:
voice_data = r.recognize_google(audio)
upload(voice_data, True)
except sr.UnknownValueError:
speak('Sorry, I did not get that.')
except sr.RequestError:
speak('Sorry, the service is down.')
return voice_data
def respond(voice_data):
# Name
if ('what is your name' in voice_data):
speak('My name is Alexa.')
# Time
elif ('time' in voice_data):
speak(ctime())
# Search
elif ('search' in voice_data):
search = record_audio('What do you want to search for?')
url = 'https://google.com/search?q=' + search
webbrowser.get().open(url)
speak('Here is what I found for ' + search)
# Location
elif ('find location' in voice_data):
location = record_audio('What is the location?')
url = 'https://google.nl/maps/place/' + location + '/&'
webbrowser.get().open(url)
speak('Here is the location of ' + location)
# To-Do List
# Create
elif ('add' in voice_data):
to_do = record_audio('What do you want to add to your to do list?')
to_do_dict = {
'To Do': to_do,
}
fb_db.child('/to-do').push(to_do_dict)
speak('Added ' + to_do + ' to the to do list.')
# Delete
elif ('remove' in voice_data):
fb_db.child('/to-do').remove()
speak('Removed all tasks')
# Turn Off
elif ('off' in voice_data):
exit()
speak('Hello, what would you like to do?')
while True:
voice_data = record_audio()
respond(voice_data)
website_input()