Skip to content

Commit

Permalink
LOG_CHAT_ID, improve mediainfo, metadata before ffmpeg, better logging
Browse files Browse the repository at this point in the history
  • Loading branch information
5hojib committed Jan 4, 2025
1 parent 80f5ac1 commit 513595c
Show file tree
Hide file tree
Showing 12 changed files with 212 additions and 84 deletions.
38 changes: 33 additions & 5 deletions bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@
INFO,
WARNING,
FileHandler,
Formatter,
LogRecord,
StreamHandler,
basicConfig,
error,
getLogger,
info,
warning,
)
from socket import setdefaulttimeout
from time import time

from pytz import timezone
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from aria2p import API as ariaAPI # noqa: N811
from aria2p import Client as ariaClient
Expand All @@ -38,12 +43,35 @@
bot_loop = new_event_loop()
set_event_loop(bot_loop)

basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
handlers=[FileHandler("log.txt"), StreamHandler()],
level=INFO,
class CustomFormatter(Formatter):
def formatTime( # noqa: N802
self,
record: LogRecord,
datefmt: str | None,
) -> str:
dt: datetime = datetime.fromtimestamp(
record.created,
tz=timezone("Asia/Dhaka"),
)
return dt.strftime(datefmt)

def format(self, record: LogRecord) -> str:
return super().format(record).replace(record.levelname, record.levelname[:1])


formatter = CustomFormatter(
"[%(asctime)s] %(levelname)s - %(message)s [%(module)s:%(lineno)d]",
datefmt="%d-%b %I:%M:%S %p",
)

file_handler = FileHandler("log.txt")
file_handler.setFormatter(formatter)

stream_handler = StreamHandler()
stream_handler.setFormatter(formatter)

basicConfig(handlers=[file_handler, stream_handler], level=INFO)

LOGGER = getLogger(__name__)

intervals = {"status": {}, "qb": "", "stopAll": False}
Expand Down
1 change: 1 addition & 0 deletions bot/core/config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class Config:
PAID_CHANNEL_LINK = ""
DELETE_LINKS = False
FSUB_IDS = ""
LOG_CHAT_ID = 0

@classmethod
def get(cls, key):
Expand Down
19 changes: 0 additions & 19 deletions bot/helper/aeon_utils/gen_mediainfo.py

This file was deleted.

20 changes: 14 additions & 6 deletions bot/helper/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,21 @@ async def before_start(self):
self.up_dest = self.user_dict["upload_paths"][self.up_dest]

if self.ffmpeg_cmds and not isinstance(self.ffmpeg_cmds, list):
if self.user_dict.get("ffmpeg_cmds", None):
self.ffmpeg_cmds = self.user_dict["ffmpeg_cmds"].get(
self.ffmpeg_cmds,
None,
)
ffmpeg_dict = self.user_dict["ffmpeg_cmds"]
self.ffmpeg_cmds = [
value
for key in list(self.ffmpeg_cmds)
if key in ffmpeg_dict
for value in ffmpeg_dict[key]
]
elif "ffmpeg_cmds" not in self.user_dict and Config.FFMPEG_CMDS:
self.ffmpeg_cmds = Config.FFMPEG_CMDS.get(self.ffmpeg_cmds, None)
ffmpeg_dict = Config.FFMPEG_CMDS
self.ffmpeg_cmds = [
value
for key in list(self.ffmpeg_cmds)
if key in ffmpeg_dict
for value in ffmpeg_dict[key]
]
else:
self.ffmpeg_cmds = None
if self.ffmpeg_cmds:
Expand Down
6 changes: 5 additions & 1 deletion bot/helper/ext_utils/bot_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,11 @@ def arg_parser(items, arg_base):
break
sub_list.append(items[j])
if sub_list:
arg_base[part] = " ".join(sub_list)
value = " ".join(sub_list)
if part == "-ff" and not value.strip().startswith("["):
arg_base[part].add(value)
else:
arg_base[part] = value
i += len(sub_list)
i += 1
if "link" in arg_base:
Expand Down
46 changes: 32 additions & 14 deletions bot/helper/listeners/task_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,20 @@ async def on_download_complete(self):
self.subname = ""
self.subsize = 0

if self.metadata:
up_path = await self.proceed_metadata(
up_path,
gid
)
if self.is_cancelled:
return
self.is_file = await aiopath.isfile(up_path)
up_dir, self.name = up_path.rsplit("/", 1)
self.size = await get_path_size(up_dir)
self.subproc = None
self.subname = ""
self.subsize = 0

