Queer European MD passionate about IT
Prechádzať zdrojové kódy

Allow to overwrite file when using bot.download_file method

Davte 11 mesiacov pred
rodič
commit
a3b28bc1d6
2 zmenil súbory, kde vykonal 6 pridanie a 4 odobranie
  1. 1 1
      davtelepot/__init__.py
  2. 5 3
      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.11"
+__version__ = "2.9.12"
 __maintainer__ = "Davide Testa"
 __contact__ = "t.me/davte"
 

+ 5 - 3
davtelepot/bot.py

@@ -2127,7 +2127,8 @@ class Bot(TelegramBot, ObjectWithDatabase, MultiLanguageObject):
         return sent_update
 
     async def download_file(self, file_id,
-                            file_name=None, path=None):
+                            file_name=None, path=None,
+                            prevent_overwriting: bool = False):
         """Given a telegram `file_id`, download the related file.
 
         Telegram may not preserve the original file name and MIME type: the
@@ -2151,8 +2152,9 @@ class Bot(TelegramBot, ObjectWithDatabase, MultiLanguageObject):
             if file_name is None:
                 file_name = get_secure_key(length=10)
             file_complete_path = os.path.join(path, file_name)
-            while os.path.exists(file_complete_path):
-                file_complete_path = file_complete_path + '1'
+            if prevent_overwriting:
+                while os.path.exists(file_complete_path):
+                    file_complete_path = file_complete_path + '1'
             try:
                 with open(file_complete_path, 'wb') as local_file:
                     local_file.write(file_bytes)