diff --git a/bot/__init__.py b/bot/__init__.py index 57ea6d8..5186248 100644 --- a/bot/__init__.py +++ b/bot/__init__.py @@ -17,7 +17,7 @@ def getConfig(name: str): LOGGER = logging.getLogger(__name__) try: - if bool(config['DEFAULT']['_____REMOVE_THIS_LINE_____']): + if bool(getConfig('_____REMOVE_THIS_LINE_____')): logging.ERROR('The README.md file there to be read! Exiting now!') exit() except KeyError: diff --git a/bot/helper/bot_utils.py b/bot/helper/bot_utils.py index cbd4d4f..e863379 100644 --- a/bot/helper/bot_utils.py +++ b/bot/helper/bot_utils.py @@ -1,8 +1,10 @@ from bot import download_dict +import logging + +LOGGER = logging.getLogger(__name__) class MirrorStatus: - STATUS_UPLOADING = "Uploading" STATUS_DOWNLOADING = "Downloading" STATUS_WAITING = "Queued" @@ -37,10 +39,10 @@ def get_download_status_list(): def get_progress_bar_string(status): if status.status() == MirrorStatus.STATUS_UPLOADING: - completed = status.uploaded_bytes/8 + completed = status.uploaded_bytes / 8 else: - completed = status.download().completed_length/8 - total = status.download().total_length/8 + completed = status.download().completed_length / 8 + total = status.download().total_length / 8 if total == 0: p = 0 else: @@ -48,10 +50,10 @@ def get_progress_bar_string(status): p = min(max(p, 0), 100) cFull = p // 8 cPart = p % 8 - 1 - p_str = '█'*cFull + p_str = '█' * cFull if cPart >= 0: - p_str += PROGRESS_INCOMPLETE[cPart] - p_str += ' '*(PROGRESS_MAX_SIZE - cFull) + p_str += PROGRESS_INCOMPLETE[cPart] + p_str += ' ' * (PROGRESS_MAX_SIZE - cFull) p_str = f"[{p_str}]" return p_str @@ -77,6 +79,7 @@ def get_readable_message(progress_list: list = download_dict.values()): msg += f'Name: {status.name()}\n' \ f'status: {status.status()}\n' \ f'{get_progress_bar_string(status)} {status.progress()} of {status.size()}\n' \ - f'Speed: {status.speed()}\n' \ - f'ETA: {status.eta()}\n' - return msg \ No newline at end of file + f'Speed: {status.speed()}\n' \ + f'ETA: {status.eta()}\n' + LOGGER.info(msg) + return msg diff --git a/bot/helper/download_tools.py b/bot/helper/download_tools.py index f377a4d..b889b1a 100644 --- a/bot/helper/download_tools.py +++ b/bot/helper/download_tools.py @@ -2,6 +2,7 @@ from time import sleep from bot import DOWNLOAD_DIR, DOWNLOAD_STATUS_UPDATE_INTERVAL, aria2 from .download_status import DownloadStatus from .bot_utils import * +from .exceptions import KillThreadException class DownloadHelper: diff --git a/bot/helper/exceptions.py b/bot/helper/exceptions.py index 0710922..77f0ccf 100644 --- a/bot/helper/exceptions.py +++ b/bot/helper/exceptions.py @@ -1,8 +1,9 @@ class DriveAuthError(Exception): pass -# Custom Exception class for killing thread as soon as they aren't needed + class KillThreadException(Exception): + """ Custom Exception class for killing thread as soon as they aren't needed""" def __init__(self, message, error=None): super().__init__(message) self.error = error diff --git a/bot/helper/gdriveTools.py b/bot/helper/gdriveTools.py index f741994..f4bc4fd 100644 --- a/bot/helper/gdriveTools.py +++ b/bot/helper/gdriveTools.py @@ -4,11 +4,12 @@ from google.auth.transport.requests import Request from googleapiclient.http import MediaFileUpload import pickle import os +import time +import logging from bot import LOGGER, parent_id, DOWNLOAD_DIR from .fs_utils import get_mime_type from .bot_utils import * -import time -import logging +from .exceptions import KillThreadException logging.getLogger('googleapiclient.discovery').setLevel(logging.ERROR) @@ -32,7 +33,8 @@ class GoogleDriveHelper: # File body description media_body = MediaFileUpload(file_path, mimetype=mime_type, - resumable=True) + resumable=True, + chunksize=1024*1024) file_metadata = { 'name': file_name, 'description': 'mirror', @@ -54,18 +56,23 @@ class GoogleDriveHelper: _list = get_download_status_list() index = get_download_index(_list, get_download(self.__listener.message.message_id).gid) uploaded_bytes = 0 + should_update = True while response is None: status, response = drive_file.next_chunk() time_lapsed = time.time() - self.start_time if status: # The iconic formula of speed = distance / time :) - LOGGER.info(status.progress() * 100) + LOGGER.info(f'{file_name}: {status.progress() * 100}') chunk_size = status.total_size*status.progress() - uploaded_bytes uploaded_bytes = status.total_size*status.progress() download_dict[self.__listener.uid].uploaded_bytes += chunk_size download_dict[self.__listener.uid].upload_time = time_lapsed - self.__listener.onUploadProgress(_list, index) + if should_update: + try: + self.__listener.onUploadProgress(_list, index) + except KillThreadException: + should_update = False # Insert new permissions self.__service.permissions().create(fileId=response['id'], body=permissions).execute() # Define file instance and get url for download diff --git a/bot/mirror.py b/bot/mirror.py index 5a4759d..c378dde 100644 --- a/bot/mirror.py +++ b/bot/mirror.py @@ -67,7 +67,10 @@ class MirrorListener(listeners.MirrorListeners): def onUploadProgress(self, progress: list, index: int): msg = get_readable_message(progress) - editMessage(msg, self.context, self.reply_message) + try: + editMessage(msg, self.context, self.reply_message) + except BadRequest: + raise KillThreadException('Message deleted. Do not call this method from the thread') @run_async