From 63000dd01ff78c1ee31d1d15dcbc8f6eb4e29540 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 --- README.md | 7 +++++-- youtube_dl/YoutubeDL.py | 6 +++++- youtube_dl/downloader/common.py | 13 +++++++++++-- youtube_dl/options.py | 2 +- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index a9f3001a6..4820c9639 100644 --- a/README.md +++ b/README.md @@ -329,8 +329,11 @@ which means you can modify it, redistribute it or use it however you like. --bidi-workaround Work around terminals that lack bidirectional text support. Requires bidiv or fribidi executable in PATH - --sleep-interval SECONDS Number of seconds to sleep before each - download. + --sleep-interval SECONDS/RANGE Number of seconds to sleep before each + download. Also accepts random sleep in + format to and + to, where limits + are in seconds. ## Video Format Options: -f, --format FORMAT Video format code, see the "FORMAT diff --git a/youtube_dl/YoutubeDL.py b/youtube_dl/YoutubeDL.py index 6551f086f..617b00cf3 100755 --- a/youtube_dl/YoutubeDL.py +++ b/youtube_dl/YoutubeDL.py @@ -250,6 +250,10 @@ 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 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 @@ -1684,7 +1688,7 @@ class YoutubeDL(object): except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err: self.report_error('unable to download video data: %s' % error_to_compat_str(err)) return - except (OSError, IOError) as err: + except (OSError, IOError, ValueError) as err: raise UnavailableVideoError(err) except (ContentTooShortError, ) as err: self.report_error('content too short (expected %s bytes and served %s)' % (err.expected, err.downloaded)) diff --git a/youtube_dl/downloader/common.py b/youtube_dl/downloader/common.py index 1dba9f49a..5c3a5ca06 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) + sleep_interval_lower = sleep_interval.lower() + + 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) + + 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..18a68867d 100644 --- a/youtube_dl/options.py +++ b/youtube_dl/options.py @@ -489,7 +489,7 @@ 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=float, + dest='sleep_interval', type=str, help='Number of seconds to sleep before each download.') verbosity = optparse.OptionGroup(parser, 'Verbosity / Simulation Options')