-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
155 lines (128 loc) · 5.09 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
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
import pyttsx3
import speech_recognition as sr
import pyautogui
import random
from fuzzywuzzy import fuzz
from bs4 import BeautifulSoup
from config import translate_key
import requests as req
# func to talk
def speak(text):
print(text)
jack_voice.say(text)
jack_voice.runAndWait()
jack_voice.stop()
# check with fuzzywuzzy
def recognize_cmd(task):
RC = {'cmd': '', 'percent': 0}
for c, v in main_dict['cmds'].items():
for x in v:
vrt = fuzz.ratio(task, x)
if vrt > RC['percent']:
RC['cmd'] = c
RC['percent'] = vrt
return RC['cmd']
# listening
def command():
jack_heards = sr.Recognizer()
# listen micro
with sr.Microphone() as source:
# pause in 1 second
jack_heards.pause_threshold = 1
# clean extra noise
print("Розпізнавати...")
jack_heards.adjust_for_ambient_noise(source, duration=1)
# rec
audio = jack_heards.listen(source)
try:
task = jack_heards.recognize_google(audio, language="ru-RU").lower()
print(task)
except sr.UnknownValueError:
speak(random.choice(main_dict['wrong_rec']))
task = command()
return task
# func for tranlate russian text on uk languege
def translate_on_uk(text):
params_translate_request = {
"key": translate_key, # translate key api from config
"text": text,
"lang": 'ru-uk' # language from ru on uk
}
URL = "https://translate.yandex.net/api/v1.5/tr.json/translate"
response = req.get(URL, params=params_translate_request)
return response.json()['text'][0]
'''
Func for parcer page with last 20 request in yandex
'''
def yandex_requst_log_cather():
try:
page = req.get("https://export.yandex.ru/last/last20x.xml")
except:
speak('Сайт впав і помер, жарти не буде')
return
soup = BeautifulSoup(page.text, 'lxml')
line = soup.find('item')
if line.text != None:
speak('один шкіряний мішок шукає ' + translate_on_uk(line.text))
else:
line = soup.find('item')
speak('один шкіряний мішок шукає ' + translate_on_uk(line.text))
# all command
def makeSomething(task):
if any(i in task for i in main_dict["names"]):
for x in main_dict['names']:
task = task.replace(x, "").strip()
task = recognize_cmd(task)
if task == 'yandex_song_next':
speak(random.choice(main_dict['accomp']))
pyautogui.keyDown('ctrl')
pyautogui.press('l')
pyautogui.keyUp('ctrl')
elif task == 'yandex_song_back':
speak(random.choice(main_dict['accomp']))
pyautogui.keyDown('ctrl')
pyautogui.press('k')
pyautogui.keyUp('ctrl')
elif task == 'yandex_song_pause':
speak(random.choice(main_dict['accomp']))
pyautogui.keyDown('ctrl')
pyautogui.press('p')
pyautogui.keyUp('ctrl')
elif task == 'yandex_request_log':
speak(random.choice(main_dict['accomp']))
yandex_requst_log_cather()
else:
speak(random.choice(main_dict['wrong_rec']))
if __name__ == '__main__':
# dictionarys
main_dict = {
# names of assistent
"names": ('джек', 'джеки'),
# Words that should be deleted
"removed": ('песню', 'поставь', 'что', 'яндекс', 'яндексе', 'люди'),
# commands
"cmds": {
"yandex_song_next": ('переключи', 'следующую'),
"yandex_song_back": ('верни', 'перемотай', 'назад'),
"yandex_song_pause": ('останови', 'включи музыку', 'паузу', 'пауза'),
'yandex_request_log': ('гуглят', 'ищут')
},
"accomp": (
'виконувавши', 'одну секундочку', 'вже виконую', 'зараз', 'так, звичайно', 'буде зроблено', 'знову робота'),
"wrong_rec": ('Ти можеш сказати це з виразом, в одному тоні', 'Ну це просто пісна хуйня якась, блядь',
'Давай по новій, Міша, все хуйня.', 'нічого не зрозумів, але дуже цікаво',
'не зрозумію, що ти мовчиш',
'а тепер давай розберемо по частинах тобою сказане',
'якщо ти щось говорив, то я ніхуя не зрозумів')
}
jack_voice = pyttsx3.init()
# set ukraine voice to jack
all_voice = jack_voice.getProperty('voices')
jack_voice.setProperty('voice', "uk")
for voice in all_voice:
if voice.name == 'Anatol':
jack_voice.setProperty('voice', voice.id)
speak("Вітаю тебе, дурник")
speak("Джек слухає")
while True:
makeSomething(command())