download_file
method updated to handle exceptions and return information about downloaded file
This commit is contained in:
parent
ec747bef1d
commit
41507067be
@ -11,7 +11,7 @@ __author__ = "Davide Testa"
|
|||||||
__email__ = "davide@davte.it"
|
__email__ = "davide@davte.it"
|
||||||
__credits__ = ["Marco Origlia", "Nick Lee @Nickoala"]
|
__credits__ = ["Marco Origlia", "Nick Lee @Nickoala"]
|
||||||
__license__ = "GNU General Public License v3.0"
|
__license__ = "GNU General Public License v3.0"
|
||||||
__version__ = "2.9.8"
|
__version__ = "2.9.9"
|
||||||
__maintainer__ = "Davide Testa"
|
__maintainer__ = "Davide Testa"
|
||||||
__contact__ = "t.me/davte"
|
__contact__ = "t.me/davte"
|
||||||
|
|
||||||
|
@ -2137,7 +2137,7 @@ class Bot(TelegramBot, ObjectWithDatabase, MultiLanguageObject):
|
|||||||
file = await self.getFile(file_id=file_id)
|
file = await self.getFile(file_id=file_id)
|
||||||
if file is None or isinstance(file, Exception):
|
if file is None or isinstance(file, Exception):
|
||||||
logging.error(f"{file}")
|
logging.error(f"{file}")
|
||||||
return
|
return file
|
||||||
file_bytes = await async_get(
|
file_bytes = await async_get(
|
||||||
url=(
|
url=(
|
||||||
f"https://api.telegram.org/file/"
|
f"https://api.telegram.org/file/"
|
||||||
@ -2147,16 +2147,21 @@ class Bot(TelegramBot, ObjectWithDatabase, MultiLanguageObject):
|
|||||||
mode='raw'
|
mode='raw'
|
||||||
)
|
)
|
||||||
path = path or self.path
|
path = path or self.path
|
||||||
while file_name is None:
|
if file_name is None:
|
||||||
file_name = get_secure_key(length=10)
|
file_name = get_secure_key(length=10)
|
||||||
if os.path.exists(f"{path}/{file_name}"):
|
file_complete_path = os.path.join(path, file_name)
|
||||||
file_name = None
|
while os.path.exists(file_complete_path):
|
||||||
|
file_complete_path = file_complete_path + '1'
|
||||||
try:
|
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)
|
local_file.write(file_bytes)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"File download failed due to {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,
|
def translate_inline_query_answer_result(self, record,
|
||||||
update=None, user_record=None):
|
update=None, user_record=None):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user