Compare commits

...

3 Commits

Author SHA1 Message Date
1f345e87f7 Improved logging 2023-03-17 14:44:37 +01:00
717586a9f0 Improved logging 2023-03-17 14:13:28 +01:00
95351f247c This commit closed #12 2023-03-17 13:52:16 +01:00
7 changed files with 182 additions and 134 deletions

View File

@@ -65,7 +65,7 @@
},
"console": {
"shutdown": "Shutting down bot with pid {0}",
"startup":"Starting with pid {0}",
"startup": "Starting with pid {0}",
"keyboard_interrupt": "\nShutting down...",
"exception_occured": "Exception {0} happened on task execution",
"post_sent": "Sent {0} to {1} with caption {2} and silently {3}",
@@ -94,6 +94,12 @@
"cleanup_completed": "Performed cleanup of the sent files",
"cleanup_unathorized": "Requested cleanup of sent files but not authorized. Please pass '--confirm' to perform that",
"cleanup_index_completed": "Performed cleanup of sent files index",
"cleanup_index_unathorized": "Requested cleanup of sent files index but not authorized. Please pass '--confirm' to perform that"
"cleanup_index_unathorized": "Requested cleanup of sent files index but not authorized. Please pass '--confirm' to perform that",
"random_pic_response": "Random pic response: {0}",
"random_pic_error_code": "Could not get photos from album {0}: HTTP {1}",
"random_pic_error_debug": "Could not get photos from '{0}/albums/{1}/photos?q=&page_size={2}&caption=queue' using token '{3}': HTTP {4}",
"find_pic_error": "Could not find image with name '{0}' and caption '{1}' due to: {2}",
"pic_upload_error": "Could not upload '{0}' to API: HTTP {1} with message '{2}'",
"api_creds_invalid": "Incorrect API credentials! Could not login into '{0}' using login '{1}': HTTP {2}"
}
}

View File

@@ -94,6 +94,12 @@
"cleanup_completed": "Виконано очищення надісланих файлів",
"cleanup_unathorized": "Надіслано запит на очищення надісланих файлів, але не авторизовано. Для цього надайте аргумент '--confirm'",
"cleanup_index_completed": "Виконано очищення індексу надісланих файлів",
"cleanup_index_unathorized": "Надіслано запит на очищення індексу надісланих файлів, але не авторизовано. Для цього надайте аргумент '--confirm'"
"cleanup_index_unathorized": "Надіслано запит на очищення індексу надісланих файлів, але не авторизовано. Для цього надайте аргумент '--confirm'",
"random_pic_response": "Відповідь на пошук випадкової картинки: {0}",
"random_pic_error_code": "Не вдалося отримати фото з альбому {0}: HTTP {1}",
"random_pic_error_debug": "Не вдалося отримати фотографії з '{0}/albums/{1}/photos?q=&page_size={2}&caption=queue', використовуючи токен '{3}': HTTP {4}",
"find_pic_error": "Не вдалося знайти зображення з назвою '{0}' та підписом '{1}' через: {2}",
"pic_upload_error": "Не вдалося завантажити '{0}' до API: HTTP {1} з повідомленням '{2}'",
"api_creds_invalid": "Невірні облікові дані API! Не вдалося увійти в '{0}' за допомогою логіна '{1}': HTTP {2}"
}
}

View File

