-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassistant.py
More file actions
151 lines (134 loc) · 4.44 KB
/
assistant.py
File metadata and controls
151 lines (134 loc) · 4.44 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
import time
import speech_recognition as sr
import json
import subprocess
from gtts import gTTS
import vlc
import sys
LANG = "de-DE";
DATA = dict();
PID = dict();
VARS = dict();
CONFIG = dict();
last_action = 0;
def getVar(name, defaut):
if name in VARS:
return VARS[name];
else:
return default;
with open('config.json') as file:
CONFIG = json.loads(file.read());
commandfile = CONFIG['data-file'];
if len(sys.argv)>1:
commandfile = sys.argv[1];
with open(commandfile) as file:
DATA = json.loads(file.read());
def log(msg):
print("[ASSISTANT] ", msg);
def say(msg, force=False):
if(msg==None or not getVar('talk', False) and not force):
return;
result = gTTS(text=msg, lang=LANG[:2]);
result.save("voice_out.mp3")
player = vlc.MediaPlayer("voice_out.mp3");
player.play();
while str(player.get_state()) != 'State.Ended':
print(player.get_state());
time.sleep(0.1);
log("Language: " + LANG);
def process(text):
global last_action;
if(time.time() - last_action < 0.5):
return;
for key in DATA:
entry = DATA[key];
doExec = False;
if('contains' in entry):
for condition in entry['contains']:
full = True;
for word in condition:
if(not word in text):
full = False;
break;
if(full):
doExec = True;
break;
if(doExec):
execute(entry, text);
last_action = time.time();
return;
def execute(action, text):
log("Executing " + action['name'] + "...");
do = action['do'];
msg = None;
if('msg' in action):
msg = action['msg'];
if('append' in action):
if(action['append']['mode'] == 'after'):
parts = text.split(action['append']['content']);
if(len(parts)>1):
params = action['append']['content'].join(parts[1:])[1:]
log("Parameters: " + params);
do += params;
if('to-msg' in action['append'] and action['append']['to-msg']):
msg+=params;
if(action['type'] == 'cmd'):
try:
pid = subprocess.Popen(do.split(' '));
log("Executed command: " + do);
say(msg);
if('pid' in action):
if(action['pid']):
log("Saving Process");
if(action['pid-category'] in PID):
PID[action['pid-category']].append(pid);
else:
PID[action['pid-category']] = [pid];
except Exception as e:
print(e);
log("An error occured while executing shell command.");
if('error' in action):
log("Running alternative...");
execute(DATA[action['error']], text);
elif(action['type'] == 'kill'):
if action['do'] in PID:
for proc in PID[do]:
proc.terminate();
del PID[action['do']];
log("Processes terminated.");
else:
log("No process found");
elif(action['type'] == 'query'):
process(action['do']);
elif(action['type'] == 'def'):
for key in do:
VARS[key] = do[key];
log("Wert " + key + " auf " + str(do[key]) + " gesetzt.");
def callback(recognizer, audio):
try:
text = recognizer.recognize_google(audio, language="de-DE")
text = text.lower();
log("Understood: " + text);
if(not CONFIG['keyword']['enabled'] or (CONFIG['keyword']['text'] in text)):
if(CONFIG['sound']['confirm']):
media = vlc.MediaPlayer("beep.mp3");
media.play();
process(text);
except sr.UnknownValueError:
pass;
except sr.RequestError as e:
log("Service error");
log(e);
r = sr.Recognizer()
m = sr.Microphone()
with m as source:
r.adjust_for_ambient_noise(source) # we only need to calibrate once, before we start listening
# start listening in the background (note that we don't have to do this inside a `with` statement)
stop_listening = r.listen_in_background(m, callback)
log("Ready");
try:
while True:
time.sleep(0.1)
except KeyboardInterrupt:
stop_listening(wait_for_stop=False)
log("Exit.");