Added option --max-sleep-interval which activates random sleep if set.

This commit is contained in:
singh-pratyush96 2016-07-25 09:41:21 +05:30
parent 63000dd01f
commit 6ab2759a7c
5 changed files with 37 additions and 14 deletions

View File

@ -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:
- <lower limit>to<upper limit>
- <upper limit>to<lower limit>
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))

View File

@ -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,

View File

@ -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)

View File

@ -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(

View File

@ -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.