@@ -20,7 +20,7 @@ from classes.exceptions import (
UserCreationError,
)
from modules.logger import logWrite
from modules.utils import configGet
from modules.utils import configGet, locale
http_session = ClientSession(
json_serialize=dumps,
@@ -52,7 +52,15 @@ async def authorize() -> str:
)
if not response.ok:
logWrite(
f'Incorrect API credentials! Could not login into "{configGet("address", "posting", "api")}" using login "{configGet("username", "posting", "api")}": HTTP {response.status}'
locale(
"api_creds_invalid",
"console",
locale=configGet("locale_log").format(
configGet("address", "posting", "api"),
configGet("username", "posting", "api"),
response.status,
),
)
)
raise ValueError
async with aiofiles.open(
@@ -78,13 +86,34 @@ async def random_pic(token: Union[str, None] = None) -> Tuple[str, str]:
f'{configGet("address", "posting", "api")}/albums/{configGet("album", "posting", "api")}/photos?q=&page_size={configGet("page_size", "posting")}&caption=queue',
headers={"Authorization": f"Bearer {token}"},
)
print(await resp.json(), flush=True)
logWrite(
locale("random_pic_response", "console", locale=configGet("locale_log")).format(
await resp.json()
),
debug=True,
)
if resp.status != 200:
logWrite(
f'Could not get photos from album {configGet("album", "posting", "api")}: HTTP {resp.status}'
locale(
"random_pic_error_code",
"console",
locale=configGet("locale_log").format(
configGet("album", "posting", "api"), resp.status
),
),
)
logWrite(
f'Could not get photos from "{configGet("address", "posting", "api")}/albums/{configGet("album", "posting", "api")}/photos?q=&page_size={configGet("page_size", "posting")}&caption=queue" using token "{token}": HTTP {resp.status}',
locale(
"random_pic_error_debug",
"console",
locale=configGet("locale_log").format(
configGet("address", "posting", "api"),
configGet("album", "posting", "api"),
configGet("page_size", "posting"),
token,
resp.status,
),
),
debug=True,
)
raise ValueError
@@ -120,7 +149,13 @@ async def upload_pic(
response_json = await response.json()
if response.status != 200 and response.status != 409:
logWrite(
f"Could not upload '{filepath}' to API: HTTP {response.status} with message '{response.content}'"
locale(
"pic_upload_error",
"console",
locale=configGet("locale_log").format(
filepath, response.status, response.content
),
),
)
raise SubmissionUploadError(
str(filepath), response.status, response.content
@@ -161,7 +196,11 @@ async def find_pic(
return (await response.json())["results"]
except Exception as exp:
logWrite(
f"Could not find image with name '{name}' and caption '{caption}' due to: {exp}"
locale(
"find_pic_error",
"console",
locale=configGet("locale_log").format(name, caption, exp),
),
)
return None

View File

@@ -1,121 +0,0 @@
{
"locale": "en",
"locale_log": "en",
"locale_fallback": "en",
"owner": 0,
"admins": [],
"bot": {
"api_id": 0,
"api_hash": "",
"bot_token": ""
},
"database": {
"user": null,
"password": null,
"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": null,
"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"
],
"commands_admin": [
"import",
"export",
"reboot"
]
}

View File

@@ -9,6 +9,86 @@ from ujson import JSONDecodeError, dumps, loads
from modules.logger import logWrite
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"],
"commands_admin": ["import", "export", "reboot"],
}
def jsonLoad(filename: str) -> Any:
"""Loads arg1 as json and returns its contents"""
@@ -77,16 +157,16 @@ def configGet(key: str, *args: str):
this_key = this_dict
for dict_key in args:
this_key = this_key[dict_key]
this_key[key]
except KeyError:
print(
f"Could not find config key '{key}' under path {args}: falling back to default config",
flush=True,
)
fallback_dict = jsonLoad(path.join("modules", "default_config.json"))
this_key = fallback_dict
this_key = default_config
for dict_key in args:
this_key = this_key[dict_key]
configSet(key, this_key[key], args)
configSet(key, this_key[key], *args)
return this_key[key]

View File

@@ -7,6 +7,7 @@ from classes.poster_client import PosterClient
from classes.user import PosterUser
from modules.app import app
from modules.logger import logWrite
from modules.utils import configGet, locale
from modules.database import col_submitted
from bson import ObjectId
@@ -38,6 +39,10 @@ async def callback_query_yes(app: PosterClient, clb: CallbackQuery):
),
quote=True,
)
logWrite(
f"Submission with ID '{fullclb[2]}' could not be accepted because of the duplicates: {str(exp.duplicates)}",
debug=True,
)
return
if submission[0] is not None:
@@ -83,6 +88,11 @@ async def callback_query_yes(app: PosterClient, clb: CallbackQuery):
reply_markup=InlineKeyboardMarkup(edited_markup)
)
logWrite(
f"Submission with ID '{fullclb[2]}' accepted and uploaded with ID '{submission[1]}'",
debug=True,
)
# try:
# if configGet("api_based", "mode") is True:
# media = await app.download_media(submission, file_name=configGet("tmp", "locations")+sep)
@@ -173,6 +183,10 @@ async def callback_query_no(app: PosterClient, clb: CallbackQuery):
await clb.message.edit_reply_markup(
reply_markup=InlineKeyboardMarkup(edited_markup)
)
logWrite(
f"Submission with ID '{fullclb[2]}' rejected",
debug=True,
)
@app.on_callback_query(filters.regex("sub_block_[\s\S]*"))
@@ -200,6 +214,10 @@ async def callback_query_block(app: PosterClient, clb: CallbackQuery):
await clb.message.edit_reply_markup(
reply_markup=InlineKeyboardMarkup(edited_markup)
)
logWrite(
f"User {fullclb[2]} has been blocked",
debug=True,
)
@app.on_callback_query(filters.regex("sub_unblock_[\s\S]*"))
@@ -227,3 +245,7 @@ async def callback_query_unblock(app: PosterClient, clb: CallbackQuery):
await clb.message.edit_reply_markup(
reply_markup=InlineKeyboardMarkup(edited_markup)
)
logWrite(
f"User {fullclb[2]} has been unblocked",
debug=True,
)

View File

@@ -43,6 +43,10 @@ async def get_submission(app: PosterClient, msg: Message):
return
if msg.document is not None:
logWrite(
f"User {msg.from_user.id} is trying to submit a file of type '{msg.document.mime_type}' with name '{msg.document.file_name}' and size of {msg.document.file_size / 1024 / 1024} MB",
debug=True,
)
if msg.document.mime_type not in configGet("mime_types", "submission"):
await msg.reply_text(
locale("mime_not_allowed", "message", locale=user_locale),
@@ -65,6 +69,10 @@ async def get_submission(app: PosterClient, msg: Message):
) # , msg.document.file_name
if msg.video is not None:
logWrite(
f"User {msg.from_user.id} is trying to submit a video with name '{msg.video.file_name}' and size of {msg.video.file_size / 1024 / 1024} MB",
debug=True,
)
if msg.video.file_size > configGet("file_size", "submission"):
await msg.reply_text(
locale("document_too_large", "message", locale=user_locale).format(
@@ -78,6 +86,10 @@ async def get_submission(app: PosterClient, msg: Message):
contents = msg.video.file_id, SubmissionType.VIDEO # , msg.video.file_name
if msg.animation is not None:
logWrite(
f"User {msg.from_user.id} is trying to submit an animation with name '{msg.animation.file_name}' and size of {msg.animation.file_size / 1024 / 1024} MB",
debug=True,
)
if msg.animation.file_size > configGet("file_size", "submission"):
await msg.reply_text(
locale("document_too_large", "message", locale=user_locale).format(
@@ -94,6 +106,10 @@ async def get_submission(app: PosterClient, msg: Message):
) # , msg.animation.file_name
if msg.photo is not None:
logWrite(
f"User {msg.from_user.id} is trying to submit a photo with ID '{msg.photo.file_id}' and size of {msg.photo.file_size / 1024 / 1024} MB",
debug=True,
)
contents = msg.photo.file_id, SubmissionType.PHOTO # , "please_generate"
if save_tmp is not None: