aboutsummaryrefslogtreecommitdiffstats
path: root/yt_dlp/YoutubeDL.py
diff options
context:
space:
mode:
authorLukas Fink <lukas.fink1@gmail.com>2022-02-11 22:35:34 +0100
committerGitHub <noreply@github.com>2022-02-11 13:35:34 -0800
commit1ce9a3cb49b209ccb999d4fb97851f60d51dabd3 (patch)
tree700859a559953123918f00674685aef771c5f9e2 /yt_dlp/YoutubeDL.py
parentd49f8db39fd94a9ec314c43ce31d85facb1b8886 (diff)
downloadhypervideo-pre-1ce9a3cb49b209ccb999d4fb97851f60d51dabd3.tar.lz
hypervideo-pre-1ce9a3cb49b209ccb999d4fb97851f60d51dabd3.tar.xz
hypervideo-pre-1ce9a3cb49b209ccb999d4fb97851f60d51dabd3.zip
Add regex operator and quoting to format filters (#2698)
Closes #2681 Authored by: lukasfink1
Diffstat (limited to 'yt_dlp/YoutubeDL.py')
-rw-r--r--yt_dlp/YoutubeDL.py12
1 files changed, 9 insertions, 3 deletions
diff --git a/yt_dlp/YoutubeDL.py b/yt_dlp/YoutubeDL.py
index 74684dea3..9892ed328 100644
--- a/yt_dlp/YoutubeDL.py
+++ b/yt_dlp/YoutubeDL.py
@@ -1842,15 +1842,21 @@ class YoutubeDL(object):
'^=': lambda attr, value: attr.startswith(value),
'$=': lambda attr, value: attr.endswith(value),
'*=': lambda attr, value: value in attr,
+ '~=': lambda attr, value: value.search(attr) is not None
}
str_operator_rex = re.compile(r'''(?x)\s*
(?P<key>[a-zA-Z0-9._-]+)\s*
- (?P<negation>!\s*)?(?P<op>%s)(?P<none_inclusive>\s*\?)?\s*
- (?P<value>[a-zA-Z0-9._-]+)\s*
+ (?P<negation>!\s*)?(?P<op>%s)\s*(?P<none_inclusive>\?\s*)?
+ (?P<quote>["'])?
+ (?P<value>(?(quote)(?:(?!(?P=quote))[^\\]|\\.)+|[\w.-]+))
+ (?(quote)(?P=quote))\s*
''' % '|'.join(map(re.escape, STR_OPERATORS.keys())))
m = str_operator_rex.fullmatch(filter_spec)
if m:
- comparison_value = m.group('value')
+ if m.group('op') == '~=':
+ comparison_value = re.compile(m.group('value'))
+ else:
+ comparison_value = re.sub(r'''\\([\\"'])''', r'\1', m.group('value'))
str_op = STR_OPERATORS[m.group('op')]
if m.group('negation'):
op = lambda attr, value: not str_op(attr, value)