[core] Make `--max-downloads ...` stop immediately on reaching the limit
authordirkf <fieldhouse@gmx.net>
Wed, 10 Aug 2022 14:37:59 +0000 (15:37 +0100)
committerdirkf <fieldhouse@gmx.net>
Wed, 10 Aug 2022 14:37:59 +0000 (15:37 +0100)
Based on and closes #26638.

youtube_dl/YoutubeDL.py

index 3895b408fc096e09400df48d43861fe49212bf0f..e77b8d50cff9e7e82ac9b1ac57ce9674db82dd93 100755 (executable)
@@ -1779,10 +1779,9 @@ class YoutubeDL(object):
 
         assert info_dict.get('_type', 'video') == 'video'
 
-        max_downloads = self.params.get('max_downloads')
-        if max_downloads is not None:
-            if self._num_downloads >= int(max_downloads):
-                raise MaxDownloadsReached()
+        max_downloads = int_or_none(self.params.get('max_downloads')) or float('inf')
+        if self._num_downloads >= max_downloads:
+            raise MaxDownloadsReached()
 
         # TODO: backward compatibility, to be removed
         info_dict['fulltitle'] = info_dict['title']
@@ -2062,6 +2061,9 @@ class YoutubeDL(object):
                     self.report_error('postprocessing: %s' % str(err))
                     return
                 self.record_download_archive(info_dict)
+                # avoid possible nugatory search for further items (PR #26638)
+                if self._num_downloads >= max_downloads:
+                    raise MaxDownloadsReached()
 
     def download(self, url_list):
         """Download a given list of URLs."""