diff --git a/youtube_dl/YoutubeDL.py b/youtube_dl/YoutubeDL.py index 617b00cf3..7e65f305a 100755 --- a/youtube_dl/YoutubeDL.py +++ b/youtube_dl/YoutubeDL.py @@ -76,6 +76,7 @@ from .utils import ( std_headers, subtitles_filename, UnavailableVideoError, + InvalidSleepTimeError, url_basename, version_tuple, write_json_file, @@ -250,10 +251,7 @@ class YoutubeDL(object): call_home: Boolean, true iff we are allowed to contact the youtube-dl servers for debugging. sleep_interval: Number of seconds to sleep before each download. - Accepts range for random sleep in formats: - - to - - to - where limits are in seconds + max_sleep_interval Upper bound of number of seconds for random sleep. listformats: Print an overview of available video formats and exit. list_thumbnails: Print a table of all thumbnails and exit. match_filter: A function that gets called with the info_dict of @@ -1782,6 +1780,8 @@ class YoutubeDL(object): except MaxDownloadsReached: self.to_screen('[info] Maximum number of downloaded files reached.') raise + except InvalidSleepTimeError: + self.report_error('Invalid argument for sleep') else: if self.params.get('dump_single_json', False): self.to_stdout(json.dumps(res)) diff --git a/youtube_dl/__init__.py b/youtube_dl/__init__.py index 2b34bf9c2..309f156da 100644 --- a/youtube_dl/__init__.py +++ b/youtube_dl/__init__.py @@ -370,6 +370,7 @@ def _real_main(argv=None): 'source_address': opts.source_address, 'call_home': opts.call_home, 'sleep_interval': opts.sleep_interval, + 'max_sleep_interval': opts.max_sleep_interval, 'external_downloader': opts.external_downloader, 'list_thumbnails': opts.list_thumbnails, 'playlist_items': opts.playlist_items, diff --git a/youtube_dl/downloader/common.py b/youtube_dl/downloader/common.py index 5c3a5ca06..8747186c5 100644 --- a/youtube_dl/downloader/common.py +++ b/youtube_dl/downloader/common.py @@ -13,6 +13,7 @@ from ..utils import ( decodeArgument, format_bytes, timeconvert, + InvalidSleepTimeError ) @@ -345,16 +346,22 @@ class FileDownloader(object): sleep_interval = self.params.get('sleep_interval') if sleep_interval: - sleep_interval_lower = sleep_interval.lower() + min_sleep_interval = sleep_interval + max_sleep_interval = self.params.get('max_sleep_interval') - if 'to' in sleep_interval_lower: - lower_sleep_limit, upper_sleep_limit = map(float, sleep_interval_lower.split('to')) - sleep_time = random.uniform(lower_sleep_limit, upper_sleep_limit) - else: - sleep_time = float(sleep_interval) + try: + if max_sleep_interval: + sleep_time = random.uniform( + float(min_sleep_interval), + float(max_sleep_interval) + ) + else: + sleep_time = float(min_sleep_interval) - self.to_screen('[download] Sleeping %s seconds...' % sleep_time) - time.sleep(sleep_time) + self.to_screen('[download] Sleeping %s seconds...' % sleep_time) + time.sleep(sleep_time) + except (ValueError, IOError) as err: + raise InvalidSleepTimeError() return self.real_download(filename, info_dict) diff --git a/youtube_dl/options.py b/youtube_dl/options.py index 18a68867d..290e973d7 100644 --- a/youtube_dl/options.py +++ b/youtube_dl/options.py @@ -489,8 +489,14 @@ def parseOpts(overrideArguments=None): help='Work around terminals that lack bidirectional text support. Requires bidiv or fribidi executable in PATH') workarounds.add_option( '--sleep-interval', metavar='SECONDS', - dest='sleep_interval', type=str, - help='Number of seconds to sleep before each download.') + dest='sleep_interval', type=float, + help='Number of seconds to sleep before each download.', + ) + workarounds.add_option( + '--max-sleep-interval', metavar='SECONDS', + dest='max_sleep_interval', type=float, + help='Maximum sleep time for random sleep.' + ) verbosity = optparse.OptionGroup(parser, 'Verbosity / Simulation Options') verbosity.add_option( diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py index f5cd6819b..ad46277a4 100644 --- a/youtube_dl/utils.py +++ b/youtube_dl/utils.py @@ -752,6 +752,15 @@ class UnavailableVideoError(Exception): pass +class InvalidSleepTimeError(Exception): + """Invalid sleep time provided exception + + This exception will be thrown when user provides invalid + time for sleep in --sleep-interval or --max-sleep-interval. + """ + pass + + class ContentTooShortError(Exception): """Content Too Short exception.