From 2fbd6de957bfd13bd0873ee865a7e693534fd3a4 Mon Sep 17 00:00:00 2001 From: Elyse <26639800+elyse0@users.noreply.github.com> Date: Thu, 9 Mar 2023 11:30:40 -0600 Subject: [PATCH] [utils] Add hackish 'now' support for --download-sections --- yt_dlp/__init__.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/yt_dlp/__init__.py b/yt_dlp/__init__.py index 9ef31601c..b52aa1e9e 100644 --- a/yt_dlp/__init__.py +++ b/yt_dlp/__init__.py @@ -42,6 +42,7 @@ GeoUtils, PlaylistEntries, SameFileError, + datetime_from_str, decodeOption, download_range_func, expand_path, @@ -320,12 +321,23 @@ def validate_outtmpl(tmpl, msg): del opts.outtmpl['default'] def parse_chapters(name, value): + def parse_timestamp(x): + # FIXME: This should be smarter, e.g. 'inf-1day'? + x = x.replace('(', '').replace(')', '') + + if x in ('inf', 'infinite'): + return float('inf') + + if re.match(r'[\d:]+', x): + return parse_duration(x) + + return datetime_from_str(x, precision='second').timestamp() + chapters, ranges = [], [] - parse_timestamp = lambda x: float('inf') if x in ('inf', 'infinite') else parse_duration(x) for regex in value or []: if regex.startswith('*'): for range_ in map(str.strip, regex[1:].split(',')): - mobj = range_ != '-' and re.fullmatch(r'([^-]+)?\s*-\s*([^-]+)?', range_) + mobj = range_ != '-' and re.fullmatch(r'(.+)?\s*-\s*(.+)?', range_) dur = mobj and (parse_timestamp(mobj.group(1) or '0'), parse_timestamp(mobj.group(2) or 'inf')) if None in (dur or [None]): raise ValueError(f'invalid {name} time range "{regex}". Must be of the form "*start-end"')