From 9a8e2a775b5e0adbf218387c402548cef3df72e3 Mon Sep 17 00:00:00 2001 From: singh-pratyush96 Date: Thu, 4 Aug 2016 15:47:22 +0530 Subject: [PATCH 1/2] Added option --max-sleep-interval to support random sleep between downloads. Fixes #9930 --- youtube_dl/YoutubeDL.py | 1 + youtube_dl/__init__.py | 7 +++++++ youtube_dl/downloader/common.py | 7 +++++-- youtube_dl/options.py | 5 +++++ 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/youtube_dl/YoutubeDL.py b/youtube_dl/YoutubeDL.py index 6551f086f..eb69e08e5 100755 --- a/youtube_dl/YoutubeDL.py +++ b/youtube_dl/YoutubeDL.py @@ -250,6 +250,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. + max_sleep_interval:Max number of random seconds to 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 diff --git a/youtube_dl/__init__.py b/youtube_dl/__init__.py index 2b34bf9c2..86af18d33 100644 --- a/youtube_dl/__init__.py +++ b/youtube_dl/__init__.py @@ -145,6 +145,12 @@ def _real_main(argv=None): if numeric_limit is None: parser.error('invalid max_filesize specified') opts.max_filesize = numeric_limit + if opts.sleep_interval is not None: + if opts.sleep_interval < 0: + parser.error('sleep interval should not be negative') + elif opts.max_sleep_interval is not None: + if opts.max_sleep_interval < opts.sleep_interval: + parser.error('max sleep interval should not be less than sleep interval') def parse_retries(retries): if retries in ('inf', 'infinite'): @@ -370,6 +376,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 1dba9f49a..5cea1b4e3 100644 --- a/youtube_dl/downloader/common.py +++ b/youtube_dl/downloader/common.py @@ -4,6 +4,7 @@ import os import re import sys import time +import random from ..compat import compat_os_name from ..utils import ( @@ -342,8 +343,10 @@ class FileDownloader(object): }) return True - sleep_interval = self.params.get('sleep_interval') - if sleep_interval: + sleep_lower_bound = self.params.get('sleep_interval') + if sleep_lower_bound: + sleep_upper_bound = self.params.get('max_sleep_interval') if self.params.get('max_sleep_interval') else sleep_lower_bound + sleep_interval = random.uniform(sleep_lower_bound, sleep_upper_bound) self.to_screen('[download] Sleeping %s seconds...' % sleep_interval) time.sleep(sleep_interval) diff --git a/youtube_dl/options.py b/youtube_dl/options.py index 942d44912..46f250659 100644 --- a/youtube_dl/options.py +++ b/youtube_dl/options.py @@ -502,6 +502,11 @@ def parseOpts(overrideArguments=None): '--sleep-interval', metavar='SECONDS', 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='Max number of random seconds to sleep.' + ) verbosity = optparse.OptionGroup(parser, 'Verbosity / Simulation Options') verbosity.add_option( From 23759afb39c705a8757be8558da99bc4a8fea26c Mon Sep 17 00:00:00 2001 From: singh-pratyush96 Date: Fri, 5 Aug 2016 09:21:23 +0530 Subject: [PATCH 2/2] Added alias --min-sleep-interval and improved description of sleep arguments. --- youtube_dl/YoutubeDL.py | 7 +++++-- youtube_dl/downloader/common.py | 2 +- youtube_dl/options.py | 9 ++++++--- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/youtube_dl/YoutubeDL.py b/youtube_dl/YoutubeDL.py index eb69e08e5..82b77783d 100755 --- a/youtube_dl/YoutubeDL.py +++ b/youtube_dl/YoutubeDL.py @@ -249,8 +249,11 @@ class YoutubeDL(object): source_address: (Experimental) Client-side IP address to bind to. 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. - max_sleep_interval:Max number of random seconds to sleep. + sleep_interval: Minimum number of seconds to sleep before each download. + Sleep will be for a random interval if --max-sleep-interval is also passed. + max_sleep_interval:Max number of seconds to sleep before each download. + Sleep will be for a random interval if passed along with --min-sleep-interval + or --sleep-interval, otherwise ignored. 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 diff --git a/youtube_dl/downloader/common.py b/youtube_dl/downloader/common.py index 5cea1b4e3..8e377c72c 100644 --- a/youtube_dl/downloader/common.py +++ b/youtube_dl/downloader/common.py @@ -345,7 +345,7 @@ class FileDownloader(object): sleep_lower_bound = self.params.get('sleep_interval') if sleep_lower_bound: - sleep_upper_bound = self.params.get('max_sleep_interval') if self.params.get('max_sleep_interval') else sleep_lower_bound + sleep_upper_bound = self.params.get('max_sleep_interval', sleep_lower_bound) sleep_interval = random.uniform(sleep_lower_bound, sleep_upper_bound) self.to_screen('[download] Sleeping %s seconds...' % sleep_interval) time.sleep(sleep_interval) diff --git a/youtube_dl/options.py b/youtube_dl/options.py index 46f250659..068e824a0 100644 --- a/youtube_dl/options.py +++ b/youtube_dl/options.py @@ -499,13 +499,16 @@ def parseOpts(overrideArguments=None): dest='bidi_workaround', action='store_true', help='Work around terminals that lack bidirectional text support. Requires bidiv or fribidi executable in PATH') workarounds.add_option( - '--sleep-interval', metavar='SECONDS', + '--sleep-interval', '--min-sleep-interval', metavar='SECONDS', dest='sleep_interval', type=float, - help='Number of seconds to sleep before each download.') + help='Minimum number of seconds to sleep before each download. Sleep will be for a random interval if ' + '--max-sleep-interval is also passed.' + ) workarounds.add_option( '--max-sleep-interval', metavar='SECONDS', dest='max_sleep_interval', type=float, - help='Max number of random seconds to sleep.' + help='Max number of seconds to sleep before each download. Sleep will be for a random interval if passed' + ' along with --min-sleep-interval or --sleep-interval, otherwise ignored.' ) verbosity = optparse.OptionGroup(parser, 'Verbosity / Simulation Options')