From 49fe46bad344c05410b88620158e3ff16c69563d Mon Sep 17 00:00:00 2001 From: lzzy12 Date: Sun, 22 Mar 2020 14:45:53 +0530 Subject: [PATCH] Add gid method in Status class for download classes Signed-off-by: lzzy12 --- bot/helper/ext_utils/bot_utils.py | 12 +++++---- .../download_utils/telegram_downloader.py | 25 +++++++++++-------- .../status_utils/aria_download_status.py | 4 +++ .../status_utils/telegram_download_status.py | 5 +++- 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/bot/helper/ext_utils/bot_utils.py b/bot/helper/ext_utils/bot_utils.py index 5e42b9a..ede4e49 100644 --- a/bot/helper/ext_utils/bot_utils.py +++ b/bot/helper/ext_utils/bot_utils.py @@ -1,9 +1,10 @@ -from bot import download_dict, download_dict_lock import logging import re import threading import time +from bot import download_dict, download_dict_lock + LOGGER = logging.getLogger(__name__) MAGNET_REGEX = r"magnet:\?xt=urn:btih:[a-zA-Z0-9]*" @@ -54,11 +55,12 @@ def get_readable_file_size(size_in_bytes) -> str: except IndexError: return 'File too large' + def getDownloadByGid(gid): with download_dict_lock: for dl in download_dict.values(): if dl.status() == MirrorStatus.STATUS_DOWNLOADING: - if dl.download().gid == gid: + if dl.gid() == gid: return dl return None @@ -97,13 +99,13 @@ def get_readable_message(): msg += download.status() if download.status() != MirrorStatus.STATUS_ARCHIVING: msg += f"\n{get_progress_bar_string(download)} {download.progress()} of " \ - f"{download.size()}" \ - f" at {download.speed()}, ETA: {download.eta()} " + f"{download.size()}" \ + f" at {download.speed()}, ETA: {download.eta()} " if download.status() == MirrorStatus.STATUS_DOWNLOADING: if hasattr(download, 'is_torrent'): msg += f"| P: {download.download().connections} " \ f"| S: {download.download().num_seeders}" - msg += f"\nGID: {download.download().gid}" + msg += f"\nGID: {download.gid()}" msg += "\n\n" return msg diff --git a/bot/helper/mirror_utils/download_utils/telegram_downloader.py b/bot/helper/mirror_utils/download_utils/telegram_downloader.py index fa3b460..8f8a671 100644 --- a/bot/helper/mirror_utils/download_utils/telegram_downloader.py +++ b/bot/helper/mirror_utils/download_utils/telegram_downloader.py @@ -1,15 +1,17 @@ -from .download_helper import DownloadHelper import threading import time -from ..status_utils.telegram_download_status import TelegramDownloadStatus -from bot.helper.ext_utils.bot_utils import get_readable_file_size -from bot import LOGGER, bot, download_dict, download_dict_lock, TELEGRAM_API,\ - TELEGRAM_HASH, USER_SESSION_STRING + from pyrogram import Client +from bot import LOGGER, bot, download_dict, download_dict_lock, TELEGRAM_API, \ + TELEGRAM_HASH, USER_SESSION_STRING +from .download_helper import DownloadHelper +from ..status_utils.telegram_download_status import TelegramDownloadStatus + global_lock = threading.Lock() GLOBAL_GID = set() + class TelegramDownloadHelper(DownloadHelper): def __init__(self, listener): super().__init__() @@ -19,8 +21,8 @@ class TelegramDownloadHelper(DownloadHelper): self.__gid = '' self.__start_time = time.time() self.__user_bot = Client(api_id=TELEGRAM_API, - api_hash=TELEGRAM_HASH, - session_name=USER_SESSION_STRING) + api_hash=TELEGRAM_HASH, + session_name=USER_SESSION_STRING) self.__user_bot.start() @property @@ -51,15 +53,16 @@ class TelegramDownloadHelper(DownloadHelper): self.progress = current / self.size * 100 except ZeroDivisionError: return 0 + def __onDownloadComplete(self): - + self.__listener.onDownloadComplete() def __download(self, message, path): self.__user_bot.download_media(message, - progress=self.__onDownloadProgress, file_name=path) + progress=self.__onDownloadProgress, file_name=path) self.__onDownloadComplete() - + def add_download(self, message, path): if message.chat.type == "private": _message = self.__user_bot.get_messages(bot.get_me().id, message.message_id) @@ -70,7 +73,7 @@ class TelegramDownloadHelper(DownloadHelper): with global_lock: # For avoiding locking the thread lock for long time unnecessarily download = media.file_id not in GLOBAL_GID - + if download: self.__onDownloadStart(media.file_name, media.file_size, media.file_id) LOGGER.info(media.file_id) 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 bfe81f1..52dc0ca 100644 --- a/bot/helper/mirror_utils/status_utils/aria_download_status.py +++ b/bot/helper/mirror_utils/status_utils/aria_download_status.py @@ -74,3 +74,7 @@ class AriaDownloadStatus(Status): def uid(self): return self.__uid + + def gid(self): + self.__update() + return self.__gid diff --git a/bot/helper/mirror_utils/status_utils/telegram_download_status.py b/bot/helper/mirror_utils/status_utils/telegram_download_status.py index 186ac96..846aa04 100644 --- a/bot/helper/mirror_utils/status_utils/telegram_download_status.py +++ b/bot/helper/mirror_utils/status_utils/telegram_download_status.py @@ -1,6 +1,6 @@ +from bot import DOWNLOAD_DIR from bot.helper.ext_utils.bot_utils import MirrorStatus, get_readable_file_size, get_readable_time from .status import Status -from bot import DOWNLOAD_DIR class TelegramDownloadStatus(Status): @@ -8,6 +8,9 @@ class TelegramDownloadStatus(Status): self.obj = obj self.uid = uid + def gid(self): + return self.obj.gid + def path(self): return f"{DOWNLOAD_DIR}{self.uid}"