TelegramPoster/modules/utils.py

253 lines
7.2 KiB
Python
Raw Normal View History

2023-03-20 13:03:03 +02:00
from os import kill, makedirs
2022-09-18 19:27:13 +03:00
from os import name as osname
2023-03-20 13:03:03 +02:00
from os import path, sep
2023-03-12 15:54:06 +02:00
from sys import exit
2022-09-18 19:27:13 +03:00
from traceback import print_exc
from typing import Any
2023-03-20 13:03:03 +02:00
from zipfile import ZipFile
2022-08-11 12:27:17 +03:00
2023-03-20 13:03:03 +02:00
import aiofiles
2023-03-12 15:54:06 +02:00
from ujson import JSONDecodeError, dumps, loads
2023-01-17 12:43:10 +02:00
from modules.logger import logWrite
2022-08-08 15:53:26 +03:00
2023-03-17 14:52:16 +02:00
default_config = {
"locale": "en",
"locale_log": "en",
"locale_fallback": "en",
"owner": 0,
"admins": [],
"bot": {"api_id": 0, "api_hash": "", "bot_token": ""},
"database": {
"user": None,
"password": None,
"host": "127.0.0.1",
"port": 27017,
"name": "tgposter",
},
"mode": {"post": True, "submit": True},
"reports": {"sent": False, "error": True, "startup": True, "shutdown": True},
"logging": {"size": 512, "location": "logs"},
"locations": {
"tmp": "tmp",
"data": "data",
"cache": "cache",
"sent": "data/sent",
"queue": "data/queue",
"index": "data/index.json",
"locale": "locale",
},
"posting": {
"channel": 0,
"silent": False,
"move_sent": False,
"use_interval": False,
"interval": "1h30m",
"page_size": 300,
"submitted_caption": {
"enabled": True,
"ignore_admins": True,
"text": "#submitted",
},
"extensions": {
"photo": ["jpg", "png", "gif", "jpeg"],
"video": ["mp4", "avi", "mkv", "webm", "mov"],
},
"time": [
"08:00",
"10:00",
"12:00",
"14:00",
"16:00",
"18:00",
"20:00",
"22:00",
],
"api": {
"address": "http://localhost:8054",
"address_external": "https://photos.domain.com",
"username": "",
"password": "",
"album": "",
},
},
"caption": {"enabled": False, "link": None, "text": ["sample text"]},
"submission": {
"timeout": 30,
"file_size": 15728640,
"tmp_size": 15728640,
"allow_duplicates": False,
"send_uploaded_id": False,
"require_confirmation": {"users": True, "admins": True},
"mime_types": [
"image/png",
"image/gif",
"image/jpeg",
"video/mp4",
"video/quicktime",
],
},
"commands": ["start", "rules"],
2023-03-20 14:56:18 +02:00
"commands_admin": ["import", "export", "shutdown"],
2023-03-17 14:52:16 +02:00
}
2023-03-09 12:33:02 +02:00
def jsonLoad(filename: str) -> Any:
2022-08-08 15:53:26 +03:00
"""Loads arg1 as json and returns its contents"""
2023-03-09 12:33:02 +02:00
with open(filename, "r", encoding="utf8") as file:
2022-08-11 12:27:17 +03:00
try:
2022-08-31 15:31:24 +03:00
output = loads(file.read())
2022-08-11 12:27:17 +03:00
except JSONDecodeError:
2023-03-09 12:33:02 +02:00
logWrite(
f"Could not load json file {filename}: file seems to be incorrect!\n{print_exc()}"
)
2022-08-11 21:39:03 +03:00
raise
2022-08-11 12:27:17 +03:00
except FileNotFoundError:
2023-03-09 12:33:02 +02:00
logWrite(
f"Could not load json file {filename}: file does not seem to exist!\n{print_exc()}"
)
2022-08-11 21:39:03 +03:00
raise
2022-08-08 15:53:26 +03:00
file.close()
return output
2023-03-09 12:33:02 +02:00
def jsonSave(contents: Any, filename: str) -> None:
2022-08-08 15:53:26 +03:00
"""Dumps dict/list arg1 to file arg2"""
2022-08-11 12:27:17 +03:00
try:
2023-03-09 12:33:02 +02:00
with open(filename, "w", encoding="utf8") as file:
2023-03-12 23:14:41 +02:00
file.write(
dumps(
contents, ensure_ascii=False, indent=4, escape_forward_slashes=False
)
)
2022-08-11 12:27:17 +03:00
file.close()
except Exception as exp:
2022-09-18 19:27:13 +03:00
logWrite(f"Could not save json file {filename}: {exp}\n{print_exc()}")
2022-08-08 15:53:26 +03:00
return
def configSet(key: str, value, *args: str):
"""Set key to a value
Args:
* key (str): The last key of the keys path.
* value (str/int/float/list/dict/None): Some needed value.
* *args (str): Path to key like: dict[args][key].
2023-03-09 12:33:02 +02:00
"""
2022-08-08 15:53:26 +03:00
this_dict = jsonLoad("config.json")
string = "this_dict"
for arg in args:
string += f'["{arg}"]'
if type(value) in [str]:
string += f'["{key}"] = "{value}"'
else:
string += f'["{key}"] = {value}'
exec(string)
jsonSave(this_dict, "config.json")
return
2023-03-09 12:33:02 +02:00
2022-08-08 15:53:26 +03:00
def configGet(key: str, *args: str):
"""Get value of the config key
Args:
* key (str): The last key of the keys path.
* *args (str): Path to key like: dict[args][key].
Returns:
* any: Value of provided key
2023-03-09 12:33:02 +02:00
"""
2022-08-08 15:53:26 +03:00
this_dict = jsonLoad("config.json")
try:
this_key = this_dict
for dict_key in args:
this_key = this_key[dict_key]
2023-03-17 14:52:16 +02:00
this_key[key]
except KeyError:
print(
f"Could not find config key '{key}' under path {args}: falling back to default config",
flush=True,
)
2023-03-17 14:52:16 +02:00
this_key = default_config
for dict_key in args:
this_key = this_key[dict_key]
2023-03-17 14:52:16 +02:00
configSet(key, this_key[key], *args)
2022-08-10 14:08:15 +03:00
return this_key[key]
2023-03-09 12:33:02 +02:00
2022-08-11 21:39:03 +03:00
def locale(key: str, *args: str, locale=configGet("locale")):
2022-08-10 14:08:15 +03:00
"""Get value of locale string
Args:
* key (str): The last key of the locale's keys path.
* *args (list): Path to key like: dict[args][key].
* locale (str): Locale to looked up in. Defaults to config's locale value.
Returns:
* any: Value of provided locale key
2023-03-09 12:33:02 +02:00
"""
if locale == None:
2022-08-10 14:08:15 +03:00
locale = configGet("locale")
2023-03-09 12:33:02 +02:00
2022-08-10 14:08:15 +03:00
try:
2022-09-18 19:27:13 +03:00
this_dict = jsonLoad(f'{configGet("locale", "locations")}{sep}{locale}.json')
2022-08-10 14:08:15 +03:00
except FileNotFoundError:
try:
2023-03-09 12:33:02 +02:00
this_dict = jsonLoad(
f'{configGet("locale", "locations")}{sep}{configGet("locale")}.json'
)
2022-08-10 14:08:15 +03:00
except FileNotFoundError:
try:
2023-03-09 12:33:02 +02:00
this_dict = jsonLoad(
f'{configGet("locale_fallback", "locations")}{sep}{configGet("locale")}.json'
)
2022-08-10 14:08:15 +03:00
except:
return f'⚠️ Locale in config is invalid: could not get "{key}" in {str(args)} from locale "{locale}"'
this_key = this_dict
for dict_key in args:
this_key = this_key[dict_key]
2023-03-09 12:33:02 +02:00
2022-08-10 14:08:15 +03:00
try:
return this_key[key]
except KeyError:
return f'⚠️ Locale in config is invalid: could not get "{key}" in {str(args)} from locale "{locale}"'
2023-03-09 12:33:02 +02:00
2023-03-20 13:03:03 +02:00
async def extract_and_save(handle: ZipFile, filename: str, destpath: str):
"""Extract and save file from archive
Args:
* handle (ZipFile): ZipFile handler
* filename (str): File base name
* path (str): Path where to store
"""
data = handle.read(filename)
filepath = path.join(destpath, filename)
try:
makedirs(path.dirname(filepath), exist_ok=True)
async with aiofiles.open(filepath, "wb") as fd:
await fd.write(data)
logWrite(f"Unzipped {filename}", debug=True)
except IsADirectoryError:
makedirs(filepath, exist_ok=True)
except FileNotFoundError:
pass
return
2022-08-11 12:27:17 +03:00
try:
2022-09-18 19:27:13 +03:00
from psutil import Process
2022-08-11 12:27:17 +03:00
except ModuleNotFoundError:
print(locale("deps_missing", "console", locale=configGet("locale")), flush=True)
2022-09-18 19:27:13 +03:00
exit()
2022-08-11 12:27:17 +03:00
2023-03-09 12:33:02 +02:00
def killProc(pid: int) -> None:
"""Kill process by its PID. Meant to be used to kill the main process of bot itself.
### Args:
* pid (`int`): PID of the target
"""
2022-09-18 19:27:13 +03:00
if osname == "posix":
2023-01-10 13:52:44 +02:00
from signal import SIGKILL
2023-03-09 12:33:02 +02:00
2022-09-18 19:27:13 +03:00
kill(pid, SIGKILL)
2022-08-10 14:08:15 +03:00
else:
2023-03-09 12:33:02 +02:00
Process(pid).kill()