1
0
mirror of https://github.com/MarkParker5/STARK.git synced 2024-11-24 08:12:13 +02:00

infinite pool for telebot, sleep mode for voice assistant

This commit is contained in:
MarkParker5 2021-01-04 20:00:32 +02:00
parent 88c735a688
commit d4cf2babcd
4 changed files with 65 additions and 70 deletions

View File

@ -43,8 +43,9 @@ def getSerial(url):
seasons[current_season] = {}
continue
spans = elem.find_all('span')
if not spans: break
audio = spans[2].text
if 'Оригинальная дорожка' in audio or 'Полное дублирование' in audio or 'LostFilm' in audio:
if audio in ['Оригинальная дорожка', 'Полное дублирование', 'LostFilm']:
num = int(spans[0].text.split(' ')[0])-1
seasons[current_season][num] = {'href': elem.ul.li.a['href'], 'title': f'{current_season+1} сезон {num+1} серия'}

27
main.py
View File

@ -1,8 +1,6 @@
#!/usr/local/bin/python3.8
import os
import config
import RPi.GPIO as GPIO
import time
modules = {
'Voice Assistant': 'voice_assistant',
@ -15,28 +13,3 @@ for name, module in modules.items():
os.system(f'lxterminal --command="python3.8 {config.path}/{module}.py"')
except:
print(f'[error]\t{name} launch failed')
def launch_vc():
try:
print('Voice Assistant activation...')
os.system(f'lxterminal --command="python3.8 {config.path}/voice_assistant.py"')
except:
print(f'[error]\tlaunch failed')
if config.double_clap_activation:
lastClapTime = 0
GPIO.setmode(GPIO.BCM)
GPIO.setup(12, GPIO.IN)
def callback(channel):
global lastClapTime
now = time.time()
delta = now - lastClapTime
if 0.1 < delta < 0.6:
lauch_vc()
else:
lastClapTime = now
GPIO.add_event_detect(12, GPIO.RISING, callback=callback)
while True: time.sleep(1)

View File

@ -66,8 +66,8 @@ def execute(msg):
while True:
#try:
try:
print("Start polling...")
bot.polling(callback = check_threads, args = (threads,) )
#except:
except:
print("Polling failed")

View File

@ -9,10 +9,19 @@ listener = SpeechRecognition.SpeechToText()
voice = Text2Speech.Engine()
threads = []
memory = []
online = True
voids = 0
listener.listen_noise()
if config.double_clap_activation:
import RPi.GPIO as GPIO
import time
lastClapTime = 0
doubleClap = False
GPIO.setmode(GPIO.BCM)
GPIO.setup(12, GPIO.IN)
GPIO.add_event_detect(12, GPIO.RISING, callback=checkClap)
def reply(responce):
if responce['text']: # print answer
print('Archie: '+responce['text'])
@ -29,6 +38,25 @@ def check_threads():
thread['finish_event'].clear()
del thread
# check double clap from arduino microphone module
def checkClap(channel):
global lastClapTime
now = time.time()
delta = now - lastClapTime
if 0.1 < delta < 0.6:
doubleClap = True
else:
lastClapTime = now
# waiting for double clap
def sleep():
global doubleClap
while not doubleClap:
check_threads()
time.sleep(1)
else:
doubleClap = False
while True: # main loop
check_threads()
print('\nYou: ', end='')
@ -38,44 +66,37 @@ while True: # main loop
if speech['status'] == 'ok':
print(text)
voids = 0
# set online add clean input if name in text
if name := list(set(config.names) & set(text.split(' '))):
online = True
text = text.replace(name[0], '').strip()
# recognize and execute command
if online:
# repeat last answer
if Command.isRepeat(text):
reply(memory[0]['responce']);
continue
# recognize command with context
try:
cmd, params = memory[0]['cmd'].checkContext(text).values()
if memory[0].get('params'): params = {**memory[0].get('params'), **params}
except:
cmd, params = Command.reg_find(text).values()
# execute command
responce = cmd.start(params)
# say answer
reply(responce)
# waiting answer on question
if responce['type'] == 'question':
print('\nYou: ', end='')
speech = listener.listen()
if speech['status'] == 'ok':
text = speech['text']
print(text)
if responce := responce['callback'].answer(text): reply(responce)
# remember the command
memory.insert(0, {
'text': text,
'cmd': cmd,
'responce': responce,
})
# repeat last answer
if Command.isRepeat(text):
reply(memory[0]['responce']);
continue
# recognize command with context
try:
cmd, params = memory[0]['cmd'].checkContext(text).values()
if memory[0].get('params'): params = {**memory[0].get('params'), **params}
except:
cmd, params = Command.reg_find(text).values()
# execute command
responce = cmd.start(params)
# say answer
reply(responce)
# waiting answer on question
if responce['type'] == 'question':
print('\nYou: ', end='')
speech = listener.listen()
if speech['status'] == 'ok':
text = speech['text']
print(text)
if responce := responce['callback'].answer(text): reply(responce)
# remember the command
memory.insert(0, {
'text': text,
'cmd': cmd,
'responce': responce,
})
else:
if speech['status'] == 'error': print('Отсутсвует подключение к интернету');
elif online and speech['status'] == 'void': voids += 1;
if online and voids >= 3: online = False; voids = 0
if not online:
if config.double_clap_activation: break
else: listener.listen_noise()
elif speech['status'] == 'void': voids += 1;
if voids >= 3:
voids = 0
if config.double_clap_activation: sleep()