From 41507067bed64f9fac3d7b7b81204fc959e7b710 Mon Sep 17 00:00:00 2001 From: Davte Date: Wed, 14 Feb 2024 19:30:35 +0100 Subject: [PATCH] `download_file` method updated to handle exceptions and return information about downloaded file --- davtelepot/__init__.py | 2 +- davtelepot/bot.py | 17 +++++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/davtelepot/__init__.py b/davtelepot/__init__.py index a1dae00..0802b74 100644 --- a/davtelepot/__init__.py +++ b/davtelepot/__init__.py @@ -11,7 +11,7 @@ __author__ = "Davide Testa" __email__ = "davide@davte.it" __credits__ = ["Marco Origlia", "Nick Lee @Nickoala"] __license__ = "GNU General Public License v3.0" -__version__ = "2.9.8" +__version__ = "2.9.9" __maintainer__ = "Davide Testa" __contact__ = "t.me/davte" diff --git a/davtelepot/bot.py b/davtelepot/bot.py index e3b4234..62fd491 100644 --- a/davtelepot/bot.py +++ b/davtelepot/bot.py @@ -2137,7 +2137,7 @@ class Bot(TelegramBot, ObjectWithDatabase, MultiLanguageObject): file = await self.getFile(file_id=file_id) if file is None or isinstance(file, Exception): logging.error(f"{file}") - return + return file file_bytes = await async_get( url=( f"https://api.telegram.org/file/" @@ -2147,16 +2147,21 @@ class Bot(TelegramBot, ObjectWithDatabase, MultiLanguageObject): mode='raw' ) path = path or self.path - while file_name is None: + if file_name is None: file_name = get_secure_key(length=10) - if os.path.exists(f"{path}/{file_name}"): - file_name = None + file_complete_path = os.path.join(path, file_name) + while os.path.exists(file_complete_path): + file_complete_path = file_complete_path + '1' try: - with open(f"{path}/{file_name}", 'wb') as local_file: + with open(file_complete_path, 'wb') as local_file: local_file.write(file_bytes) except Exception as e: logging.error(f"File download failed due to {e}") - return + return e + return dict(file_id=file_id, + file_name=file_name, + path=path, + file_complete_path=file_complete_path) def translate_inline_query_answer_result(self, record, update=None, user_record=None):