mirror of
https://github.com/ytdl-org/youtube-dl
synced 2024-12-23 04:30:10 +09:00
[utils] Don't raise in js_to_json() template substitution when non-strict
* template extression should be evaluated with the same strict-ness
This commit is contained in:
parent
26f0de9eab
commit
6397648af6
@ -4498,7 +4498,7 @@ def strip_jsonp(code):
|
|||||||
def js_to_json(code, *args, **kwargs):
|
def js_to_json(code, *args, **kwargs):
|
||||||
|
|
||||||
# vars is a dict of (var, val) pairs to substitute
|
# vars is a dict of (var, val) pairs to substitute
|
||||||
vars = args[0] if len(args) > 0 else kwargs.get('vars', {})
|
js_vars = args[0] if len(args) > 0 else kwargs.get('vars', {})
|
||||||
strict = kwargs.get('strict', False)
|
strict = kwargs.get('strict', False)
|
||||||
|
|
||||||
STRING_QUOTES = '\'"`'
|
STRING_QUOTES = '\'"`'
|
||||||
@ -4523,9 +4523,13 @@ def js_to_json(code, *args, **kwargs):
|
|||||||
else escape)
|
else escape)
|
||||||
|
|
||||||
def template_substitute(match):
|
def template_substitute(match):
|
||||||
evaluated = js_to_json(match.group(1), vars, strict=strict)
|
evaluated = js_to_json(match.group(1), js_vars, strict=strict)
|
||||||
if evaluated[0] == '"':
|
if evaluated[0] == '"':
|
||||||
return json.loads(evaluated)
|
try:
|
||||||
|
return json.loads(evaluated)
|
||||||
|
except JSONDecodeError:
|
||||||
|
if strict:
|
||||||
|
raise
|
||||||
return evaluated
|
return evaluated
|
||||||
|
|
||||||
def fix_kv(m):
|
def fix_kv(m):
|
||||||
@ -4559,14 +4563,14 @@ def js_to_json(code, *args, **kwargs):
|
|||||||
i = int(im.group(1), base)
|
i = int(im.group(1), base)
|
||||||
return ('"%s":' if v.endswith(':') else '%s') % inv(i)
|
return ('"%s":' if v.endswith(':') else '%s') % inv(i)
|
||||||
|
|
||||||
if v in vars:
|
if v in js_vars:
|
||||||
try:
|
try:
|
||||||
if not strict:
|
if not strict:
|
||||||
json.loads(vars[v])
|
json.loads(js_vars[v])
|
||||||
except JSONDecodeError:
|
except JSONDecodeError:
|
||||||
return inv(json.dumps(vars[v]))
|
return inv(json.dumps(js_vars[v]))
|
||||||
else:
|
else:
|
||||||
return inv(vars[v])
|
return inv(js_vars[v])
|
||||||
|
|
||||||
if not strict:
|
if not strict:
|
||||||
v = try_call(inv, args=(v,), default=v)
|
v = try_call(inv, args=(v,), default=v)
|
||||||
@ -4577,7 +4581,7 @@ def js_to_json(code, *args, **kwargs):
|
|||||||
raise ValueError('Unknown value: ' + v)
|
raise ValueError('Unknown value: ' + v)
|
||||||
|
|
||||||
def create_map(mobj):
|
def create_map(mobj):
|
||||||
return json.dumps(dict(json.loads(js_to_json(mobj.group(1) or '[]', vars=vars))))
|
return json.dumps(dict(json.loads(js_to_json(mobj.group(1) or '[]', vars=js_vars))))
|
||||||
|
|
||||||
code = re.sub(r'new Map\((\[.*?\])?\)', create_map, code)
|
code = re.sub(r'new Map\((\[.*?\])?\)', create_map, code)
|
||||||
if not strict:
|
if not strict:
|
||||||
@ -6715,3 +6719,8 @@ class _UnsafeExtensionError(Exception):
|
|||||||
raise cls(extension)
|
raise cls(extension)
|
||||||
|
|
||||||
return extension
|
return extension
|
||||||
|
|
||||||
|
|
||||||
|
def json_stringify(json_data, **kwargs):
|
||||||
|
kwargs.setdefault('separators', (',', ':'))
|
||||||
|
return json.dumps(json_data, **kwargs).decode('utf-8')
|
||||||
|
Loading…
Reference in New Issue
Block a user