From 0de19d2c1105e2fdaf65006c4d95347fcecc2d0a Mon Sep 17 00:00:00 2001 From: Daniel Date: Sat, 23 Mar 2013 18:26:48 +0100 Subject: [PATCH] Option --flexible-quality (-Q) --- lib/svtplay_dl/__init__.py | 9 ++++++++- lib/svtplay_dl/utils.py | 29 +++++++++++++++++++---------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/lib/svtplay_dl/__init__.py b/lib/svtplay_dl/__init__.py index 1ddb4ec..6df3ad7 100644 --- a/lib/svtplay_dl/__init__.py +++ b/lib/svtplay_dl/__init__.py @@ -42,6 +42,7 @@ class Options: self.live = False self.silent = False self.quality = None + self.flexibleq = None self.hls = False self.other = None self.subtitle = False @@ -105,8 +106,10 @@ def main(): help="Enable for live streams") parser.add_option("-s", "--silent", action="store_true", dest="silent", default=False) - parser.add_option("-q", "--quality", + parser.add_option("-q", "--quality", default=0, metavar="quality", help="Choose what format to download.\nIt will download the best format by default") + parser.add_option("-Q", "--flexible-quality", default=0, + metavar="amount", dest="flexibleq", help="Allow given quality (as above) to differ by an amount.") parser.add_option("-H", "--hls", action="store_true", dest="hls", default=False) parser.add_option("-S", "--subtitle", @@ -122,5 +125,9 @@ def main(): setup_log(options.silent) + if options.flexibleq and not options.quality: + log.error("flexible-quality requires a quality") + sys.exit(4) + url = args[0] get_media(url, options) diff --git a/lib/svtplay_dl/utils.py b/lib/svtplay_dl/utils.py index 062dced..6d29f9f 100644 --- a/lib/svtplay_dl/utils.py +++ b/lib/svtplay_dl/utils.py @@ -193,19 +193,28 @@ def subtitle_wsrt(options, data): fd.close() def select_quality(options, streams): - sort = sorted(streams.keys(), key=int) + available = sorted(streams.keys(), key=int) - if options.quality: - quality = options.quality + optq = int(options.quality) + if optq: + optf = int(options.flexibleq) + if not optf: + wanted = [optq] + else: + wanted = range(optq-optf, optq+optf+1) else: - quality = sort.pop() + wanted = [available[-1]] - try: - selected = streams[int(quality)] - except (KeyError, ValueError): - log.error("Can't find that quality. (Try one of: %s)", - ", ".join(map(str, sort))) + selected = None + for q in available: + if q in wanted: + selected = q + break + + if not selected: + log.error("Can't find that quality. Try one of: %s (or try --flexible-quality)", + ", ".join(map(str, available))) sys.exit(4) - return selected + return streams[selected]