From ebb8700496ddd7beba8657acf9c1c2f0389430de Mon Sep 17 00:00:00 2001
From: thedragonsinn <98635854+thedragonsinn@users.noreply.github.com>
Date: Thu, 16 Nov 2023 15:32:03 +0530
Subject: [PATCH] upload bug fixes and logs.
---
.gitignore | 1 +
app/__init__.py | 1 +
app/core/__init__.py | 1 +
app/core/client/client.py | 23 ++++++++++++-----------
app/core/logger.py | 24 ++++++++++++++++++++++++
app/plugins/misc/upload.py | 15 +++++----------
app/plugins/utils/logs.py | 14 ++++++++++++++
7 files changed, 58 insertions(+), 21 deletions(-)
create mode 100644 app/core/logger.py
create mode 100644 app/plugins/utils/logs.py
diff --git a/.gitignore b/.gitignore
index 466d5d3..8788524 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,3 +5,4 @@ down*
__pycache__
.idea/
conf_backup/
+logs/
diff --git a/app/__init__.py b/app/__init__.py
index 427530a..cce73fa 100644
--- a/app/__init__.py
+++ b/app/__init__.py
@@ -5,6 +5,7 @@ from dotenv import load_dotenv
load_dotenv("config.env")
from app.config import Config # NOQA
+from app.core import LOGGER # NOQA
from app.core.db import DB # NOQA
from app.core.client.client import BOT # NOQA
diff --git a/app/core/__init__.py b/app/core/__init__.py
index f314825..e3b570c 100644
--- a/app/core/__init__.py
+++ b/app/core/__init__.py
@@ -1,4 +1,5 @@
from app.core.client import filters
+from app.core.logger import LOGGER
from app.core.types.callback_query import CallbackQuery
from app.core.client.conversation import Conversation # NOQA
diff --git a/app/core/client/client.py b/app/core/client/client.py
index 3dcb9f3..2f80afb 100644
--- a/app/core/client/client.py
+++ b/app/core/client/client.py
@@ -13,18 +13,18 @@ from pyrogram.types import Message as Msg
from telegraph.aio import Telegraph
from app import DB, Config
-from app.core import Conversation, Message
+from app.core import Conversation, Message, logger
from app.utils import aiohttp_tools, helpers
def import_modules():
- for py_module in glob.glob(pathname="app/**/*.py", recursive=True):
+ for py_module in glob.glob(pathname="app/**/[!^_]*.py", recursive=True):
name = os.path.splitext(py_module)[0]
py_name = name.replace("/", ".")
try:
importlib.import_module(py_name)
- except Exception as e:
- print(e)
+ except Exception as exc:
+ logger.LOGGER.error(exc.with_exception())
async def init_tasks():
@@ -91,7 +91,7 @@ class BOT(Client):
async def boot(self) -> None:
await super().start()
- print("started")
+ logger.LOGGER.info("Started")
await asyncio.gather(
init_tasks(), self.edit_restart_msg(), self.log(text="Started")
)
@@ -124,12 +124,13 @@ class BOT(Client):
if message:
return (await message.copy(chat_id=Config.LOG_CHAT)) # fmt: skip
if traceback:
- text = f"""
-#Traceback
-Function: {func}
-Chat: {chat}
-Traceback:
-{traceback}"""
+ text = (
+ "#Traceback"
+ f"\nFunction: {func}"
+ f"\nChat: {chat}"
+ f"\nTraceback:"
+ f"\n{traceback}"
+ )
return await self.send_message(
chat_id=Config.LOG_CHAT,
text=text,
diff --git a/app/core/logger.py b/app/core/logger.py
new file mode 100644
index 0000000..2e180cd
--- /dev/null
+++ b/app/core/logger.py
@@ -0,0 +1,24 @@
+import os
+from logging import INFO, WARNING, basicConfig, getLogger, handlers
+
+os.makedirs("logs", exist_ok=True)
+
+LOGGER = getLogger("PLAIN_UB")
+
+basicConfig(
+ level=INFO,
+ format="[%(asctime)s] [%(levelname)s] [%(name)s]: %(message)s",
+ datefmt="%y-%m-%d %H:%M:%S",
+ handlers={
+ handlers.RotatingFileHandler(
+ filename="logs/app_logs.txt",
+ mode="a",
+ maxBytes=5 * 1024 * 1024,
+ backupCount=2,
+ encoding=None,
+ delay=0,
+ )
+ },
+)
+
+getLogger("pyrogram").setLevel(WARNING)
diff --git a/app/plugins/misc/upload.py b/app/plugins/misc/upload.py
index 1da4bfc..86caeef 100644
--- a/app/plugins/misc/upload.py
+++ b/app/plugins/misc/upload.py
@@ -14,14 +14,14 @@ async def video_upload(
file: DownloadedFile, has_spoiler: bool
) -> dict[str, bot.send_video, bot.send_animation, dict]:
thumb = await take_ss(file.full_path)
- if not check_audio(file.full_path):
+ if not (await check_audio(file.full_path)): # fmt:skip
return {
"method": bot.send_animation,
"kwargs": {
"thumb": thumb,
"unsave": True,
"animation": file.full_path,
- "duration": get_duration(file.full_path),
+ "duration": await get_duration(file.full_path),
"has_spoiler": has_spoiler,
},
}
@@ -30,7 +30,7 @@ async def video_upload(
"kwargs": {
"thumb": thumb,
"video": file.full_path,
- "duration": get_duration(file.full_path),
+ "duration": await get_duration(file.full_path),
"has_spoiler": has_spoiler,
},
}
@@ -52,7 +52,7 @@ async def audio_upload(
"method": bot.send_audio,
"kwargs": {
"audio": file.full_path,
- "duration": get_duration(file=file.full_path),
+ "duration": await get_duration(file=file.full_path),
},
}
@@ -120,12 +120,7 @@ async def upload(bot: BOT, message: Message):
await response.edit("invalid `cmd` | `url` | `file path`!!!")
return
await response.edit("uploading....")
- progress_args = (
- response,
- "Uploading...",
- file.name,
- file.full_path,
- )
+ progress_args = (response, "Uploading...", file.name, file.full_path)
if "-d" in message.flags:
media: dict = {
"method": bot.send_document,
diff --git a/app/plugins/utils/logs.py b/app/plugins/utils/logs.py
new file mode 100644
index 0000000..042d147
--- /dev/null
+++ b/app/plugins/utils/logs.py
@@ -0,0 +1,14 @@
+import aiofiles
+
+from app import BOT, bot
+from app.core import Message
+
+
+@bot.add_cmd(cmd="logs")
+async def read_logs(bot: BOT, message: Message):
+ async with aiofiles.open("logs/app_logs.txt", "r") as aio_file:
+ text = await aio_file.read()
+ if len(text) < 4050:
+ await message.reply(f"
{text}")
+ else:
+ await message.reply_document(document="logs/app_logs.txt")