From 016daaa0d91bfc8dd7165c28be05d85ce7324a3b Mon Sep 17 00:00:00 2001 From: Muhammad Absar Date: Sat, 1 Jun 2024 17:09:15 +0500 Subject: [PATCH 01/10] compeleted spliting of files for free users --- .gitignore | 43 ++++++++++++++ Dockerfile | 2 +- docker-compose.yml | 3 + main/__init__.py | 1 + main/plugins/batch.py | 8 ++- main/plugins/pyroplug.py | 122 ++++++++++++++++++++++++++++----------- main/plugins/splitter.py | 65 +++++++++++++++++++++ main/utils.py | 9 +++ requirements.txt | 1 + 9 files changed, 216 insertions(+), 38 deletions(-) create mode 100644 .gitignore create mode 100644 main/plugins/splitter.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..0b59bac84 --- /dev/null +++ b/.gitignore @@ -0,0 +1,43 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# PyCharm +.idea/ +*.iml +*.ipr +*.iws + +# VSCode +.vscode/ +.vscode/settings.json +.vscode/tasks.json +.vscode/launch.json +.vscode/extensions.json +.vscode/*.code-workspace + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Pyrogram/Telethon specific +*.session + +# Local environment variables +.env.local +.env.dev +.env.test +.env.prod + +# Log files +*.log diff --git a/Dockerfile b/Dockerfile index 0bd91ea89..3f5d7c50d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,7 +2,7 @@ FROM python:3.9.2-slim-buster RUN mkdir /app && chmod 777 /app WORKDIR /app ENV DEBIAN_FRONTEND=noninteractive -RUN apt -qq update && apt -qq install -y git python3 python3-pip ffmpeg +RUN apt -qq update && apt -qq install -y git python3 python3-pip ffmpeg p7zip-full COPY . . RUN pip3 install --no-cache-dir -r requirements.txt CMD ["bash","bash.sh"] diff --git a/docker-compose.yml b/docker-compose.yml index 51b41620d..86acc28b7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,3 +12,6 @@ services: SESSION: # Pyrogram string session AUTH: # User ID of Bot owner FORCESUB: # Username name of public channel without using '@' + # optionals + MAX_SPLIT_SIZE: # max file split size, if you don't have telegram premium - default is 2GB, if you have premium skip this option + BATCH_SIZE: # batch size - default is 100 \ No newline at end of file diff --git a/main/__init__.py b/main/__init__.py index f4f60cf34..bcbaf0ec8 100644 --- a/main/__init__.py +++ b/main/__init__.py @@ -19,6 +19,7 @@ FORCESUB = config("FORCESUB", default=None) AUTH = config("AUTH", default=None, cast=int) + bot = TelegramClient('bot', API_ID, API_HASH).start(bot_token=BOT_TOKEN) userbot = Client("saverestricted", session_string=SESSION, api_hash=API_HASH, api_id=API_ID) diff --git a/main/plugins/batch.py b/main/plugins/batch.py index 2bb76d965..0cf7d4b81 100644 --- a/main/plugins/batch.py +++ b/main/plugins/batch.py @@ -5,7 +5,7 @@ Plugin for both public & private channels! """ -import time, os, asyncio +import time, os, asyncio, decouple from .. import bot as Drone from .. import userbot, Bot, AUTH @@ -26,6 +26,8 @@ batch = [] +BATCH_SIZE = decouple.config("BATCH_SIZE", 100) + @Drone.on(events.NewMessage(incoming=True, from_users=AUTH, pattern='/cancel')) async def cancel(event): if not event.sender_id in batch: @@ -37,7 +39,7 @@ async def cancel(event): async def _batch(event): if not event.is_private: return - s, r = await force_sub(event.client, fs, event.sender_id, ft) + s, r = await force_sub(event.client, fs, event.sender_id, ft) if s == True: await event.reply(r) return @@ -66,7 +68,7 @@ async def _batch(event): return conv.cancel() try: value = int(_range.text) - if value > 100: + if value > BATCH_SIZE: await conv.send_message("You can only get upto 100 files in a single batch.") return conv.cancel() except ValueError: diff --git a/main/plugins/pyroplug.py b/main/plugins/pyroplug.py index 1e608e86a..87fa44d62 100644 --- a/main/plugins/pyroplug.py +++ b/main/plugins/pyroplug.py @@ -5,6 +5,9 @@ from .. import bot as Drone from main.plugins.progress import progress_for_pyrogram from main.plugins.helpers import screenshot +from main.utils import isPremium +from main.plugins.splitter import split_video, do_file_split + from pyrogram import Client, filters from pyrogram.errors import ChannelBanned, ChannelInvalid, ChannelPrivate, ChatIdInvalid, ChatInvalid, PeerIdInvalid @@ -94,47 +97,98 @@ async def get_msg(userbot, client, bot, sender, edit_id, msg_link, i): ) elif msg.media==MessageMediaType.VIDEO and msg.video.mime_type in ["video/mp4", "video/x-matroska"]: print("Trying to get metadata") - data = video_metadata(file) - height, width, duration = data["height"], data["width"], data["duration"] - print(f'd: {duration}, w: {width}, h:{height}') - try: - thumb_path = await screenshot(file, duration, sender) - except Exception: - thumb_path = None - await client.send_video( - chat_id=sender, - video=file, - caption=caption, - supports_streaming=True, - height=height, width=width, duration=duration, - thumb=thumb_path, - progress=progress_for_pyrogram, - progress_args=( - client, - '**UPLOADING:**\n', - edit, - time.time() + if (not (await isPremium(userbot)) and os.path.getsize(file) > 2 * 1024 * 1024 * 1024): + splitted_files = split_video(file) + for file in splitted_files: + data = video_metadata(file) + height, width, duration = data["height"], data["width"], data["duration"] + try: + thumb_path = await screenshot(file, duration, sender) + except Exception: + thumb_path = None + + await client.send_video( + chat_id=sender, + video=file, + caption=caption, + supports_streaming=True, + height=height, width=width, duration=duration, + thumb=thumb_path, + progress=progress_for_pyrogram, + progress_args=( + client, "**UPLOADING**\n", + edit, + time.time() + ) + ) + + # clean up + try: + os.remove(file) + except: + pass + else: + data = video_metadata(file) + height, width, duration = data["height"], data["width"], data["duration"] + print(f'd: {duration}, w: {width}, h:{height}') + try: + thumb_path = await screenshot(file, duration, sender) + except Exception: + thumb_path = None + + await client.send_video( + chat_id=sender, + video=file, + caption=caption, + supports_streaming=True, + height=height, width=width, duration=duration, + thumb=thumb_path, + progress=progress_for_pyrogram, + progress_args=( + client, + '**UPLOADING:**\n', + edit, + time.time() + ) ) - ) elif msg.media==MessageMediaType.PHOTO: await edit.edit("Uploading photo.") await bot.send_file(sender, file, caption=caption) else: - thumb_path=thumbnail(sender) - await client.send_document( - sender, - file, - caption=caption, - thumb=thumb_path, - progress=progress_for_pyrogram, - progress_args=( - client, - '**UPLOADING:**\n', - edit, - time.time() + thumb_path = thumbnail(sender) + if (not (await isPremium(client)) and os.path.getsize(file)): + # splitting the files + splitted_files = do_file_split(file) + for file in splitted_files: + await client.send_docment( + sender, + file, + caption=caption, + thumb_path=thumb_path, + progress=progress_for_pyrogram, + progress_args=( + client, + '**UPLOADING:**\n', + edit, + time.time() + ) + ) + + else: + await client.send_document( + sender, + file, + caption=caption, + thumb=thumb_path, + progress=progress_for_pyrogram, + progress_args=( + client, + '**UPLOADING:**\n', + edit, + time.time() + ) ) - ) try: os.remove(file) if os.path.isfile(file) == True: diff --git a/main/plugins/splitter.py b/main/plugins/splitter.py new file mode 100644 index 000000000..d313bd2ca --- /dev/null +++ b/main/plugins/splitter.py @@ -0,0 +1,65 @@ +import subprocess +import os +import math +import logzero +from decouple import config + +logger = logzero.logger + +MAX_SPLIT_SIZE = config("MAX_SPLIT_SIZE", default=2000) + + +def split_video(video_path, size='2G'): + base_path, ext = os.path.splitext(video_path) + output_path_template = f"{base_path}_part%03d{ext}" + + split_command = f"ffmpeg -i {video_path} -c copy -map 0 -segment_time 00:20:00 -f segment -reset_timestamps 1 {output_path_template}" + subprocess.call(split_command, shell=True) + + video_paths = [f"{base_path}_part{str(i).zfill(3)}{ext}" for i in range(999) if os.path.exists(f"{base_path}_part{str(i).zfill(3)}{ext}")] + + return video_paths + + +def file_split_7z(file_path, split_size=MAX_SPLIT_SIZE): + file_path_7z_list = [] + + origin_file_path = "" + if os.path.splitext(file_path)[1] == ".7z": + origin_file_path = file_path + file_path = os.path.splitext(origin_file_path)[0] + ".7zo" + os.rename(origin_file_path, file_path) + + fz = os.path.getsize(file_path) / 1024 / 1024 + pa = math.ceil(fz / split_size) + head, ext = os.path.splitext(os.path.abspath(file_path)) + archive_head = "".join((head, ext.replace(".", "_"))) + ".7z" + for i in range(pa): + check_file_name = "{}.{:03d}".format(archive_head, i + 1) + if os.path.isfile(check_file_name): + logger.debug("remove exists file | {}".format(check_file_name)) + os.remove(check_file_name) + cmd_7z = ["7z", "a", "-v{}m".format(split_size), "-y", "-mx0", archive_head, file_path] + proc = subprocess.Popen(cmd_7z, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + out, err = proc.communicate() + if b"Everything is Ok" not in out: + logger.error("7z output | {}".format(out.decode("utf-8"))) + logger.error("7z error | {}".format(err.decode("utf-8"))) + return file_path_7z_list + + for i in range(pa): + file_path_7z_list.append("{}.{:03d}".format(archive_head, i + 1)) + + if origin_file_path: + os.rename(file_path, origin_file_path) + return file_path_7z_list + + +def do_file_split(file_path, split_size=MAX_SPLIT_SIZE): + file_size = os.path.getsize(file_path) / 2 ** 20 + split_part = math.ceil(file_size / split_size) + new_split_size = math.ceil(file_size / split_part) + logger.info("file size | {} | split num | {} | split size | {}".format(file_size, split_part, new_split_size)) + file_path_7z_list = file_split_7z(file_path, split_size=new_split_size) + logger.info(file_path_7z_list) + return file_path_7z_list diff --git a/main/utils.py b/main/utils.py index ba63ac827..7d7e2f934 100644 --- a/main/utils.py +++ b/main/utils.py @@ -2,6 +2,8 @@ import logging import importlib from pathlib import Path +from pyrogram import Client + def load_plugins(plugin_name): path = Path(f"main/plugins/{plugin_name}.py") @@ -12,3 +14,10 @@ def load_plugins(plugin_name): spec.loader.exec_module(load) sys.modules["main.plugins." + plugin_name] = load print("main has Imported " + plugin_name) + +async def isPremium(client:Client ) -> bool: + my_info = await client.get_me() + return my_info.is_premium + + + diff --git a/requirements.txt b/requirements.txt index 48fcbc6ff..e02874c0e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,3 +6,4 @@ cryptg tgcrypto pyrogram python-decouple +logzero \ No newline at end of file From 1b056c405a60cf62acfce2b179b093e32b05fe46 Mon Sep 17 00:00:00 2001 From: Muhammad Absar Date: Sat, 1 Jun 2024 18:31:46 +0500 Subject: [PATCH 02/10] Updated docker-compose for max_split_size --- docker-compose.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 86acc28b7..3f1537019 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,4 +1,4 @@ -version: "3.3" + version: "3.3" services: app: @@ -13,5 +13,5 @@ services: AUTH: # User ID of Bot owner FORCESUB: # Username name of public channel without using '@' # optionals - MAX_SPLIT_SIZE: # max file split size, if you don't have telegram premium - default is 2GB, if you have premium skip this option + MAX_SPLIT_SIZE: # max file split size, if you don't have telegram premium - default is 2000 (for 2GB), if you have premium skip this option BATCH_SIZE: # batch size - default is 100 \ No newline at end of file From e02cf11a064394f1430aed9bd4d3e71ee354d3ea Mon Sep 17 00:00:00 2001 From: Muhammad Absar Date: Sun, 2 Jun 2024 09:38:05 +0500 Subject: [PATCH 03/10] updated gitignore for test files --- .gitignore | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.gitignore b/.gitignore index 0b59bac84..cc0612c61 100644 --- a/.gitignore +++ b/.gitignore @@ -32,6 +32,7 @@ venv.bak/ # Pyrogram/Telethon specific *.session +*.session-journal # Local environment variables .env.local @@ -41,3 +42,7 @@ venv.bak/ # Log files *.log + +# test files +*.test + From ebcf8ebda560857d78a89d9767a2a723ca00f05a Mon Sep 17 00:00:00 2001 From: Muhammad Absar Date: Sun, 2 Jun 2024 09:39:06 +0500 Subject: [PATCH 04/10] removed bugs from video splitting and added the reuquired dependcies to requirements.txt --- main/plugins/pyroplug.py | 88 ++++++++++++++++++++++++++++------------ main/plugins/splitter.py | 35 +++++++++++++--- requirements.txt | 3 +- 3 files changed, 92 insertions(+), 34 deletions(-) diff --git a/main/plugins/pyroplug.py b/main/plugins/pyroplug.py index 87fa44d62..f4e065099 100644 --- a/main/plugins/pyroplug.py +++ b/main/plugins/pyroplug.py @@ -97,36 +97,53 @@ async def get_msg(userbot, client, bot, sender, edit_id, msg_link, i): ) elif msg.media==MessageMediaType.VIDEO and msg.video.mime_type in ["video/mp4", "video/x-matroska"]: print("Trying to get metadata") - if (not (await isPremium(userbot)) and os.path.getsize(file) > 2 * 1024 * 1024 * 1024): - splitted_files = split_video(file) - for file in splitted_files: - data = video_metadata(file) - height, width, duration = data["height"], data["width"], data["duration"] - try: - thumb_path = await screenshot(file, duration, sender) - except Exception: - thumb_path = None - - await client.send_video( - chat_id=sender, - video=file, - caption=caption, - supports_streaming=True, - height=height, width=width, duration=duration, - thumb=thumb_path, - progress=progress_for_pyrogram, - progress_args=( - client, "**UPLOADING**\n", - edit, - time.time() + if (not (await isPremium(userbot)) and os.path.getsize(file) > 2 * 1024 * 1024 * 1024 ): + try: + splitted_files = split_video(file) + if not splitted_files: + print(splitted_files) + raise Exception("The input files are corrupt") + for file in splitted_files: + data = video_metadata(file) + height, width, duration = data["height"], data["width"], data["duration"] + try: + thumb_path = await screenshot(file, duration, sender) + except Exception: + thumb_path = None + await client.send_video( + chat_id=sender, + video=file, + caption=caption, + supports_streaming=True, + height=height, width=width, duration=duration, + thumb=thumb_path, + progress=progress_for_pyrogram, + progress_args=( + client, "**UPLOADING**\n", + edit, + time.time() + ) ) - ) + + # clean up + try: + os.remove(file) + except: + try: + os.remove(file) + except: + pass # clean up try: os.remove(file) except: - pass + try: + os.remove(file) + except: + pass + except Exception as e: + await client.send_message(sender, f"An error occured while splitting the file\n\n{e}") else: data = video_metadata(file) height, width, duration = data["height"], data["width"], data["duration"] @@ -157,15 +174,15 @@ async def get_msg(userbot, client, bot, sender, edit_id, msg_link, i): await bot.send_file(sender, file, caption=caption) else: thumb_path = thumbnail(sender) - if (not (await isPremium(client)) and os.path.getsize(file)): + if (not (await isPremium(client)) and os.path.getsize(file) > 2 * 1024 * 1024 * 1024): # splitting the files splitted_files = do_file_split(file) for file in splitted_files: - await client.send_docment( + await client.send_document( sender, file, caption=caption, - thumb_path=thumb_path, + thumb=thumb_path, progress=progress_for_pyrogram, progress_args=( client, @@ -175,6 +192,23 @@ async def get_msg(userbot, client, bot, sender, edit_id, msg_link, i): ) ) + # clean up + try: + os.remove(file) + except: + try: + os.remove(file) + except: + pass + + # clean up + try: + os.remove(file) + except: + try: + os.remove(file) + except: + pass else: await client.send_document( sender, diff --git a/main/plugins/splitter.py b/main/plugins/splitter.py index d313bd2ca..2ef682739 100644 --- a/main/plugins/splitter.py +++ b/main/plugins/splitter.py @@ -3,22 +3,45 @@ import math import logzero from decouple import config +from time import sleep +from moviepy.editor import VideoFileClip logger = logzero.logger MAX_SPLIT_SIZE = config("MAX_SPLIT_SIZE", default=2000) +MAX_SPLIT_SIZE = 2000 +def split_video(video_path:str, size='2G'): + sleep(3) + clip = VideoFileClip(video_path) + duration = clip.duration + half_duration = duration / 2 -def split_video(video_path, size='2G'): base_path, ext = os.path.splitext(video_path) - output_path_template = f"{base_path}_part%03d{ext}" + base_path = "_".join(base_path.split()) + output_path_template = f"{base_path}_part%2d{ext}" - split_command = f"ffmpeg -i {video_path} -c copy -map 0 -segment_time 00:20:00 -f segment -reset_timestamps 1 {output_path_template}" - subprocess.call(split_command, shell=True) + file_name = video_path.split("/")[-1] + directory = video_path.split("/")[0:-1] + directory = "/".join(directory) + video_path = f"{directory}/'{file_name}'" - video_paths = [f"{base_path}_part{str(i).zfill(3)}{ext}" for i in range(999) if os.path.exists(f"{base_path}_part{str(i).zfill(3)}{ext}")] - return video_paths + try: + split_command = f"ffmpeg -i {video_path} -loglevel fatal -c copy -segment_time {half_duration} -f segment -reset_timestamps 1 {output_path_template}" + return_code = subprocess.call(split_command, shell=True) + if return_code != 0: + print(f"Error: ffmpeg command failed with return code {return_code}") + return [] + + sleep(3) + + video_paths = [f"{base_path}_part0{i}{ext}" for i in range(5) if os.path.exists(f"{base_path}_part0{i}{ext}")] + return video_paths + except: + print("Error") + return [] + def file_split_7z(file_path, split_size=MAX_SPLIT_SIZE): diff --git a/requirements.txt b/requirements.txt index e02874c0e..3523572e8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,4 +6,5 @@ cryptg tgcrypto pyrogram python-decouple -logzero \ No newline at end of file +logzero +moviepy \ No newline at end of file From 99249632136e15730073d82915a7a040b0e4fb5e Mon Sep 17 00:00:00 2001 From: Muhammad Absar Date: Sun, 2 Jun 2024 09:44:58 +0500 Subject: [PATCH 05/10] removed free space --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index 3f1537019..0d16e736d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,4 +1,4 @@ - version: "3.3" +version: "3.3" services: app: From 45146f3bade8412ccceb4849b10362a2dbdefe56 Mon Sep 17 00:00:00 2001 From: Muhammad Absar Date: Sun, 2 Jun 2024 13:17:13 +0500 Subject: [PATCH 06/10] fixed typo in docker-compose --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index 0d16e736d..f24e261f7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -13,5 +13,5 @@ services: AUTH: # User ID of Bot owner FORCESUB: # Username name of public channel without using '@' # optionals - MAX_SPLIT_SIZE: # max file split size, if you don't have telegram premium - default is 2000 (for 2GB), if you have premium skip this option + MAX_SPLIT_SIZE: # max file split size in Mb, if you don't have telegram premium - default is 2048 (for 2GB), if you have premium skip this option BATCH_SIZE: # batch size - default is 100 \ No newline at end of file From 0d36e62ea436e909e6c614168cd794b1825388a9 Mon Sep 17 00:00:00 2001 From: Muhammad Absar Date: Sun, 2 Jun 2024 13:17:57 +0500 Subject: [PATCH 07/10] improved the document files splitting according to MAX_SPLIT_SIZE --- main/plugins/splitter.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/main/plugins/splitter.py b/main/plugins/splitter.py index 2ef682739..d87f8b755 100644 --- a/main/plugins/splitter.py +++ b/main/plugins/splitter.py @@ -8,10 +8,9 @@ logger = logzero.logger -MAX_SPLIT_SIZE = config("MAX_SPLIT_SIZE", default=2000) -MAX_SPLIT_SIZE = 2000 +MAX_SPLIT_SIZE = config("MAX_SPLIT_SIZE", default=2*1024) -def split_video(video_path:str, size='2G'): +def split_video(video_path:str): sleep(3) clip = VideoFileClip(video_path) duration = clip.duration @@ -78,11 +77,7 @@ def file_split_7z(file_path, split_size=MAX_SPLIT_SIZE): return file_path_7z_list -def do_file_split(file_path, split_size=MAX_SPLIT_SIZE): - file_size = os.path.getsize(file_path) / 2 ** 20 - split_part = math.ceil(file_size / split_size) - new_split_size = math.ceil(file_size / split_part) - logger.info("file size | {} | split num | {} | split size | {}".format(file_size, split_part, new_split_size)) - file_path_7z_list = file_split_7z(file_path, split_size=new_split_size) - logger.info(file_path_7z_list) +def do_file_split(file_path): + file_path_7z_list = file_split_7z(file_path) return file_path_7z_list + From 91ebac82b36f376ed8bd16e235edc32a0b2cf46d Mon Sep 17 00:00:00 2001 From: Muhammad Absar Date: Sun, 2 Jun 2024 13:20:48 +0500 Subject: [PATCH 08/10] updated container version to match the movie.py import issue --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 3f5d7c50d..649864692 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM python:3.9.2-slim-buster +FROM python:3.10-slim RUN mkdir /app && chmod 777 /app WORKDIR /app ENV DEBIAN_FRONTEND=noninteractive From b7b2dc10c529a31afb7e2a268483c7bdd7306427 Mon Sep 17 00:00:00 2001 From: Muhammad Absar Date: Sun, 2 Jun 2024 13:30:28 +0500 Subject: [PATCH 09/10] configured the whole project to use logger instead of print statements --- main/__init__.py | 6 ++++-- main/__main__.py | 6 ++++-- main/plugins/batch.py | 8 +++++--- main/plugins/frontend.py | 3 ++- main/plugins/helpers.py | 4 +++- main/plugins/pyroplug.py | 19 +++++++++---------- main/plugins/splitter.py | 7 +++---- main/utils.py | 3 +++ 8 files changed, 33 insertions(+), 23 deletions(-) diff --git a/main/__init__.py b/main/__init__.py index bcbaf0ec8..1c076275e 100644 --- a/main/__init__.py +++ b/main/__init__.py @@ -8,6 +8,8 @@ from decouple import config import logging, time, sys +from main.utils import logger + logging.basicConfig(format='[%(levelname) 5s/%(asctime)s] %(name)s: %(message)s', level=logging.WARNING) @@ -27,7 +29,7 @@ try: userbot.start() except BaseException: - print("Userbot Error ! Have you added SESSION while deploying??") + logger.error("Userbot Error ! Have you added SESSION while deploying??") sys.exit(1) Bot = Client( @@ -40,5 +42,5 @@ try: Bot.start() except Exception as e: - print(e) + logger.error(e) sys.exit(1) diff --git a/main/__main__.py b/main/__main__.py index fda9da21e..1f8a4a0ca 100644 --- a/main/__main__.py +++ b/main/__main__.py @@ -4,6 +4,8 @@ import logging from . import bot +from main.utils import logger + logging.basicConfig(format='[%(levelname) 5s/%(asctime)s] %(name)s: %(message)s', level=logging.WARNING) @@ -16,8 +18,8 @@ load_plugins(plugin_name.replace(".py", "")) #Don't be a thief -print("Successfully deployed!") -print("By MaheshChauhan • DroneBots") +logger.info("Successfully deployed!") +logger.info("By MaheshChauhan • DroneBots") if __name__ == "__main__": bot.run_until_disconnected() diff --git a/main/plugins/batch.py b/main/plugins/batch.py index 0cf7d4b81..be2e4f21c 100644 --- a/main/plugins/batch.py +++ b/main/plugins/batch.py @@ -12,6 +12,8 @@ from .. import FORCESUB as fs from main.plugins.pyroplug import get_bulk_msg from main.plugins.helpers import get_link, screenshot +from main.utils import logger + from telethon import events, Button, errors from telethon.tl.types import DocumentAttributeVideo @@ -56,14 +58,14 @@ async def _batch(event): await conv.send_message("No link found.") return conv.cancel() except Exception as e: - print(e) + logger.error(e) await conv.send_message("Cannot wait more longer for your response!") return conv.cancel() await conv.send_message("Send me the number of files/range you want to save from the given message, as a reply to this message.", buttons=Button.force_reply()) try: _range = await conv.get_reply() except Exception as e: - print(e) + logger.error(e) await conv.send_message("Cannot wait more longer for your response!") return conv.cancel() try: @@ -98,7 +100,7 @@ async def run_batch(userbot, client, sender, link, _range): await client.send_message(sender, "Batch completed.") break except Exception as e: - print(e) + logger.error(e) await client.send_message(sender, "Batch completed.") break try: diff --git a/main/plugins/frontend.py b/main/plugins/frontend.py index b35c84ea0..e13fc9867 100644 --- a/main/plugins/frontend.py +++ b/main/plugins/frontend.py @@ -7,6 +7,7 @@ from .. import FORCESUB as fs from main.plugins.pyroplug import get_msg from main.plugins.helpers import get_link, join +from main.utils import loggger from telethon import events from pyrogram.errors import FloodWait @@ -44,6 +45,6 @@ async def clone(event): except FloodWait as fw: return await Drone.send_message(event.sender_id, f'Try again after {fw.x} seconds due to floodwait from telegram.') except Exception as e: - print(e) + loggger.error(e) await Drone.send_message(event.sender_id, f"An error occurred during cloning of `{link}`\n\n**Error:** {str(e)}") diff --git a/main/plugins/helpers.py b/main/plugins/helpers.py index bc3ed2a47..22609f99e 100644 --- a/main/plugins/helpers.py +++ b/main/plugins/helpers.py @@ -1,5 +1,7 @@ #Github.com/Vasusen-code +from main.utils import logger + from pyrogram.errors import FloodWait, InviteHashInvalid, InviteHashExpired, UserAlreadyParticipant from telethon import errors, events @@ -20,7 +22,7 @@ async def join(client, invite_link): except FloodWait: return "Too many requests, try again later." except Exception as e: - print(e) + logger.error(e) return "Could not join, try joining manually." #Regex--------------------------------------------------------------------------------------------------------------- diff --git a/main/plugins/pyroplug.py b/main/plugins/pyroplug.py index f4e065099..68db7b639 100644 --- a/main/plugins/pyroplug.py +++ b/main/plugins/pyroplug.py @@ -7,7 +7,7 @@ from main.plugins.helpers import screenshot from main.utils import isPremium from main.plugins.splitter import split_video, do_file_split - +from main.utils import logger from pyrogram import Client, filters from pyrogram.errors import ChannelBanned, ChannelInvalid, ChannelPrivate, ChatIdInvalid, ChatInvalid, PeerIdInvalid @@ -67,17 +67,16 @@ async def get_msg(userbot, client, bot, sender, edit_id, msg_link, i): time.time() ) ) - print(file) + logger.info(file) await edit.edit('Preparing to Upload!') caption = None if msg.caption is not None: caption = msg.caption if msg.media==MessageMediaType.VIDEO_NOTE: round_message = True - print("Trying to get metadata") + logger.info("Trying to get metadata") data = video_metadata(file) height, width, duration = data["height"], data["width"], data["duration"] - print(f'd: {duration}, w: {width}, h:{height}') try: thumb_path = await screenshot(file, duration, sender) except Exception: @@ -96,12 +95,12 @@ async def get_msg(userbot, client, bot, sender, edit_id, msg_link, i): ) ) elif msg.media==MessageMediaType.VIDEO and msg.video.mime_type in ["video/mp4", "video/x-matroska"]: - print("Trying to get metadata") + logger.info("Trying to get metadata") if (not (await isPremium(userbot)) and os.path.getsize(file) > 2 * 1024 * 1024 * 1024 ): try: splitted_files = split_video(file) if not splitted_files: - print(splitted_files) + logger.info(file) raise Exception("The input files are corrupt") for file in splitted_files: data = video_metadata(file) @@ -147,7 +146,7 @@ async def get_msg(userbot, client, bot, sender, edit_id, msg_link, i): else: data = video_metadata(file) height, width, duration = data["height"], data["width"], data["duration"] - print(f'd: {duration}, w: {width}, h:{height}') + logger.info(f'd: {duration}, w: {width}, h:{height}') try: thumb_path = await screenshot(file, duration, sender) except Exception: @@ -242,7 +241,7 @@ async def get_msg(userbot, client, bot, sender, edit_id, msg_link, i): new_link = f"t.me/b/{chat}/{msg_id}" return await get_msg(userbot, client, bot, sender, edit_id, msg_link, i) except Exception as e: - print(e) + logger.error(e) if "messages.SendMedia" in str(e) \ or "SaveBigFilePartRequest" in str(e) \ or "SendMediaRequest" in str(e) \ @@ -264,7 +263,7 @@ async def get_msg(userbot, client, bot, sender, edit_id, msg_link, i): if os.path.isfile(file) == True: os.remove(file) except Exception as e: - print(e) + logger.error(e) await client.edit_message_text(sender, edit_id, f'Failed to save: `{msg_link}`\n\nError: {str(e)}') try: os.remove(file) @@ -296,7 +295,7 @@ async def get_msg(userbot, client, bot, sender, edit_id, msg_link, i): return await get_msg(userbot, client, bot, sender, edit_id, new_link, i) await client.copy_message(sender, chat, msg_id) except Exception as e: - print(e) + logger.error(e) return await client.edit_message_text(sender, edit_id, f'Failed to save: `{msg_link}`\n\nError: {str(e)}') await edit.delete() diff --git a/main/plugins/splitter.py b/main/plugins/splitter.py index d87f8b755..4d2c16e32 100644 --- a/main/plugins/splitter.py +++ b/main/plugins/splitter.py @@ -1,12 +1,11 @@ import subprocess import os import math -import logzero from decouple import config from time import sleep from moviepy.editor import VideoFileClip -logger = logzero.logger +from main.utils import logger MAX_SPLIT_SIZE = config("MAX_SPLIT_SIZE", default=2*1024) @@ -30,7 +29,7 @@ def split_video(video_path:str): split_command = f"ffmpeg -i {video_path} -loglevel fatal -c copy -segment_time {half_duration} -f segment -reset_timestamps 1 {output_path_template}" return_code = subprocess.call(split_command, shell=True) if return_code != 0: - print(f"Error: ffmpeg command failed with return code {return_code}") + logger.error(f"Error: ffmpeg command failed with return code {return_code}") return [] sleep(3) @@ -38,7 +37,7 @@ def split_video(video_path:str): video_paths = [f"{base_path}_part0{i}{ext}" for i in range(5) if os.path.exists(f"{base_path}_part0{i}{ext}")] return video_paths except: - print("Error") + logger.error("Error") return [] diff --git a/main/utils.py b/main/utils.py index 7d7e2f934..f4fafba2b 100644 --- a/main/utils.py +++ b/main/utils.py @@ -4,6 +4,9 @@ from pathlib import Path from pyrogram import Client +import logzero + +logger = logzero.logger def load_plugins(plugin_name): path = Path(f"main/plugins/{plugin_name}.py") From 4ca0ee8237cc8993a74320e7ef9694584e6aa91d Mon Sep 17 00:00:00 2001 From: Muhammad Absar Date: Sun, 2 Jun 2024 13:33:25 +0500 Subject: [PATCH 10/10] fixed typo --- main/plugins/frontend.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main/plugins/frontend.py b/main/plugins/frontend.py index e13fc9867..85fdc2d64 100644 --- a/main/plugins/frontend.py +++ b/main/plugins/frontend.py @@ -7,7 +7,7 @@ from .. import FORCESUB as fs from main.plugins.pyroplug import get_msg from main.plugins.helpers import get_link, join -from main.utils import loggger +from main.utils import logger from telethon import events from pyrogram.errors import FloodWait @@ -45,6 +45,6 @@ async def clone(event): except FloodWait as fw: return await Drone.send_message(event.sender_id, f'Try again after {fw.x} seconds due to floodwait from telegram.') except Exception as e: - loggger.error(e) + logger.error(e) await Drone.send_message(event.sender_id, f"An error occurred during cloning of `{link}`\n\n**Error:** {str(e)}")