diff --git a/bot/helper/mirror_utils/download_utils/aria2_download.py b/bot/helper/mirror_utils/download_utils/aria2_download.py index c3fbd59..4ca0f9e 100644 --- a/bot/helper/mirror_utils/download_utils/aria2_download.py +++ b/bot/helper/mirror_utils/download_utils/aria2_download.py @@ -15,38 +15,34 @@ class AriaDownloadHelper(DownloadHelper): super().__init__() @new_thread - def __onDownloadStarted(self, api: API, gid): + def __onDownloadStarted(self, api, gid): if STOP_DUPLICATE_MIRROR or TORRENT_DIRECT_LIMIT is not None or TAR_UNZIP_LIMIT is not None: sleep(1) dl = getDownloadByGid(gid) - download = api.get_download(gid) - + download = aria2.get_download(gid) if STOP_DUPLICATE_MIRROR: LOGGER.info(f"Checking File/Folder if already in Drive...") sleep(1) - self.name = aria2.get_download(gid).name - sname = self.name + sname = aria2.get_download(gid).name if self.listener.isTar: sname = sname + ".tar" - if self.listener.extract: + elif self.listener.extract: smsg = None else: gdrive = GoogleDriveHelper(None) smsg, button = gdrive.drive_list(sname) if smsg: dl.getListener().onDownloadError(f'File/Folder already available in Drive.\n\n') - aria2.remove([download], force = True) - aria2.purge() + aria2.remove([download], force=True) sendMarkup("Here are the search results:", dl.getListener().bot, dl.getListener().update, button) return - if TORRENT_DIRECT_LIMIT is not None or TAR_UNZIP_LIMIT is not None: limit = None if TAR_UNZIP_LIMIT is not None and (self.listener.isTar or self.listener.extract): LOGGER.info(f"Checking File/Folder Size...") limit = TAR_UNZIP_LIMIT mssg = f'Tar/Unzip limit is {TAR_UNZIP_LIMIT}' - if TORRENT_DIRECT_LIMIT is not None and limit is None: + elif TORRENT_DIRECT_LIMIT is not None and limit is None: LOGGER.info(f"Checking File/Folder Size...") limit = TORRENT_DIRECT_LIMIT mssg = f'Torrent/Direct limit is {TORRENT_DIRECT_LIMIT}' @@ -58,24 +54,23 @@ class AriaDownloadHelper(DownloadHelper): if 'G' in limit[1] or 'g' in limit[1]: if size > limitint * 1024**3: dl.getListener().onDownloadError(f'{mssg}.\nYour File/Folder size is {get_readable_file_size(size)}') - aria2.remove([download], force = True) - aria2.purge() + aria2.remove([download], force=True) return elif 'T' in limit[1] or 't' in limit[1]: if size > limitint * 1024**4: dl.getListener().onDownloadError(f'{mssg}.\nYour File/Folder size is {get_readable_file_size(size)}') - aria2.remove([download], force = True) - aria2.purge() + aria2.remove([download], force=True) return update_all_messages() def __onDownloadComplete(self, api: API, gid): - LOGGER.info(f"onDownloadComplete: {gid}") dl = getDownloadByGid(gid) - download = api.get_download(gid) + download = aria2.get_download(gid) if download.followed_by_ids: new_gid = download.followed_by_ids[0] - new_download = api.get_download(new_gid) + new_download = aria2.get_download(new_gid) + if dl is None: + dl = getDownloadByGid(new_gid) with download_dict_lock: download_dict[dl.uid()] = AriaDownloadStatus(new_gid, dl.getListener()) if new_download.is_torrent: @@ -88,22 +83,20 @@ class AriaDownloadHelper(DownloadHelper): @new_thread def __onDownloadStopped(self, api, gid): - sleep(5) + sleep(0.5) dl = getDownloadByGid(gid) if dl: dl.getListener().onDownloadError('Dead torrent!') - aria2.purge() @new_thread def __onDownloadError(self, api, gid): sleep(0.5) # sleep for split second to ensure proper dl gid update from onDownloadComplete dl = getDownloadByGid(gid) - download = api.get_download(gid) + download = aria2.get_download(gid) error = download.error_message LOGGER.info(f"Download Error: {error}") if dl: dl.getListener().onDownloadError(error) - aria2.purge() def start_listener(self): aria2.listen_to_notifications(threaded=True, on_download_start=self.__onDownloadStarted, diff --git a/bot/helper/mirror_utils/download_utils/mega_downloader.py b/bot/helper/mirror_utils/download_utils/mega_downloader.py index b807418..7e264c4 100644 --- a/bot/helper/mirror_utils/download_utils/mega_downloader.py +++ b/bot/helper/mirror_utils/download_utils/mega_downloader.py @@ -169,7 +169,7 @@ class MegaDownloadHelper: mname = node.getName() if listener.isTar: mname = mname + ".tar" - if listener.extract: + elif listener.extract: smsg = None else: gd = GoogleDriveHelper() @@ -184,7 +184,7 @@ class MegaDownloadHelper: if TAR_UNZIP_LIMIT is not None and (listener.isTar or listener.extract): limit = TAR_UNZIP_LIMIT msg3 = f'Failed, Tar/Unzip limit is {TAR_UNZIP_LIMIT}.\nYour File/Folder size is {get_readable_file_size(api.getSize(node))}.' - if MEGA_LIMIT is not None and limit is None: + elif MEGA_LIMIT is not None and limit is None: limit = MEGA_LIMIT msg3 = f'Failed, Mega limit is {MEGA_LIMIT}.\nYour File/Folder size is {get_readable_file_size(api.getSize(node))}.' if limit is not None: diff --git a/bot/helper/mirror_utils/download_utils/telegram_downloader.py b/bot/helper/mirror_utils/download_utils/telegram_downloader.py index 3ff9b63..795d23b 100644 --- a/bot/helper/mirror_utils/download_utils/telegram_downloader.py +++ b/bot/helper/mirror_utils/download_utils/telegram_downloader.py @@ -104,7 +104,7 @@ class TelegramDownloadHelper(DownloadHelper): LOGGER.info(f"Checking File/Folder if already in Drive...") if self.__listener.isTar: name = name + ".tar" - if self.__listener.extract: + elif self.__listener.extract: smsg = None else: gd = GoogleDriveHelper() diff --git a/bot/helper/mirror_utils/status_utils/aria_download_status.py b/bot/helper/mirror_utils/status_utils/aria_download_status.py index 394319d..f298d10 100644 --- a/bot/helper/mirror_utils/status_utils/aria_download_status.py +++ b/bot/helper/mirror_utils/status_utils/aria_download_status.py @@ -2,7 +2,6 @@ from bot import aria2, DOWNLOAD_DIR, LOGGER from bot.helper.ext_utils.bot_utils import MirrorStatus from .status import Status - def get_download(gid): return aria2.get_download(gid) @@ -24,6 +23,9 @@ class AriaDownloadStatus(Status): def __update(self): self.__download = get_download(self.__gid) + download = self.__download + if download.followed_by_ids: + self.__gid = download.followed_by_ids[0] def progress(self): """ @@ -95,13 +97,11 @@ class AriaDownloadStatus(Status): LOGGER.info(f"Cancelling Download: {self.name()}") download = self.aria_download() if download.is_waiting: - aria2.remove([download], force = True) - aria2.purge() self.__listener.onDownloadError("Cancelled by user") + aria2.remove([download], force=True) return if len(download.followed_by_ids) != 0: downloads = aria2.get_downloads(download.followed_by_ids) - aria2.remove(downloads, force = True) - self.__listener.onDownloadError("Download stopped by user!") - aria2.remove([download], force = True) - aria2.purge() + aria2.remove(downloads, force=True) + self.__listener.onDownloadError('Download stopped by user!') + aria2.remove([download], force=True) diff --git a/bot/helper/telegram_helper/message_utils.py b/bot/helper/telegram_helper/message_utils.py index e9f0392..2c2acf5 100644 --- a/bot/helper/telegram_helper/message_utils.py +++ b/bot/helper/telegram_helper/message_utils.py @@ -1,11 +1,10 @@ -from telegram import InlineKeyboardButton, InlineKeyboardMarkup -from telegram.ext import CallbackContext, CallbackQueryHandler +from telegram import InlineKeyboardMarkup from telegram.message import Message from telegram.update import Update import psutil, shutil import time from bot import AUTO_DELETE_MESSAGE_DURATION, LOGGER, bot, \ - status_reply_dict, status_reply_dict_lock, download_dict, download_dict_lock, botStartTime, dispatcher + status_reply_dict, status_reply_dict_lock, download_dict, download_dict_lock, botStartTime from bot.helper.ext_utils.bot_utils import get_readable_message, get_readable_file_size, get_readable_time, MirrorStatus from telegram.error import TimedOut, BadRequest @@ -14,7 +13,7 @@ def sendMessage(text: str, bot, update: Update): try: return bot.send_message(update.message.chat_id, reply_to_message_id=update.message.message_id, - text=text, allow_sending_without_reply=True, parse_mode='HTMl') + text=text, allow_sending_without_reply=True, parse_mode='HTMl') except Exception as e: LOGGER.error(str(e)) def sendMarkup(text: str, bot, update: Update, reply_markup: InlineKeyboardMarkup): @@ -68,7 +67,13 @@ def delete_all_messages(): def update_all_messages(): + total, used, free = shutil.disk_usage('.') + free = get_readable_file_size(free) + currentTime = get_readable_time(time.time() - botStartTime) msg = get_readable_message() + msg += f"CPU: {psutil.cpu_percent()}%" \ + f" RAM: {psutil.virtual_memory().percent}%" \ + f" DISK: {psutil.disk_usage('/').percent}%" with download_dict_lock: dlspeed_bytes = 0 uldl_bytes = 0 @@ -86,22 +91,27 @@ def update_all_messages(): uldl_bytes += float(speedy.split('M')[0]) * 1048576 dlspeed = get_readable_file_size(dlspeed_bytes) ulspeed = get_readable_file_size(uldl_bytes) - msg += f"DL: {dlspeed}ps 🔻 | UL: {ulspeed}ps 🔺" + msg += f"\nFREE: {free} | UPTIME: {currentTime}\nDL: {dlspeed}ps 🔻 | UL: {ulspeed}ps 🔺\n" with status_reply_dict_lock: for chat_id in list(status_reply_dict.keys()): if status_reply_dict[chat_id] and msg != status_reply_dict[chat_id].text: if len(msg) == 0: msg = "Starting DL" try: - keyboard = [[InlineKeyboardButton("♻️ Stats", callback_data="stats_")]] - editMessage(msg, status_reply_dict[chat_id], reply_markup=InlineKeyboardMarkup(keyboard)) + editMessage(msg, status_reply_dict[chat_id]) except Exception as e: LOGGER.error(str(e)) status_reply_dict[chat_id].text = msg def sendStatusMessage(msg, bot): + total, used, free = shutil.disk_usage('.') + free = get_readable_file_size(free) + currentTime = get_readable_time(time.time() - botStartTime) progress = get_readable_message() + progress += f"CPU: {psutil.cpu_percent()}%" \ + f" RAM: {psutil.virtual_memory().percent}%" \ + f" DISK: {psutil.disk_usage('/').percent}%" with download_dict_lock: dlspeed_bytes = 0 uldl_bytes = 0 @@ -119,7 +129,7 @@ def sendStatusMessage(msg, bot): uldl_bytes += float(speedy.split('M')[0]) * 1048576 dlspeed = get_readable_file_size(dlspeed_bytes) ulspeed = get_readable_file_size(uldl_bytes) - progress += f"DL: {dlspeed}ps 🔻 | UL: {ulspeed}ps 🔺" + progress += f"\nFREE: {free} | UPTIME: {currentTime}\nDL: {dlspeed}ps 🔻 | UL: {ulspeed}ps 🔺\n" with status_reply_dict_lock: if msg.message.chat.id in list(status_reply_dict.keys()): try: @@ -134,27 +144,3 @@ def sendStatusMessage(msg, bot): progress = "Starting DL" message = sendMessage(progress, bot, msg) status_reply_dict[msg.message.chat.id] = message - - -def pop_up_stats(update, context): - query = update.callback_query - stats = bot_sys_stats() - query.answer(text=stats, show_alert=True) - -def bot_sys_stats(): - currentTime = get_readable_time(time.time() - botStartTime) - cpu = psutil.cpu_percent() - mem = psutil.virtual_memory().percent - disk = psutil.disk_usage("/").percent - total, used, free = shutil.disk_usage('.') - free = get_readable_file_size(free) - stats = f""" -Bot Uptime: {currentTime} -Free Disk: {free} -CPU: {cpu}% -RAM: {mem}% -DISK: {disk}% -""" - return stats - -dispatcher.add_handler(CallbackQueryHandler(pop_up_stats, pattern="stats_")) diff --git a/bot/modules/mirror.py b/bot/modules/mirror.py index f72a81c..3fcf05b 100644 --- a/bot/modules/mirror.py +++ b/bot/modules/mirror.py @@ -2,7 +2,7 @@ import requests from telegram.ext import CommandHandler from telegram import InlineKeyboardMarkup -from bot import Interval, INDEX_URL, BUTTON_FOUR_NAME, BUTTON_FOUR_URL, BUTTON_FIVE_NAME, BUTTON_FIVE_URL, BUTTON_SIX_NAME, BUTTON_SIX_URL, BLOCK_MEGA_FOLDER, BLOCK_MEGA_LINKS, VIEW_LINK +from bot import Interval, INDEX_URL, BUTTON_FOUR_NAME, BUTTON_FOUR_URL, BUTTON_FIVE_NAME, BUTTON_FIVE_URL, BUTTON_SIX_NAME, BUTTON_SIX_URL, BLOCK_MEGA_FOLDER, BLOCK_MEGA_LINKS, VIEW_LINK, aria2 from bot import dispatcher, DOWNLOAD_DIR, DOWNLOAD_STATUS_UPDATE_INTERVAL, download_dict, download_dict_lock, SHORTENER, SHORTENER_API, TAR_UNZIP_LIMIT from bot.helper.ext_utils import fs_utils, bot_utils from bot.helper.ext_utils.bot_utils import setInterval, get_mega_link_type @@ -50,6 +50,7 @@ class MirrorListener(listeners.MirrorListeners): def clean(self): try: + aria2.purge() Interval[0].cancel() del Interval[0] delete_all_messages()