diff options
author | Simon Sawicki <contact@grub4k.xyz> | 2023-03-25 19:41:28 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-25 19:41:28 +0100 |
commit | 0898c5c8ccadfc404472456a7a7751b72afebadd (patch) | |
tree | 36aff78eca23d72a6b22c8601258f6be33dd5449 /yt_dlp/utils.py | |
parent | f68434cc74cfd3db01b266476a2eac8329fbb267 (diff) | |
download | hypervideo-pre-0898c5c8ccadfc404472456a7a7751b72afebadd.tar.lz hypervideo-pre-0898c5c8ccadfc404472456a7a7751b72afebadd.tar.xz hypervideo-pre-0898c5c8ccadfc404472456a7a7751b72afebadd.zip |
[utils] `js_to_json`: Implement template strings (#6623)
Authored by: Grub4K
Diffstat (limited to 'yt_dlp/utils.py')
-rw-r--r-- | yt_dlp/utils.py | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/yt_dlp/utils.py b/yt_dlp/utils.py index 8c2c5593c..40533c2cb 100644 --- a/yt_dlp/utils.py +++ b/yt_dlp/utils.py @@ -3366,7 +3366,7 @@ def strip_jsonp(code): def js_to_json(code, vars={}, *, strict=False): # vars is a dict of var, val pairs to substitute - STRING_QUOTES = '\'"' + STRING_QUOTES = '\'"`' STRING_RE = '|'.join(rf'{q}(?:\\.|[^\\{q}])*{q}' for q in STRING_QUOTES) COMMENT_RE = r'/\*(?:(?!\*/).)*?\*/|//[^\n]*\n' SKIP_RE = fr'\s*(?:{COMMENT_RE})?\s*' @@ -3384,6 +3384,12 @@ def js_to_json(code, vars={}, *, strict=False): else '' if escape == '\n' else escape) + def template_substitute(match): + evaluated = js_to_json(match.group(1), vars, strict=strict) + if evaluated[0] == '"': + return json.loads(evaluated) + return evaluated + def fix_kv(m): v = m.group(0) if v in ('true', 'false', 'null'): @@ -3394,7 +3400,8 @@ def js_to_json(code, vars={}, *, strict=False): return '' if v[0] in STRING_QUOTES: - escaped = re.sub(r'(?s)(")|\\(.)', process_escape, v[1:-1]) + v = re.sub(r'(?s)\${([^}]+)}', template_substitute, v[1:-1]) if v[0] == '`' else v[1:-1] + escaped = re.sub(r'(?s)(")|\\(.)', process_escape, v) return f'"{escaped}"' for regex, base in INTEGER_TABLE: |