if self.ffmpeg_cmds:
up_path = await self.proceed_ffmpeg(
up_path,
Expand All @@ -208,17 +222,6 @@ async def on_download_complete(self):
self.is_file = await aiopath.isfile(up_path)
self.name = up_path.rsplit("/", 1)[1]

if self.metadata:
up_path = await self.proceed_metadata(up_path, gid)
if self.is_cancelled:
return
self.is_file = await aiopath.isfile(up_path)
up_dir, self.name = up_path.rsplit("/", 1)
self.size = await get_path_size(up_dir)
self.subproc = None
self.subname = ""
self.subsize = 0

if self.screen_shots:
up_path = await self.generate_screenshots(up_path)
if self.is_cancelled:
Expand Down Expand Up @@ -350,6 +353,7 @@ async def on_upload_complete(
):
await database.rm_complete_task(self.message.link)
msg = f"<b>Name: </b><code>{escape(self.name)}</code>\n\n<b>Size: </b>{get_readable_file_size(self.size)}"
done_msg = f"{self.tag}\nYour task is complete\nPlease check your inbox."
LOGGER.info(f"Task Done: {self.name}")
if self.is_leech:
msg += f"\n<b>Total Files: </b>{folders}"
Expand All @@ -363,11 +367,22 @@ async def on_upload_complete(
for index, (link, name) in enumerate(files.items(), start=1):
fmsg += f"{index}. <a href='{link}'>{name}</a>\n"
if len(fmsg.encode() + msg.encode()) > 4000:
await send_message(self.message, msg + fmsg)
await send_message(self.user_id, f"{msg}<blockquote expandable>{fmsg}</blockquote>")
if Config.LOG_CHAT_ID:
await send_message(
Config.LOG_CHAT_ID,
f"{msg}<blockquote expandable>{fmsg}</blockquote>",
)
await sleep(1)
fmsg = ""
if fmsg != "":
await send_message(self.message, msg + fmsg)
await send_message(self.user_id, f"{msg}<blockquote expandable>{fmsg}</blockquote>")
if Config.LOG_CHAT_ID:
await send_message(
Config.LOG_CHAT_ID,
f"{msg}<blockquote expandable>{fmsg}</blockquote>",
)
await send_message(self.message, done_msg)
else:
msg += f"\n\n<b>Type: </b>{mime_type}"
if mime_type == "Folder":
Expand Down Expand Up @@ -405,7 +420,10 @@ async def on_upload_complete(
msg += f"\n\nPath: <code>{rclone_path}</code>"
button = None
msg += f"\n\n<b>cc: </b>{self.tag}"
await send_message(self.message, msg, button)
await send_message(self.user_id, msg, button)
if Config.LOG_CHAT_ID:
await send_message(Config.LOG_CHAT_ID, msg, button)
await send_message(self.message, done_msg)
if self.seed:
if self.new_dir:
await clean_target(self.new_dir)
Expand Down
38 changes: 34 additions & 4 deletions bot/modules/mediainfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from bot import LOGGER
from bot.core.aeon_client import TgClient
from bot.helper.aeon_utils.access_check import token_check
from bot.helper.aeon_utils.gen_mediainfo import parseinfo
from bot.helper.ext_utils.bot_utils import cmd_exec
from bot.helper.ext_utils.telegraph_helper import telegraph
from bot.helper.telegram_helper.bot_commands import BotCommands
Expand All @@ -24,6 +23,31 @@
send_message,
)

section_dict = {"General", "Video", "Audio", "Text", "Image"}


def parseinfo(out, file_size):
tc = ""
skip = False
file_size_line = f"File size : {file_size / (1024 * 1024):.2f} MiB"

for line in out.split("\n"):
if line.startswith("Menu"):
skip = True
elif any(line.startswith(section) for section in section_dict):
skip = False
if not line.startswith("General"):
tc += "</pre><br>"
tc += f"<blockquote>{line.replace('Text', 'Subtitle')}</blockquote><pre>"
if not skip:
# Replace File size line
if line.startswith("File size"):
line = file_size_line
key, sep, value = line.partition(":")
tc += f"{key.strip():<28}{sep} {value.strip()}\n"
tc += "</pre><br>"
return tc


async def gen_mediainfo(message, link=None, media=None, msg=None):
temp_send = await send_message(message, "Generating MediaInfo...")
Expand All @@ -32,6 +56,7 @@ async def gen_mediainfo(message, link=None, media=None, msg=None):
if not await aiopath.isdir(path):
await mkdir(path)

file_size = 0
if link:
filename = re_search(".+/(.+)", link).group(1)
des_path = ospath.join(path, filename)
Expand All @@ -40,23 +65,28 @@ async def gen_mediainfo(message, link=None, media=None, msg=None):
}
async with aiohttp.ClientSession() as session:
async with session.get(link, headers=headers) as response:
file_size = int(response.headers.get("Content-Length", 0))
async with aiopen(des_path, "wb") as f:
async for chunk in response.content.iter_chunked(10000000):
await f.write(chunk)
break
elif media:
des_path = ospath.join(path, media.file_name)
if media.file_size <= 50000000:
file_size = media.file_size
if file_size <= 30000000:
await msg.download(ospath.join(getcwd(), des_path))
else:
async for chunk in TgClient.bot.stream_media(media, limit=5):
async for chunk in TgClient.bot.stream_media(media, limit=3):
async with aiopen(des_path, "ab") as f:
await f.write(chunk)

# Get MediaInfo
stdout, _, _ = await cmd_exec(ssplit(f'mediainfo "{des_path}"'))

# Parse MediaInfo with updated file size
tc = f"<h4>{ospath.basename(des_path)}</h4><br><br>"
if stdout:
tc += parseinfo(stdout)
tc += parseinfo(stdout, file_size)

except Exception as e:
LOGGER.error(e)
Expand Down
11 changes: 6 additions & 5 deletions bot/modules/mirror_leech.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ async def new_event(self):
"-ns": "",
"-md": "",
"-tl": "",
"-ff": None,
"-ff": set(),
}

arg_parser(input_list[1:], args)
Expand Down Expand Up @@ -168,10 +168,11 @@ async def new_event(self):
self.multi = 0

try:
if args["-ff"].strip().startswith("["):
self.ffmpeg_cmds = eval(args["-ff"])
else:
self.ffmpeg_cmds = args["-ff"]
if args["-ff"]:
if isinstance(args["-ff"], set):
self.ffmpeg_cmds = args["-ff"]
else:
self.ffmpeg_cmds = eval(args["-ff"])
except Exception as e:
self.ffmpeg_cmds = None
LOGGER.error(e)
Expand Down
Loading

0 comments on commit 513595c

Please sign in to comment.