This repository has been archived on 2024-10-14. You can view files and clone it, but cannot push or open issues or pull requests.
AutoZoom/functions.py

436 lines
14 KiB
Python
Raw Permalink Normal View History

# -*- coding: utf-8 -*-
import pip
2021-04-24 03:08:43 +03:00
import time
import json
import os
2021-04-24 03:08:43 +03:00
import shutil
import gzip
2021-04-30 03:25:36 +03:00
import getpass
2021-04-24 03:08:43 +03:00
from datetime import datetime
from pathlib import Path
2021-04-30 03:25:36 +03:00
from subprocess import check_output
path = Path(__file__).resolve().parent
sounds_folder = str(Path(str(path)+"/sounds/")) + os.sep
files_folder = str(Path(str(path)+"/files/")) + os.sep
2021-04-24 03:08:43 +03:00
logs_folder = str(Path(str(path)+"/logs/")) + os.sep
2021-04-30 03:25:36 +03:00
yes_list = ['y', 'yes', 'д', 'да']
no_list = ['n', 'no', 'н', 'нет']
2021-04-24 03:08:43 +03:00
default_config = {
"firstboot": True,
"debug": False,
"shutdown_timeout": 30,
"shutdown_enabled": False,
"start": "shift+f7",
"stop": "shift+f8",
"telegram_enabled": False,
"use_colors": True,
"run_fullscreen": False,
"rpc_use": True,
"rpc_id": "800049969960058882",
"sounds": True,
"remove_old": True,
"end_mode": "shutdown",
"obs_exe": None,
"obs_core": None,
"obs_delay": 10,
"update_check": True,
"write_logs": True,
"log_size": 512,
"sound_ended": "ended",
"sound_recordstart": "recordstart",
"sound_recordstop": "recordstop",
"sound_shutdown": "shutdown",
"sound_started": "started",
"sound_warning": "warning"
}
2021-04-24 03:08:43 +03:00
2021-04-30 03:25:36 +03:00
# Функция возвращающая надпись Windows Only
def winOnly(color, reset, system, start='', end=''):
if system != 'windows':
return f"{start}{color}Только для Windows!{reset}{end}"
else:
return ""
# Функция возвращающая тип ОС
def getOS():
if os.name == 'nt':
return "windows"
elif 'android' in str(check_output('uname -a', shell=True).lower()):
return "android"
else:
return "unix"
# Функция отвечает за очищение командной строки
if getOS() == "windows":
clear = lambda: os.system('cls')
else:
clear = lambda: os.system('clear')
2021-05-08 01:28:41 +03:00
# Импортирование игралки звуков
2021-11-03 09:20:15 +02:00
try:
if getOS() == "windows":
import winsound
from playsound import playsound
elif getOS() == "unix":
from playsound import playsound
except:
pass
2021-05-08 01:28:41 +03:00
2021-04-30 03:25:36 +03:00
# Установка заголовка окна cmd.exe
def setTitle(title, system):
if system == "windows":
try:
os.system(f"title {title}")
except:
pass
# Получить номер дня недели
def getDayNum(day):
output = datetime.strptime(day, "%d.%m.%Y").isoweekday()
return output
2021-04-24 03:08:43 +03:00
# Функция проверки размера файла
def checkSize():
global logs_folder
i = 0
while i < 2:
try:
log = os.stat(logs_folder + 'latest.log')
if (log.st_size / 1024) > getConfig("log_size"):
with open(logs_folder + 'latest.log', 'rb') as f_in:
with gzip.open(f'{logs_folder}{datetime.now().strftime("%d.%m.%Y_%H:%M:%S")}.zip', 'wb') as f_out:
shutil.copyfileobj(f_in, f_out)
if getConfig("debug"):
print(f'Copied {logs_folder}{datetime.now().strftime("%d.%m.%Y_%H:%M:%S")}.zip')
open(logs_folder + 'latest.log', 'w').close()
i = 2
except FileNotFoundError:
if getConfig("debug"):
print('Log file not found')
time.sleep(2)
try:
log = open(logs_folder + 'latest.log', 'a')
open(logs_folder + 'latest.log', 'a').close()
except:
try:
os.mkdir(logs_folder)
log = open(logs_folder + 'latest.log', 'a')
open(logs_folder + 'latest.log', 'a').close()
except:
if getConfig("debug"):
time.sleep(2)
print('Log file could not be created')
i += 1
# Функция добавления в лог
2021-04-30 03:25:36 +03:00
def appendLog(message, startup=False, shutdown=False):
2021-04-24 03:08:43 +03:00
if getConfig("write_logs"):
global logs_folder
checkSize()
try:
log = open(logs_folder + 'latest.log', 'a')
open(logs_folder + 'latest.log', 'a').close()
except:
try:
os.mkdir(logs_folder)
log = open(logs_folder + 'latest.log', 'a')
open(logs_folder + 'latest.log', 'a').close()
except:
time.sleep(2)
print('Log file could not be created')
2021-04-30 03:25:36 +03:00
if startup:
log.write(f'[{datetime.now().strftime("%H:%M:%S | %d.%m.%Y")}] [STARTUP] {message}\n')
elif shutdown:
log.write(f'[{datetime.now().strftime("%H:%M:%S | %d.%m.%Y")}] [SHUTDOWN] {message}\n')
else:
log.write(f'[{datetime.now().strftime("%H:%M:%S | %d.%m.%Y")}] {message}\n')
2021-04-24 03:08:43 +03:00
log.close()
2021-04-30 03:25:36 +03:00
# Функция проигрывания звука
def playSound(soundname, timing=''):
global sysname
if getConfig("sounds"):
if getOS() == "windows":
try:
winsound.PlaySound(sounds_folder+soundname+".wav", winsound.SND_FILENAME)
except Exception as exp:
appendLog(f'Could not play winsound: {exp}')
if getConfig("debug"):
print(f'{timing} Не удалось проиграть winsound звук "{soundname}" (Ошибка: {exp})')
try:
playsound(sounds_folder+soundname+".wav")
except Exception as exp:
appendLog(f'Could not play playsound: {exp}')
if getConfig("debug"):
print(f'{timing} Не удалось проиграть playsound звук "{soundname}" (Ошибка: {exp})')
2021-08-01 11:14:33 +03:00
2021-04-30 03:25:36 +03:00
elif getOS() == "android":
try:
os.system(f'play-audio {sounds_folder}{soundname}.wav')
except Exception as exp:
appendLog(f'Could not play play-audio: {exp}')
2021-08-01 11:14:33 +03:00
2021-04-30 03:25:36 +03:00
else:
try:
playsound(sounds_folder+soundname+".wav")
except Exception as exp:
appendLog(f'Could not play playsound: {exp}')
if getConfig("debug"):
print(f'{timing} Не удалось проиграть playsound звук "{soundname}" (Ошибка: {exp})')
2021-08-01 11:14:33 +03:00
# Функция удаления ненужного мусора из строки
def strCleaner(string):
output = string.replace('"', '\"').replace('\n', '')
appendLog(f"String cleaned: {output}")
return output
2021-04-24 03:08:43 +03:00
# Функция добавления переменных, если их нет
def repairConfig(some_dic):
2021-04-30 03:25:36 +03:00
2021-04-24 03:08:43 +03:00
global files_folder
global default_config
2021-04-30 03:25:36 +03:00
for key in default_config:
2021-04-24 03:08:43 +03:00
try:
some_dic[key]
2021-04-30 03:25:36 +03:00
except KeyError:
some_dic[key] = default_config[key]
2021-04-24 03:08:43 +03:00
saveJson(files_folder+'config.json', some_dic)
2021-04-30 03:25:36 +03:00
# Функция изменения переменной конфигурации
def setConfig(some_var, some_val):
global files_folder
global default_config
if os.path.exists(files_folder):
if not os.path.exists(files_folder+'config.json'):
temp_config_list = default_config
temp_config_list[some_var] = some_val
saveJson(files_folder+'config.json', temp_config_list)
else:
try:
with open(f"{files_folder}config.json", encoding="utf-8") as json_file:
config_list = json.load(json_file)
json_file.close()
try:
config_list[some_var] = some_val
saveJson(files_folder+'config.json', config_list)
appendLog(f'Changed variable "{somevar}" to {some_val}')
except:
try:
repairConfig(config_list)
config_list = json.load(json_file)
json_file.close()
config_list[some_var] = some_val
saveJson(files_folder+'config.json', config_list)
appendLog(f'Changed variable "{somevar}" to {some_val}')
except:
pass
except:
return "Error"
else:
os.mkdir(files_folder)
if not os.path.exists(files_folder+'config.json'):
temp_config_list = default_config
temp_config_list[some_var] = some_val
saveJson(files_folder+'config.json', temp_config_list)
else:
try:
with open(f"{files_folder}config.json", encoding="utf-8") as json_file:
config_list = json.load(json_file)
json_file.close()
try:
config_list[some_var] = some_val
saveJson(files_folder+'config.json', config_list)
except:
try:
repairConfig(config_list)
config_list = json.load(json_file)
json_file.close()
config_list[some_var] = some_val
saveJson(files_folder+'config.json', config_list)
appendLog(f'Changed variable "{somevar}" to {some_val}')
except:
config_list[some_var] = some_val
saveJson(files_folder+'config.json', config_list)
appendLog(f'Changed variable "{somevar}" to {some_val}')
2021-08-01 11:14:33 +03:00
2021-04-30 03:25:36 +03:00
except:
return "Error"
# Функция получения переменной конфигурации
def getConfig(some_var):
2021-04-30 03:25:36 +03:00
global files_folder
2021-04-24 03:08:43 +03:00
global default_config
if os.path.exists(files_folder):
2021-04-30 03:25:36 +03:00
if not os.path.exists(files_folder+'config.json'):
2021-04-24 03:08:43 +03:00
temp_config_list = default_config
saveJson(files_folder+'config.json', temp_config_list)
return temp_config_list[some_var]
2021-04-30 03:25:36 +03:00
else:
try:
with open(f"{files_folder}config.json", encoding="utf-8") as json_file:
2021-04-30 03:25:36 +03:00
config_list = json.load(json_file)
json_file.close()
2021-04-24 03:08:43 +03:00
try:
return config_list[some_var]
2021-04-30 03:25:36 +03:00
2021-04-24 03:08:43 +03:00
except:
try:
2021-11-03 09:20:15 +02:00
try:
setConfig(some_var, default_config[some_var])
return default_config[some_var]
except:
repairConfig(config_list)
config_list = json.load(json_file)
json_file.close()
return config_list[some_var]
2021-04-24 03:08:43 +03:00
except:
return default_config[some_var]
except:
return "Error"
else:
os.mkdir(files_folder)
if not os.path.exists(files_folder+'config.json'):
2021-04-24 03:08:43 +03:00
temp_config_list = default_config
saveJson(files_folder+'config.json', temp_config_list)
return temp_config_list[some_var]
else:
2021-04-30 03:25:36 +03:00
try:
with open(f"{files_folder}config.json", encoding="utf-8") as json_file:
config_list = json.load(json_file)
json_file.close()
2021-04-24 03:08:43 +03:00
try:
return config_list[some_var]
2021-04-30 03:25:36 +03:00
2021-04-24 03:08:43 +03:00
except:
try:
repairConfig(config_list)
config_list = json.load(json_file)
json_file.close()
return config_list[some_var]
2021-04-30 03:25:36 +03:00
except:
return default_config[some_var]
except:
return "Error"
2021-04-30 03:25:36 +03:00
# Получить статус процесса
def getState(process="CptHost.exe"):
if getOS() == 'windows':
try:
output = os.popen(f'tasklist /fi "IMAGENAME eq {process}" /fi "USERNAME ne NT AUTHORITY\{getpass.getuser()}"').read()
if process in output:
return True
else:
return False
2021-08-01 11:14:33 +03:00
2021-04-30 03:25:36 +03:00
except Exception as exp:
appendLog(f'Failed to get state using tasklist: {exp}')
output = os.popen('wmic process get description, processid').read()
if "CptHost.exe" in output:
return True
else:
return False
# Функция сохранения информации в json файл
def saveJson(filename, value):
with open(filename, 'w', encoding="utf-8") as f:
json.dump(value, f, indent=4, ensure_ascii=False)
f.close()