From b6e542a3df19d973ee6a406c67fac1dd8b5b5947 Mon Sep 17 00:00:00 2001 From: singh-pratyush96 Date: Mon, 25 Jul 2016 01:51:52 +0530 Subject: [PATCH] Added random sleep for given interval range. Fixes #9930 --- youtube_dl/YoutubeDL.py | 1 + youtube_dl/__init__.py | 1 + youtube_dl/downloader/common.py | 13 +++++++++++-- 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..10dc86038 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: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 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 1dba9f49a..075c425be 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 ( @@ -344,8 +345,16 @@ class FileDownloader(object): sleep_interval = self.params.get('sleep_interval') if sleep_interval: - self.to_screen('[download] Sleeping %s seconds...' % sleep_interval) - time.sleep(sleep_interval) + min_sleep_interval = sleep_interval + max_sleep_interval = self.params.get('max_sleep_interval') or min_sleep_interval + + sleep_time = random.uniform( + min_sleep_interval, + max_sleep_interval + ) + + self.to_screen('[download] Sleeping %s seconds...' % sleep_time) + time.sleep(sleep_time) return self.real_download(filename, info_dict) diff --git a/youtube_dl/options.py b/youtube_dl/options.py index c4a85b2c0..8c6207219 100644 --- a/youtube_dl/options.py +++ b/youtube_dl/options.py @@ -491,6 +491,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='Maximum sleep time for random sleep.' + ) verbosity = optparse.OptionGroup(parser, 'Verbosity / Simulation Options') verbosity.add_option(