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

118 lines
3.2 KiB
Python
Raw Normal View History

2022-09-08 13:54:43 +03:00
#-*- coding: utf-8 -*-
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):
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
def jsonLoad(filename):
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}'
def checkSize():
global logs_folder, log_size
i = 0
while i < 2:
try:
2022-12-30 23:13:10 +02:00
log = stat(logs_folder + 'latest.log')
2022-09-08 13:12:25 +03:00
if (log.st_size / 1024) > log_size:
with open(logs_folder + 'latest.log', 'rb') as f_in:
2022-12-30 23:13:10 +02:00
with gzipopen(f'{logs_folder}{datetime.now().strftime("%d.%m.%Y_%H:%M:%S")}.zip', 'wb') as f_out:
copyfileobj(f_in, f_out)
2022-09-08 13:12:25 +03:00
open(logs_folder + 'latest.log', 'w').close()
i = 2
except FileNotFoundError:
try:
log = open(logs_folder + 'latest.log', 'a')
open(logs_folder + 'latest.log', 'a').close()
except:
try:
2022-12-30 23:13:10 +02:00
makedirs(logs_folder, exist_ok=True)
2022-09-08 13:12:25 +03:00
log = open(logs_folder + 'latest.log', 'a')
open(logs_folder + 'latest.log', 'a').close()
except:
pass
i += 1
def appendLog(message):
global logs_folder
checkSize()
try:
log = open(logs_folder + 'latest.log', 'a')
open(logs_folder + 'latest.log', 'a').close()
except:
try:
2022-12-30 23:13:10 +02:00
makedirs(logs_folder, exist_ok=True)
2022-09-08 13:12:25 +03:00
log = open(logs_folder + 'latest.log', 'a')
open(logs_folder + 'latest.log', 'a').close()
except:
2022-12-30 23:13:10 +02:00
sleep(2)
2022-09-08 13:12:25 +03:00
print('Log file could not be created')
return
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')
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)
def userReset(userid, key: str):
database = jsonLoad("data/database.json")
del database[str(userid)][key]
jsonSave("data/database.json", database)
def userGet(userid, key: str):
try:
return jsonLoad("data/database.json")[str(userid)][key]
except KeyError:
return None
except FileNotFoundError:
return None