2023-03-18 01:52:15 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2022-09-08 13:54:43 +03:00
|
|
|
|
2022-12-30 23:13:10 +02:00
|
|
|
from os import makedirs, stat
|
|
|
|
from gzip import open as gzipopen
|
|
|
|
from shutil import copyfileobj
|
|
|
|
from time import sleep
|
|
|
|
from ujson import loads, dumps
|
2022-09-08 13:12:25 +03:00
|
|
|
from modules.colors import *
|
|
|
|
from datetime import datetime
|
|
|
|
|
2022-12-30 23:13:10 +02:00
|
|
|
users_path = "users/"
|
|
|
|
logs_folder = "logs/"
|
2022-09-08 13:12:25 +03:00
|
|
|
|
|
|
|
|
|
|
|
def jsonSave(filename, value):
|
2023-03-18 01:52:15 +02:00
|
|
|
with open(filename, "w", encoding="utf-8") as f:
|
2022-12-30 23:13:10 +02:00
|
|
|
f.write(dumps(value, indent=4, ensure_ascii=False))
|
2022-09-08 13:12:25 +03:00
|
|
|
|
2023-03-18 01:52:15 +02:00
|
|
|
|
2022-09-08 13:12:25 +03:00
|
|
|
def jsonLoad(filename):
|
2023-03-18 01:52:15 +02:00
|
|
|
with open(filename, "r", encoding="utf-8") as f:
|
2022-12-30 23:13:10 +02:00
|
|
|
value = loads(f.read())
|
2022-09-08 13:12:25 +03:00
|
|
|
return value
|
|
|
|
|
|
|
|
|
2022-12-30 23:13:10 +02:00
|
|
|
config = jsonLoad(f"config.json")
|
2022-09-08 13:12:25 +03:00
|
|
|
|
|
|
|
log_size = config["log_size"]
|
|
|
|
owner_id = config["owner_id"]
|
|
|
|
|
|
|
|
|
|
|
|
def nowtime():
|
|
|
|
return f'{BBLACK}[{CYAN}{datetime.now().strftime("%H:%M:%S")}{BBLACK}]{RESET}'
|
|
|
|
|
2023-03-18 01:52:15 +02:00
|
|
|
|
2022-09-08 13:12:25 +03:00
|
|
|
def checkSize():
|
|
|
|
global logs_folder, log_size
|
2023-03-18 01:52:15 +02:00
|
|
|
|
2022-09-08 13:12:25 +03:00
|
|
|
i = 0
|
|
|
|
|
|
|
|
while i < 2:
|
|
|
|
try:
|
2023-03-18 01:52:15 +02:00
|
|
|
log = stat(logs_folder + "latest.log")
|
2022-09-08 13:12:25 +03:00
|
|
|
|
|
|
|
if (log.st_size / 1024) > log_size:
|
2023-03-18 01:52:15 +02:00
|
|
|
with open(logs_folder + "latest.log", "rb") as f_in:
|
|
|
|
with gzipopen(
|
|
|
|
f'{logs_folder}{datetime.now().strftime("%d.%m.%Y_%H:%M:%S")}.zip',
|
|
|
|
"wb",
|
|
|
|
) as f_out:
|
2022-12-30 23:13:10 +02:00
|
|
|
copyfileobj(f_in, f_out)
|
2023-03-18 01:52:15 +02:00
|
|
|
|
|
|
|
open(logs_folder + "latest.log", "w").close()
|
|
|
|
|
2022-09-08 13:12:25 +03:00
|
|
|
i = 2
|
|
|
|
|
|
|
|
except FileNotFoundError:
|
|
|
|
try:
|
2023-03-18 01:52:15 +02:00
|
|
|
log = open(logs_folder + "latest.log", "a")
|
|
|
|
open(logs_folder + "latest.log", "a").close()
|
2022-09-08 13:12:25 +03:00
|
|
|
except:
|
|
|
|
try:
|
2022-12-30 23:13:10 +02:00
|
|
|
makedirs(logs_folder, exist_ok=True)
|
2023-03-18 01:52:15 +02:00
|
|
|
log = open(logs_folder + "latest.log", "a")
|
|
|
|
open(logs_folder + "latest.log", "a").close()
|
2022-09-08 13:12:25 +03:00
|
|
|
except:
|
|
|
|
pass
|
2023-03-18 01:52:15 +02:00
|
|
|
|
2022-09-08 13:12:25 +03:00
|
|
|
i += 1
|
|
|
|
|
2023-03-18 01:52:15 +02:00
|
|
|
|
2022-09-08 13:12:25 +03:00
|
|
|
def appendLog(message):
|
|
|
|
global logs_folder
|
|
|
|
|
|
|
|
checkSize()
|
|
|
|
|
|
|
|
try:
|
2023-03-18 01:52:15 +02:00
|
|
|
log = open(logs_folder + "latest.log", "a")
|
|
|
|
open(logs_folder + "latest.log", "a").close()
|
2022-09-08 13:12:25 +03:00
|
|
|
except:
|
|
|
|
try:
|
2022-12-30 23:13:10 +02:00
|
|
|
makedirs(logs_folder, exist_ok=True)
|
2023-03-18 01:52:15 +02:00
|
|
|
log = open(logs_folder + "latest.log", "a")
|
|
|
|
open(logs_folder + "latest.log", "a").close()
|
2022-09-08 13:12:25 +03:00
|
|
|
except:
|
2022-12-30 23:13:10 +02:00
|
|
|
sleep(2)
|
2023-03-18 01:52:15 +02:00
|
|
|
print("Log file could not be created")
|
2022-09-08 13:12:25 +03:00
|
|
|
return
|
2023-03-18 01:52:15 +02:00
|
|
|
|
2022-12-30 23:13:10 +02:00
|
|
|
print(message, flush=True)
|
2022-09-08 13:12:25 +03:00
|
|
|
log.write(f'[{datetime.now().strftime("%H:%M:%S | %d.%m.%Y")}] {message}\n')
|
2023-03-18 01:52:15 +02:00
|
|
|
|
2022-09-08 13:12:25 +03:00
|
|
|
log.close()
|
|
|
|
|
|
|
|
|
|
|
|
def string(key: str, *args: str, userlocale="uk"):
|
|
|
|
locales = jsonLoad("strings.json")
|
|
|
|
strings = locales[userlocale]
|
|
|
|
string = strings
|
|
|
|
for dict_key in args:
|
|
|
|
string = string[dict_key]
|
|
|
|
return string[key]
|
|
|
|
|
|
|
|
|
|
|
|
def userSet(userid, key: str, value):
|
|
|
|
database = jsonLoad("data/database.json")
|
|
|
|
if str(userid) not in database:
|
|
|
|
database[str(userid)] = {}
|
|
|
|
database[str(userid)][key] = value
|
|
|
|
jsonSave("data/database.json", database)
|
|
|
|
|
2023-03-18 01:52:15 +02:00
|
|
|
|
2022-09-08 13:12:25 +03:00
|
|
|
def userReset(userid, key: str):
|
|
|
|
database = jsonLoad("data/database.json")
|
|
|
|
del database[str(userid)][key]
|
|
|
|
jsonSave("data/database.json", database)
|
|
|
|
|
2023-03-18 01:52:15 +02:00
|
|
|
|
2022-09-08 13:12:25 +03:00
|
|
|
def userGet(userid, key: str):
|
|
|
|
try:
|
|
|
|
return jsonLoad("data/database.json")[str(userid)][key]
|
|
|
|
except KeyError:
|
|
|
|
return None
|
|
|
|
except FileNotFoundError:
|
2023-03-18 01:52:15 +02:00
|
|
|
return None
|