-
Notifications
You must be signed in to change notification settings - Fork 0
/
veronica.py
135 lines (116 loc) · 4.49 KB
/
veronica.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
from importsmain import *
currentpath = os.path.dirname(os.path.abspath(inspect.stack()[0][1]))
logfilepath = os.path.join(currentpath,"logs/log.txt")
errorfilepath = os.path.join(currentpath,"logs/error.txt")
#NOTE THESE FILES ARE NOT SEND TO ANYWHERE. LOG AND ERROR FILES STAYS
# ON YOUR COMPUTER UNLESS YOU SEND IT. THESE FILES ARE HELPFUL TO RESOLVE
# ANY ERRORS
def checkandcreatefile(x,y):
if os.path.exists(x) == False:
if ".txt" in x:
with open(x,"w+") as f:
f.write(y)
else:
os.mkdir(x)
#Checking if log files are present if not creating
checkandcreatefile(os.path.join(currentpath,"logs"), "asdad")
checkandcreatefile(logfilepath,"Log File created at "+datetime.now().strftime("%d %b %Y %I:%M:%S %p"))
checkandcreatefile(errorfilepath,"Error File created at "+datetime.now().strftime("%d %b %Y %I:%M:%S %p"))
checkandcreatefile(os.path.join(currentpath,"keypress.txt"),"False")
def errorrlog(*text):
global errorfilepath
with open(errorfilepath,"a+") as errorfile:
for i in text:
errorfile.write(i)
def logging(*text):
global logfilepath
with open(logfilepath,"a+") as logfile:
for i in text:
logfile.write(i)
#LOGGING DETAILS SUCH AS START TIME,TIME ZONE,....
with open(logfilepath,"a+") as logfile:
logging("\n\nStart Time: ",
datetime.now().strftime("%d %b %Y %I:%M:%S %p").rjust(24),
"\n",
"DISTRIBUTION: "+str(distro.linux_distribution()))
def get_pid(name):
return subprocess.check_output(["pidof",name])
try:
count = 0
for i in get_pid('Veronica').decode("utf-8").split():
count += 1
if count == 1:
veronicatts("Found a running instance of Veronica. Please close")
elif count > 1:
veronicatts("Found multiple running instances of Veronica. Please close them")
except:
pass
with open("keypress.txt","w+") as f:
f.write("False")
setproctitle.setproctitle('Veronica')
print("Initialised")
playsound(currentpath+"/audio/start.mp3")
manoeuvre = False
def commands_func(txt):
command_one = getcommand(txt)
global logfilepath
with open(logfilepath,"a+") as logfile:
logging("\n"," "*28+"Result: "+str(command_one),"\n")
if command_one != None:
command_name = command_one[0]
print(command_name)
command_result = eval(command_name)
command_thread = threading.Thread(target=command_result, args=(1,))
command_thread.start()
def keypress():
path_for_f8 = os.path.join(currentpath,"f8press.py")
os.system("sudo -A python3 "+path_for_f8)
def keypress_detect():
while True:
with open("keypress.txt","r") as file:
if file.readline() == "True":
after_detection()
def detector_snowboy():
modelspath = os.path.join(currentpath,"models/")
try:
detector = snowboydecoder.HotwordDetector(modelspath+"veronica.pmdl", sensitivity=0.45, audio_gain=3)
except Exception as e:
print(e)
print("Hotword Engine Not Running")
errorrlog("Hotword Problem","\n",e)
global manoeuvre
manoeuvre = True
detector.start(after_detection)
def after_detection():
speech_text = speechtotext()
global logfilepath
with open(logfilepath,"a+") as logfile:
logging("\n",datetime.now().strftime("%d %b %Y %I:%M:%S %p").rjust(24),"--> Query: "+str(speech_text))
if "open" in speech_text:
app_name,app_percent = get_app(speech_text.replace("open",""))
print(app_name,app_percent)
if app_percent > 80:
print(app_name)
try:
if "https" in app_name or "/home" in app_name:
app_name = "xdg-open "+app_name
subprocess.Popen(app_name.split())
logging("\n"," "*28+"Result: "+"Opened "+str(app_name),"\n")
except Exception as e:
errorfilepath("Error in opening "+app_name)
print("Some error occured in opening app. If found please report at https://github.com/adhitht123/Veronica. ")
print(e)
else:
commands_func(speech_text)
else:
commands_func(speech_text)
global manoeuvre
manoeuvre = False
with open("keypress.txt","w+") as f:
f.write("False")
keypress_thread = threading.Thread(target=keypress)
keypress_thread.start()
keypress_detect_thread = threading.Thread(target=keypress_detect)
keypress_detect_thread.start()
detector_thread = threading.Thread(target=detector_snowboy(), args=(1,))
detector_thread.start()