download_file method updated to handle exceptions and return information about downloaded file

This commit is contained in:
Davte 2024-02-14 19:30:35 +01:00
parent ec747bef1d
commit 41507067be
Signed by: Davte
GPG Key ID: 70336F92E6814706
2 changed files with 12 additions and 7 deletions

View File

@ -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"

View File

@ -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):