From fe935ab7ecaf9f008910c823101d6b02faee1a8d Mon Sep 17 00:00:00 2001 From: FooBarQuaxx Date: Wed, 16 Nov 2016 11:18:53 +0300 Subject: [PATCH 1/3] Dryied bytes option parsing. --- youtube_dl/__init__.py | 31 ++++++++++--------------------- youtube_dl/options.py | 13 +++++++++++++ 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/youtube_dl/__init__.py b/youtube_dl/__init__.py index 643393558..c92870c49 100644 --- a/youtube_dl/__init__.py +++ b/youtube_dl/__init__.py @@ -140,21 +140,6 @@ def _real_main(argv=None): opts.password = compat_getpass('Type account password and press [Return]: ') if opts.ap_username is not None and opts.ap_password is None: opts.ap_password = compat_getpass('Type TV provider account password and press [Return]: ') - if opts.ratelimit is not None: - numeric_limit = FileDownloader.parse_bytes(opts.ratelimit) - if numeric_limit is None: - parser.error('invalid rate limit specified') - opts.ratelimit = numeric_limit - if opts.min_filesize is not None: - numeric_limit = FileDownloader.parse_bytes(opts.min_filesize) - if numeric_limit is None: - parser.error('invalid min_filesize specified') - opts.min_filesize = numeric_limit - if opts.max_filesize is not None: - numeric_limit = FileDownloader.parse_bytes(opts.max_filesize) - 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 must be positive or 0') @@ -181,11 +166,6 @@ def _real_main(argv=None): opts.retries = parse_retries(opts.retries) if opts.fragment_retries is not None: opts.fragment_retries = parse_retries(opts.fragment_retries) - if opts.buffersize is not None: - numeric_buffersize = FileDownloader.parse_bytes(opts.buffersize) - if numeric_buffersize is None: - parser.error('invalid buffer size specified') - opts.buffersize = numeric_buffersize if opts.playliststart <= 0: raise ValueError('Playlist start must be positive') if opts.playlistend not in (-1, None) and opts.playlistend < opts.playliststart: @@ -231,7 +211,16 @@ def _real_main(argv=None): ' file! Use "{0}.%(ext)s" instead of "{0}" as the output' ' template'.format(outtmpl)) - any_getting = opts.geturl or opts.gettitle or opts.getid or opts.getthumbnail or opts.getdescription or opts.getfilename or opts.getformat or opts.getduration or opts.dumpjson or opts.dump_single_json + any_getting = any((getattr(opts, attr, None) for attr in ('geturl', + 'gettitle', + 'getid', + 'getthumbnail', + 'getdescription', + 'getfilename', + 'getformat', + 'getduration', + 'dumpjson', + 'dump_single_json'))) any_printing = opts.print_json download_archive_fn = compat_expanduser(opts.download_archive) if opts.download_archive is not None else opts.download_archive diff --git a/youtube_dl/options.py b/youtube_dl/options.py index 53497fbc6..7f98f3088 100644 --- a/youtube_dl/options.py +++ b/youtube_dl/options.py @@ -17,6 +17,9 @@ from .utils import ( preferredencoding, write_string, ) +from .downloader import ( + FileDownloader, +) from .version import __version__ @@ -93,6 +96,12 @@ def parseOpts(overrideArguments=None): def _comma_separated_values_options_callback(option, opt_str, value, parser): setattr(parser.values, option.dest, value.split(',')) + def _bytes_option_callback(option, opt_str, value, parser): + _bytes = FileDownloader.parse_bytes(value) + if _bytes is None: + raise OptionValueError('invalid %s specified' % option.dest) + setattr(parser.values, option.dest, _bytes) + def _hide_login_info(opts): PRIVATE_OPTS = ['-p', '--password', '-u', '--username', '--video-password', '--ap-password', '--ap-username'] eqre = re.compile('^(?P' + ('|'.join(re.escape(po) for po in PRIVATE_OPTS)) + ')=.+$') @@ -264,10 +273,12 @@ def parseOpts(overrideArguments=None): selection.add_option( '--min-filesize', metavar='SIZE', dest='min_filesize', default=None, + action='callback', callback=_bytes_option_callback, help='Do not download any videos smaller than SIZE (e.g. 50k or 44.6m)') selection.add_option( '--max-filesize', metavar='SIZE', dest='max_filesize', default=None, + action='callback', callback=_bytes_option_callback, help='Do not download any videos larger than SIZE (e.g. 50k or 44.6m)') selection.add_option( '--date', @@ -433,6 +444,7 @@ def parseOpts(overrideArguments=None): downloader.add_option( '-r', '--limit-rate', '--rate-limit', dest='ratelimit', metavar='RATE', + action='callback', callback=_bytes_option_callback, help='Maximum download rate in bytes per second (e.g. 50K or 4.2M)') downloader.add_option( '-R', '--retries', @@ -453,6 +465,7 @@ def parseOpts(overrideArguments=None): downloader.add_option( '--buffer-size', dest='buffersize', metavar='SIZE', default='1024', + action='callback', callback=_bytes_option_callback, help='Size of download buffer (e.g. 1024 or 16K) (default is %default)') downloader.add_option( '--no-resize-buffer', From e42b8ebbd414ddd4c5223ca882761d383958fb58 Mon Sep 17 00:00:00 2001 From: FooBarQuaxx Date: Wed, 16 Nov 2016 11:23:54 +0300 Subject: [PATCH 2/3] Fixed exception name --- youtube_dl/options.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/youtube_dl/options.py b/youtube_dl/options.py index 7f98f3088..fc2ea4885 100644 --- a/youtube_dl/options.py +++ b/youtube_dl/options.py @@ -99,7 +99,7 @@ def parseOpts(overrideArguments=None): def _bytes_option_callback(option, opt_str, value, parser): _bytes = FileDownloader.parse_bytes(value) if _bytes is None: - raise OptionValueError('invalid %s specified' % option.dest) + raise optparse.OptionValueError('invalid %s specified' % option.dest) setattr(parser.values, option.dest, _bytes) def _hide_login_info(opts): From cd51a4b508c1d167f94ad409f3803f4a6fd52cd6 Mon Sep 17 00:00:00 2001 From: FooBarQuaxx Date: Fri, 18 Nov 2016 23:09:23 +0300 Subject: [PATCH 3/3] Fix bytes options option definition --- youtube_dl/options.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/youtube_dl/options.py b/youtube_dl/options.py index fc2ea4885..e7ab654c5 100644 --- a/youtube_dl/options.py +++ b/youtube_dl/options.py @@ -273,12 +273,12 @@ def parseOpts(overrideArguments=None): selection.add_option( '--min-filesize', metavar='SIZE', dest='min_filesize', default=None, - action='callback', callback=_bytes_option_callback, + type=str, action='callback', callback=_bytes_option_callback, help='Do not download any videos smaller than SIZE (e.g. 50k or 44.6m)') selection.add_option( '--max-filesize', metavar='SIZE', dest='max_filesize', default=None, - action='callback', callback=_bytes_option_callback, + type=str, action='callback', callback=_bytes_option_callback, help='Do not download any videos larger than SIZE (e.g. 50k or 44.6m)') selection.add_option( '--date', @@ -444,7 +444,7 @@ def parseOpts(overrideArguments=None): downloader.add_option( '-r', '--limit-rate', '--rate-limit', dest='ratelimit', metavar='RATE', - action='callback', callback=_bytes_option_callback, + type=str, action='callback', callback=_bytes_option_callback, help='Maximum download rate in bytes per second (e.g. 50K or 4.2M)') downloader.add_option( '-R', '--retries', @@ -465,7 +465,7 @@ def parseOpts(overrideArguments=None): downloader.add_option( '--buffer-size', dest='buffersize', metavar='SIZE', default='1024', - action='callback', callback=_bytes_option_callback, + type=str, action='callback', callback=_bytes_option_callback, help='Size of download buffer (e.g. 1024 or 16K) (default is %default)') downloader.add_option( '--no-resize-buffer',