Queer European MD passionate about IT
Browse Source

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

Davte 1 year ago
parent
commit
41507067be
2 changed files with 12 additions and 7 deletions
  1. 1 1
      davtelepot/__init__.py
  2. 11 6
      davtelepot/bot.py

+ 1 - 1
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"
 

+ 11 - 6
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):