/
main.py
128 lines (108 loc) · 3.76 KB
/
main.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
from pygame import mixer
import time
import word_detector
import arduino_driver
import party
import answers
import argparse
import random
import json
import logging
from pygame import mixer
import tts
import os
from os.path import isfile, join
import datetime
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s] %(message)s",
handlers=[
logging.FileHandler("logs/oracle.log"),
logging.StreamHandler()
])
bubbles = False
states_statments = json.load(open("states_statments.json","r"))
start_party_states = ['A1.mp3', 'A2.mp3', 'A3.mp3', 'A4.mp3']
start_answer = ['B1.mp3', 'B2.mp3', 'B3.mp3']
now_leave = ['C1.mp3', 'C2.mp3', 'C3.mp3', 'C4.mp3']
pressed_during_answer = ['D1.mp3', 'D2.mp3']
no_question = ['E1.mp3', 'E2.mp3', 'E3.mp3']
asked_recently = ['F1.mp3', 'F2.mp3']
no_actual_question = ['E3.mp3']
answers_dir = '/home/private/oracle/answers_mp3'
answers_files = [f for f in os.listdir(answers_dir) if isfile(join(answers_dir, f))]
def play_answer(answer):
number, text = answer
mp3_file = '%s.mp3' % number
if mp3_file in answers_files:
logging.info('answer mp3 file exist')
party.play_a_file(os.path.join(answers_dir, mp3_file))
else:
tts.say(answer[1])
def play_statement(statement):
logging.info(statement)
full_path = os.path.join('statements', statement)
party.play_a_file(full_path)
all_questions = []
def get_question(ar, wd):
while True:
if not ar.read_button():
time.sleep(0.1)
continue
logging.info('button pushed')
words = wd.detect()
print(words)
if len(words) < 2:
logging.info('question not found')
play_statement(random.choice(no_question))
ar.flush_serial()
continue
if words in all_questions[-5:]:
logging.info('question ask in the last 5 times')
play_statement(random.choice(asked_recently))
ar.flush_serial()
continue
if 4 <= datetime.datetime.now().hour <= 7:
logging.info('push in the night')
tts.say('I will answer you, but you should go to sleep afterward')
all_questions.append(words)
logging.info('question %s' % words)
logging.info("number of questions from the begining of the world is %d" % len(all_questions))
return words
legal_start_words = ['dont', 'do', 'how', 'are', 'should', 'i', 'will', 'does', 'what', 'lets', 'is', 'when', 'where', 'who', 'could', 'can', 'why', 'am']
def grammer_checker(question):
return question.split()[0] in legal_start_words
def main_loop():
wd = word_detector.WordDetector(retries=20)
ar = arduino_driver.ArduinoController()
while True:
logging.info('start loop')
question = get_question(ar, wd)
if not grammer_checker(question):
play_statement(random.choice(no_actual_question))
ar.flush_serial()
continue
play_statement(random.choice(start_party_states))
logging.info("start party")
ar.set_party()
party.play_random_song()
answer = answers.get_answer(question)
logging.info('answer - %s' % str(answer))
ar.flush_serial()
mixer.music.stop()
logging.info('party end')
ar.set_ambient()
play_statement(random.choice(start_answer))
play_answer(answer)
time.sleep(0.4)
play_statement(random.choice(now_leave))
ar.flush_serial()
if __name__ == '__main__':
mixer.init()
#main_loop()
while True:
try:
main_loop()
except Exception as e:
logging.info('problem ' + str(e))
#play_answer(('s', 'You are not funny, stop pretend that you are'))