-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyz.py
More file actions
438 lines (354 loc) · 14.3 KB
/
yz.py
File metadata and controls
438 lines (354 loc) · 14.3 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import CryptoStuffClass
import snowboydecoder
import signal
from urllib.parse import urlencode, quote_plus, unquote
from creds import *
import requests, http.cookiejar
import sys
import speech_recognition as sr
from threading import Thread
import time
import json
from urllib.request import urlretrieve
import shutil
import os
import vlc
import logging
from hashlib import sha1
from subprocess import call
CryptoClass = CryptoStuffClass.CryptoStuff()
#Debug
Debug = True
if Debug:
log_level = logging.DEBUG
else:
log_level = logging.getLevelName('INFO')
# Logger oluşturalım.
logger = logging.getLogger("ProjectArtex")
logger.setLevel(log_level) # Uygulamız şuanda çalışıyor.
# Consol yapısını oluşturduk
ch = logging.StreamHandler()
ch.setLevel(log_level) # Hata ayıklama tipini belirledik.
formatter = logging.Formatter('%(asctime)s %(levelname)s: %(message)s')
# Konsol Formatı
ch.setFormatter(formatter)
# logger için konsol
#logger.addHandler(ch)
# Log kayıt yolunu belirleme
logging.basicConfig(filename='artex.log', filemode='w', level=log_level)
if(UsePins):
from rgbControlClass import RGBControl
if len(sys.argv) == 1:
logger.error("HATA: özel bir model ismi gerekiyor")
logger.info("ÖRNEK KULLANIM: python3 yz.py modeldosyasi.model")
sys.exit(-1)
interrupted = False
def signal_handler(signal, frame):
global interrupted
interrupted = True
# capture SIGINT signal, e.g., Ctrl+C
signal.signal(signal.SIGINT, signal_handler)
def interrupt_callback():
global interrupted
return interrupted
if(UsePins):
led = RGBControl(7, 8, 9)
led.off()
def delete_last_lines(n=1):
for _ in range(n):
sys.stdout.write('\x1b[1A') #CURSOR_UP_ONE
sys.stdout.write('\x1b[2K') #ERASE_LINE
r = sr.Recognizer()
m = sr.Microphone()
#delete_last_lines(100)
#print("....")
#delete_last_lines()
logger.warning("Biraz sessiz kalın, Lütfen...")
with m as source: r.adjust_for_ambient_noise(source)
logger.debug("Minimum threshold enerjisi {} olarak tanımlandı.".format(r.energy_threshold))
dir_path = os.path.dirname(os.path.realpath(__file__))
GlobalLastMessageTime = ''
playlists = set(['pls', 'm3u', 'ash'])
model = sys.argv[1]
detector = snowboydecoder.HotwordDetector(model, sensitivity=0.4)
cookieJar = http.cookiejar.MozillaCookieJar(filename = dir_path + "/cookies.txt")
try:
cookieJar.load()
except FileNotFoundError:
cookieJar.save()
except http.cookiejar.LoadError:
cookieJar.save()
requests.packages.urllib3.disable_warnings()
s = requests.Session()
#s.verify = dir_path + "/certificate.crt"
s.cookies = cookieJar
def Web_Request(URL, Data, WantEncryption):
global s, CryptionKey, cookieJar
out = None
try:
payload = None
if(WantEncryption == True):
payload = urlencode(Data, quote_via=quote_plus)
sifrele = CryptoClass.encrypt(payload, CryptionKey)
payload = {'_yzCryption': sifrele}
else:
payload = Data
req = s.post(URL, data=payload, verify=False)
cookieJar.save()
req.raise_for_status()
if(req.status_code == 200):
out = req.text
if(not out or out == ''):
out = None
except requests.exceptions.Timeout as e:
logger.error("Timeout => " + str(e))
except requests.exceptions.TooManyRedirects as e:
logger.error("TooManyRedirects => " + str(e))
except requests.exceptions.HTTPError as e:
logger.error("HTTPError => " + str(e))
except requests.exceptions.RequestException as e:
logger.error("RequestException => " + str(e))
except Exception:
logger.error("Fatal error in Web_Request", exc_info=True)
finally:
return out
def GetCryptionKey():
try:
global CryptionKey
payload = {'sayfa': 'yz_CryptionKey'}
jsonOutput = Web_Request(BaseUrl + 'main', payload, False)
output = json.loads(jsonOutput)
CryptionKey = CryptoClass.decrypt(output, Salt.encode())
logger.debug(CryptionKey)
return True
except Exception:
logger.error("Fatal error in GetCryptionKey", exc_info=True)
return False
def Login():
if(GetCryptionKey() != True):
logger.error("GetCryptionKey sırasında hata oluştu baştan başlatın!")
return
payload = {'pltfrm': 'orangepi', 'Username': Username, 'Password': Password, 'Remember': 'on'}
jsonOutput = Web_Request(BaseUrl + 'login', payload, True)
logger.debug(jsonOutput)
output = json.loads(jsonOutput)
if (output['type'] == "danger"):
logger.error(output['message'])
sys.exit(1)
elif (output['type'] == "success"):
logger.debug("Giris Islemi Basarili!")
if UsePins: led.magenta()
SendMessage("", False, False)
ThreadCheckNotifications.start()
#burada mesaj gonderme fonksiyonunu calistiracak
#if (ApplicationDeployment.IsNetworkDeployed)
# set_setting("csharp_version", ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString())
elif (output == "yok"):
logger.error("Boyle Bir Kullanici Yok!")
sys.exit(1)
#else:
#logger.debug(output)
if UsePins: led.off()
def SendMessage(Message="", Talking=True, Listening=True):
global BaseUrl
logger.debug("SendMessage Calisti, Gonderilecek Mesaj: '" + Message + "'")
payload = {'msg': Message, 'pltfrm': 'orangepi'}
output = None
while True:
try:
output = Web_Request(BaseUrl + 'message.php', payload, True)
break
except Exception as ex:
time.sleep(0.5)
logger.error(ex)
logger.debug('SendMessage Yanit Geldi -> ' + str(output))
ShowAll(Talking, Listening)
def StartBackground(Status, PythonCode = None):
logger.debug("StartBackground Çalıştı")
if Status == "rebootSystem":
import os
os.system("reboot")
elif Status == "PythonExec":
exec(PythonCode)
elif Status == "rebootYourself":
import os
os.system("systemctl reload Artex.service")
else:
logger.debug("do something")
firstStart = True
def ShowAll(Talking=True, Listening=True):
global BaseUrl, GlobalLastMessageTime, firstStart
logger.debug('ShowAll Calisti')
payload = {'all': '1'}
output = Web_Request(BaseUrl + 'message.php', payload, True)
#logger.debug(output)
logger.debug('ShowAll Yanit Geldi')
if (output and output != None and output != ''):
try:
array = json.loads(output)
datas = array['datas']
Messages = array['messages']
OwnerName = datas['kendi_ismim']
BotName = datas['bot_ismi']
VoiceOpenOff = datas['ses_ac_kapa']
VoiceData = datas['ses_data']
VoiceApi = datas['ses_api']
GlobalLastMessageTime = datas['LastMessageTime']
count, i = len(Messages), 1
for message in Messages:
dt = message['time']
if (i == count):
if ('msj' in message and message['msj'] != None and message['msj'] != ""):
logger.debug(OwnerName + " -> " + message['msj'] + " | " + dt)
if (message['cvp'] != None and message['cvp'] != ""):
logger.debug(BotName + " -> " + message['cvp'] + " | " + dt)
if (message['platform'] == "orangepi"):
logger.debug(message)
PythonCode = None
Status = message['isdurumu']
if ('PythonCode' in message and message['PythonCode'] != None and message['PythonCode'] != ""):
PythonCode = unquote(message['PythonCode'])
logger.debug(PythonCode)
if(firstStart == False):
StartBackground(Status, PythonCode)
i += 1
if(VoiceOpenOff == True and Talking == True):
Talk(VoiceData, VoiceApi)
IsSpeaking(Listening)
firstStart = False
except ValueError:
logger.error("bu bir json değil")
GetCryptionKey()
intance = vlc.Instance()
player = vlc.MediaPlayer()
def Talk(VoiceData, VoiceApi):
try:
global BaseUrl, UsePins, cookieJar, dir_path, player, s
hash_object = sha1(b''+VoiceData.encode('utf-8').strip())
hash_dig = hash_object.hexdigest()
ses_path = dir_path + "/sesler/" + VoiceApi + "/"
if(not os.path.exists(ses_path)):
logger.debug("Ses klasörü bulunamadı, oluşturuluyor..")
os.makedirs(ses_path)
file_path = ses_path + hash_dig + ".mp3"
if(not os.path.isfile(file_path)):
logger.debug("Ses dosyası bulunamadı, indiriliyor..")
req = s.post(BaseUrl + "main?sayfa=ses&ses=" + VoiceData + "&pltfrm=csharp&ses_api=" + VoiceApi, stream=True, verify=False)
cookieJar.save()
with open(file_path, 'wb') as f:
shutil.copyfileobj(req.raw, f)
if UsePins: led.blue()
player.set_media(intance.media_new(file_path))
player.audio_set_volume(100)
player.play()
except Exception as ex:
logger.error(ex)
def IsSpeaking(Listening=True):
global player, UsePins
time.sleep(0.5)
while(player.is_playing() == True):
pass
if(Listening == True):
detect_callback()
if UsePins: led.off()
def CheckNotifications():
while True:
global BaseUrl, GlobalLastMessageTime
time.sleep(1)
#logger.debug('CheckNotifications Calisti')
payload = {'sayfa': 'CheckNotifications'}
try:
output = Web_Request(BaseUrl + 'main', payload, True)
#logger.debug('CheckNotifications Yanit Geldi')
#logger.debug(output)
#if UsePins: led.yellow()
if (output and output != None and output != ''):
try:
Talking = False
Listening = False
jsonObject = json.loads(output)
type = jsonObject['type']
code = jsonObject['code']
data = jsonObject['data']
# Bildirim kontrolü sırasında oturum silinirse tekrardan login olmak için
if (code == 72):
Login()
continue
LastMessageTime = data['LastMessageTime']
if (GlobalLastMessageTime != None and GlobalLastMessageTime != ''):
if (GlobalLastMessageTime != LastMessageTime):
GlobalLastMessageTime = GlobalLastMessageTime;
logger.debug(output)
if ('kind' in data and data['kind'] == 'bildirim'):
Talking = True
if ('WaitForResponse' in data and data['WaitForResponse'] == True):
Listening = True
logger.debug('CheckNotifications gelen yanit > 0 oldugundan ShowAll calistirildi.')
ShowAll(Talking, Listening)
else:
GlobalLastMessageTime = LastMessageTime
except Exception as ex:
logger.error(ex)
GetCryptionKey()
pass
except Exception as e:
logger.error(e)
GetCryptionKey()
pass
def DING():
global UsePins
call(["amixer", "sset", "Lineout volume control", "10%"])
if UsePins: led.cyan()
snowboydecoder.play_audio_file(snowboydecoder.DETECT_DING)
call(["amixer", "sset", "Lineout volume control", "80%"])
def DONG():
global UsePins
call(["amixer", "sset", "Lineout volume control", "10%"])
if UsePins: led.red()
snowboydecoder.play_audio_file(snowboydecoder.DETECT_DONG)
call(["amixer", "sset", "Lineout volume control", "80%"])
def Triggered():
global UsePins
try:
logger.debug("Bir şeyler söyle!")
with m as source: audio = r.listen(source, timeout=4)
if UsePins: led.yellow()
logger.debug("Yakaladım! Şimdi sesi tanımaya çalışıyorum...")
try:
value = r.recognize_google(audio, language="tr-TR")
logger.debug("Minimum threshold enerjisi {} olarak tanımlandı.".format(r.energy_threshold))
if str is bytes:
logger.debug(u"P2Dediğin: {}".format(value).encode("utf-8"))
data = format(value).encode("utf-8")
else:
logger.debug("P3Dediğin: {}".format(value))
data = format(value)
if(data and data != None and data != ''):
if UsePins: led.green()
SendMessage(data, True, True)
except sr.UnknownValueError:
logger.warning("Eyvah! Sesi yakalayamadım!")
DONG()
except sr.RequestError as e:
logger.error("Ah be! Google Ses Tanıma servisinden sonuç isteği yapılamadı; {}".format(e))
DONG()
except sr.WaitTimeoutError:
logger.warning("Zaman Aşımı Gerçekleşti")
DONG()
if UsePins: led.off()
def detect_callback():
delete_last_lines()
logger.debug("...")
delete_last_lines()
#TriggeredThread = Thread(target = Triggered)
#TriggeredThread.start()
DINGThread = Thread(target = DING)
DINGThread.start()
Triggered()
if __name__ == "__main__":
ThreadCheckNotifications = Thread(target = CheckNotifications)
ThreadCheckNotifications.setDaemon(True)
Login()
logger.debug('Artex Sözcüğü Dinleniyor... Çıkış için Ctrl+C basın')
detector.start(detected_callback=detect_callback, interrupt_check=interrupt_callback, sleep_time=0.03)
detector.terminate()