[jsinterp] Handle new youtube signature functions

Closes #4635
This commit is contained in:
pukkandan 2022-08-14 04:51:54 +05:30
parent 1cddfdc52b
commit 8f53dc44a0
No known key found for this signature in database
GPG Key ID: 7EEE9E1E817D0A39
6 changed files with 287 additions and 125 deletions

View File

@ -19,6 +19,9 @@ def test_basic(self):
jsi = JSInterpreter('function x3(){return 42;}')
self.assertEqual(jsi.call_function('x3'), 42)
jsi = JSInterpreter('function x3(){42}')
self.assertEqual(jsi.call_function('x3'), None)
jsi = JSInterpreter('var x5 = function(){return 42;}')
self.assertEqual(jsi.call_function('x5'), 42)
@ -51,8 +54,11 @@ def test_operators(self):
jsi = JSInterpreter('function f(){return 11 >> 2;}')
self.assertEqual(jsi.call_function('f'), 2)
jsi = JSInterpreter('function f(){return []? 2+3: 4;}')
self.assertEqual(jsi.call_function('f'), 5)
def test_array_access(self):
jsi = JSInterpreter('function f(){var x = [1,2,3]; x[0] = 4; x[0] = 5; x[2] = 7; return x;}')
jsi = JSInterpreter('function f(){var x = [1,2,3]; x[0] = 4; x[0] = 5; x[2.0] = 7; return x;}')
self.assertEqual(jsi.call_function('f'), [5, 2, 7])
def test_parens(self):
@ -62,6 +68,10 @@ def test_parens(self):
jsi = JSInterpreter('function f(){return (1 + 2) * 3;}')
self.assertEqual(jsi.call_function('f'), 9)
def test_quotes(self):
jsi = JSInterpreter(R'function f(){return "a\"\\("}')
self.assertEqual(jsi.call_function('f'), R'a"\(')
def test_assignments(self):
jsi = JSInterpreter('function f(){var x = 20; x = 30 + 1; return x;}')
self.assertEqual(jsi.call_function('f'), 31)
@ -107,14 +117,15 @@ def test_precedence(self):
def test_call(self):
jsi = JSInterpreter('''
function x() { return 2; }
function y(a) { return x() + a; }
function y(a) { return x() + (a?a:0); }
function z() { return y(3); }
''')
self.assertEqual(jsi.call_function('z'), 5)
self.assertEqual(jsi.call_function('y'), 2)
def test_for_loop(self):
jsi = JSInterpreter('''
function x() { a=0; for (i=0; i-10; i++) {a++} a }
function x() { a=0; for (i=0; i-10; i++) {a++} return a }
''')
self.assertEqual(jsi.call_function('x'), 10)
@ -155,19 +166,19 @@ def test_try(self):
def test_for_loop_continue(self):
jsi = JSInterpreter('''
function x() { a=0; for (i=0; i-10; i++) { continue; a++ } a }
function x() { a=0; for (i=0; i-10; i++) { continue; a++ } return a }
''')
self.assertEqual(jsi.call_function('x'), 0)
def test_for_loop_break(self):
jsi = JSInterpreter('''
function x() { a=0; for (i=0; i-10; i++) { break; a++ } a }
function x() { a=0; for (i=0; i-10; i++) { break; a++ } return a }
''')
self.assertEqual(jsi.call_function('x'), 0)
def test_literal_list(self):
jsi = JSInterpreter('''
function x() { [1, 2, "asdf", [5, 6, 7]][3] }
function x() { return [1, 2, "asdf", [5, 6, 7]][3] }
''')
self.assertEqual(jsi.call_function('x'), [5, 6, 7])
@ -177,6 +188,12 @@ def test_comma(self):
''')
self.assertEqual(jsi.call_function('x'), 7)
def test_return_function(self):
jsi = JSInterpreter('''
function x() { return [1, function(){return 1}][1] }
''')
self.assertEqual(jsi.call_function('x')([]), 1)
if __name__ == '__main__':
unittest.main()

View File

@ -413,6 +413,10 @@ def test_unified_timestamps(self):
self.assertEqual(unified_timestamp('December 15, 2017 at 7:49 am'), 1513324140)
self.assertEqual(unified_timestamp('2018-03-14T08:32:43.1493874+00:00'), 1521016363)
self.assertEqual(unified_timestamp('December 31 1969 20:00:01 EDT'), 1)
self.assertEqual(unified_timestamp('Wednesday 31 December 1969 18:01:26 MDT'), 86)
self.assertEqual(unified_timestamp('12/31/1969 20:01:18 EDT', False), 78)
def test_determine_ext(self):
self.assertEqual(determine_ext('http://example.com/foo/bar.mp4/?download'), 'mp4')
self.assertEqual(determine_ext('http://example.com/foo/bar/?download', None), None)

View File

@ -94,6 +94,14 @@
'https://www.youtube.com/s/player/5dd88d1d/player-plasma-ias-phone-en_US.vflset/base.js',
'kSxKFLeqzv_ZyHSAt', 'n8gS8oRlHOxPFA',
),
(
'https://www.youtube.com/s/player/324f67b9/player_ias.vflset/en_US/base.js',
'xdftNy7dh9QGnhW', '22qLGxrmX8F1rA',
),
(
'https://www.youtube.com/s/player/4c3f79c5/player_ias.vflset/en_US/base.js',
'TDCstCG66tEAO5pR9o', 'dbxNtZ14c-yWyw',
),
]

View File

@ -2653,7 +2653,8 @@ def _extract_n_function(self, video_id, player_url):
if self.get_param('youtube_print_sig_code'):
self.to_screen(f'Extracted nsig function from {player_id}:\n{func_code[1]}\n')
return lambda s: jsi.extract_function_from_code(*func_code)([s])
func = jsi.extract_function_from_code(*func_code)
return lambda s: func([s])
def _extract_signature_timestamp(self, video_id, player_url, ytcfg=None, fatal=False):
"""

View File

@ -1,29 +1,62 @@
import collections
import contextlib
import itertools
import json
import math
import operator
import re
from .utils import ExtractorError, remove_quotes, truncate_string
from .utils import (
NO_DEFAULT,
ExtractorError,
js_to_json,
remove_quotes,
truncate_string,
unified_timestamp,
write_string,
)
_NAME_RE = r'[a-zA-Z_$][\w$]*'
_OPERATORS = {
_OPERATORS = { # None => Defined in JSInterpreter._operator
'?': None,
'||': None,
'&&': None,
'&': operator.and_,
'|': operator.or_,
'^': operator.xor,
'&': operator.and_,
# FIXME: This should actually be below comparision
'>>': operator.rshift,
'<<': operator.lshift,
'-': operator.sub,
'<=': operator.le,
'>=': operator.ge,
'<': operator.lt,
'>': operator.gt,
'+': operator.add,
'%': operator.mod,
'/': operator.truediv,
'-': operator.sub,
'*': operator.mul,
'/': operator.truediv,
'%': operator.mod,
}
_MATCHING_PARENS = dict(zip('({[', ')}]'))
_QUOTES = '\'"'
def _ternary(cndn, if_true=True, if_false=False):
"""Simulate JS's ternary operator (cndn?if_true:if_false)"""
if cndn in (False, None, 0, ''):
return if_false
with contextlib.suppress(TypeError):
if math.isnan(cndn): # NB: NaN cannot be checked by membership
return if_false
return if_true
class JS_Break(ExtractorError):
def __init__(self):
ExtractorError.__init__(self, 'Invalid break')
@ -46,6 +79,27 @@ def __delitem__(self, key):
raise NotImplementedError('Deleting is not supported')
class Debugger:
import sys
ENABLED = 'pytest' in sys.modules
@staticmethod
def write(*args, level=100):
write_string(f'[debug] JS: {" " * (100 - level)}'
f'{" ".join(truncate_string(str(x), 50, 50) for x in args)}\n')
@classmethod
def wrap_interpreter(cls, f):
def interpret_statement(self, stmt, local_vars, allow_recursion, *args, **kwargs):
if cls.ENABLED and stmt.strip():
cls.write(stmt, level=allow_recursion)
ret, should_ret = f(self, stmt, local_vars, allow_recursion, *args, **kwargs)
if cls.ENABLED and stmt.strip():
cls.write(['->', '=>'][should_ret], repr(ret), '<-|', stmt, level=allow_recursion)
return ret, should_ret
return interpret_statement
class JSInterpreter:
__named_object_counter = 0
@ -56,7 +110,7 @@ def __init__(self, code, objects=None):
class Exception(ExtractorError):
def __init__(self, msg, expr=None, *args, **kwargs):
if expr is not None:
msg += f' in: {truncate_string(expr, 50, 50)}'
msg = f'{msg.rstrip()} in: {truncate_string(expr, 50, 50)}'
super().__init__(msg, *args, **kwargs)
def _named_object(self, namespace, obj):
@ -73,9 +127,9 @@ def _separate(expr, delim=',', max_split=None):
start, splits, pos, delim_len = 0, 0, 0, len(delim) - 1
in_quote, escaping = None, False
for idx, char in enumerate(expr):
if char in _MATCHING_PARENS:
if not in_quote and char in _MATCHING_PARENS:
counters[_MATCHING_PARENS[char]] += 1
elif char in counters:
elif not in_quote and char in counters:
counters[char] -= 1
elif not escaping and char in _QUOTES and in_quote in (char, None):
in_quote = None if in_quote else char
@ -101,50 +155,91 @@ def _separate_at_paren(cls, expr, delim):
raise cls.Exception(f'No terminating paren {delim}', expr)
return separated[0][1:].strip(), separated[1].strip()
def _operator(self, op, left_val, right_expr, expr, local_vars, allow_recursion):
if op in ('||', '&&'):
if (op == '&&') ^ _ternary(left_val):
return left_val # short circuiting
elif op == '?':
right_expr = _ternary(left_val, *self._separate(right_expr, ':', 1))
right_val = self.interpret_expression(right_expr, local_vars, allow_recursion)
if not _OPERATORS.get(op):
return right_val
try:
return _OPERATORS[op](left_val, right_val)
except Exception as e:
raise self.Exception(f'Failed to evaluate {left_val!r} {op} {right_val!r}', expr, cause=e)
def _index(self, obj, idx):
if idx == 'length':
return len(obj)
try:
return obj[int(idx)] if isinstance(obj, list) else obj[idx]
except Exception as e:
raise self.Exception(f'Cannot get index {idx}', repr(obj), cause=e)
def _dump(self, obj, namespace):
try:
return json.dumps(obj)
except TypeError:
return self._named_object(namespace, obj)
@Debugger.wrap_interpreter
def interpret_statement(self, stmt, local_vars, allow_recursion=100):
if allow_recursion < 0:
raise self.Exception('Recursion limit reached')
allow_recursion -= 1
should_abort = False
should_return = False
sub_statements = list(self._separate(stmt, ';')) or ['']
stmt = sub_statements.pop().lstrip()
expr = stmt = sub_statements.pop().strip()
for sub_stmt in sub_statements:
ret, should_abort = self.interpret_statement(sub_stmt, local_vars, allow_recursion - 1)
if should_abort:
return ret, should_abort
ret, should_return = self.interpret_statement(sub_stmt, local_vars, allow_recursion)
if should_return:
return ret, should_return
m = re.match(r'(?P<var>var\s)|return(?:\s+|$)', stmt)
if not m: # Try interpreting it as an expression
expr = stmt
elif m.group('var'):
expr = stmt[len(m.group(0)):]
else:
expr = stmt[len(m.group(0)):]
should_abort = True
return self.interpret_expression(expr, local_vars, allow_recursion), should_abort
def interpret_expression(self, expr, local_vars, allow_recursion):
expr = expr.strip()
if m:
expr = stmt[len(m.group(0)):].strip()
should_return = not m.group('var')
if not expr:
return None
return None, should_return
if expr[0] in _QUOTES:
inner, outer = self._separate(expr, expr[0], 1)
inner = json.loads(js_to_json(f'{inner}{expr[0]}', strict=True))
if not outer:
return inner, should_return
expr = self._named_object(local_vars, inner) + outer
if expr.startswith('new '):
obj = expr[4:]
if obj.startswith('Date('):
left, right = self._separate_at_paren(obj[4:], ')')
expr = unified_timestamp(left[1:-1], False)
if not expr:
raise self.Exception(f'Failed to parse date {left!r}', expr)
expr = self._dump(int(expr * 1000), local_vars) + right
else:
raise self.Exception(f'Unsupported object {obj}', expr)
if expr.startswith('{'):
inner, outer = self._separate_at_paren(expr, '}')
inner, should_abort = self.interpret_statement(inner, local_vars, allow_recursion - 1)
inner, should_abort = self.interpret_statement(inner, local_vars, allow_recursion)
if not outer or should_abort:
return inner
return inner, should_abort or should_return
else:
expr = json.dumps(inner) + outer
expr = self._dump(inner, local_vars) + outer
if expr.startswith('('):
inner, outer = self._separate_at_paren(expr, ')')
inner = self.interpret_expression(inner, local_vars, allow_recursion)
if not outer:
return inner
inner, should_abort = self.interpret_statement(inner, local_vars, allow_recursion)
if not outer or should_abort:
return inner, should_abort or should_return
else:
expr = json.dumps(inner) + outer
expr = self._dump(inner, local_vars) + outer
if expr.startswith('['):
inner, outer = self._separate_at_paren(expr, ']')
@ -153,21 +248,23 @@ def interpret_expression(self, expr, local_vars, allow_recursion):
for item in self._separate(inner)])
expr = name + outer
m = re.match(r'(?P<try>try)\s*|(?:(?P<catch>catch)|(?P<for>for)|(?P<switch>switch))\s*\(', expr)
m = re.match(r'(?P<try>try|finally)\s*|(?:(?P<catch>catch)|(?P<for>for)|(?P<switch>switch))\s*\(', expr)
if m and m.group('try'):
if expr[m.end()] == '{':
try_expr, expr = self._separate_at_paren(expr[m.end():], '}')
else:
try_expr, expr = expr[m.end() - 1:], ''
ret, should_abort = self.interpret_statement(try_expr, local_vars, allow_recursion - 1)
ret, should_abort = self.interpret_statement(try_expr, local_vars, allow_recursion)
if should_abort:
return ret
return self.interpret_statement(expr, local_vars, allow_recursion - 1)[0]
return ret, True
ret, should_abort = self.interpret_statement(expr, local_vars, allow_recursion)
return ret, should_abort or should_return
elif m and m.group('catch'):
# We ignore the catch block
_, expr = self._separate_at_paren(expr, '}')
return self.interpret_statement(expr, local_vars, allow_recursion - 1)[0]
ret, should_abort = self.interpret_statement(expr, local_vars, allow_recursion)
return ret, should_abort or should_return
elif m and m.group('for'):
constructor, remaining = self._separate_at_paren(expr[m.end() - 1:], ')')
@ -182,22 +279,21 @@ def interpret_expression(self, expr, local_vars, allow_recursion):
else:
body, expr = remaining, ''
start, cndn, increment = self._separate(constructor, ';')
if self.interpret_statement(start, local_vars, allow_recursion - 1)[1]:
raise self.Exception('Premature return in the initialization of a for loop', constructor)
self.interpret_expression(start, local_vars, allow_recursion)
while True:
if not self.interpret_expression(cndn, local_vars, allow_recursion):
if not _ternary(self.interpret_expression(cndn, local_vars, allow_recursion)):
break
try:
ret, should_abort = self.interpret_statement(body, local_vars, allow_recursion - 1)
ret, should_abort = self.interpret_statement(body, local_vars, allow_recursion)
if should_abort:
return ret
return ret, True
except JS_Break:
break
except JS_Continue:
pass
if self.interpret_statement(increment, local_vars, allow_recursion - 1)[1]:
raise self.Exception('Premature return in the initialization of a for loop', constructor)
return self.interpret_statement(expr, local_vars, allow_recursion - 1)[0]
self.interpret_expression(increment, local_vars, allow_recursion)
ret, should_abort = self.interpret_statement(expr, local_vars, allow_recursion)
return ret, should_abort or should_return
elif m and m.group('switch'):
switch_val, remaining = self._separate_at_paren(expr[m.end() - 1:], ')')
@ -215,20 +311,23 @@ def interpret_expression(self, expr, local_vars, allow_recursion):
if not matched:
continue
try:
ret, should_abort = self.interpret_statement(stmt, local_vars, allow_recursion - 1)
ret, should_abort = self.interpret_statement(stmt, local_vars, allow_recursion)
if should_abort:
return ret
except JS_Break:
break
if matched:
break
return self.interpret_statement(expr, local_vars, allow_recursion - 1)[0]
ret, should_abort = self.interpret_statement(expr, local_vars, allow_recursion)
return ret, should_abort or should_return
# Comma separated statements
sub_expressions = list(self._separate(expr))
expr = sub_expressions.pop().strip() if sub_expressions else ''
for sub_expr in sub_expressions:
self.interpret_expression(sub_expr, local_vars, allow_recursion)
ret, should_abort = self.interpret_statement(sub_expr, local_vars, allow_recursion)
if should_abort:
return ret, True
for m in re.finditer(rf'''(?x)
(?P<pre_sign>\+\+|--)(?P<var1>{_NAME_RE})|
@ -240,10 +339,10 @@ def interpret_expression(self, expr, local_vars, allow_recursion):
local_vars[var] += 1 if sign[0] == '+' else -1
if m.group('pre_sign'):
ret = local_vars[var]
expr = expr[:start] + json.dumps(ret) + expr[end:]
expr = expr[:start] + self._dump(ret, local_vars) + expr[end:]
if not expr:
return None
return None, should_return
m = re.match(fr'''(?x)
(?P<assign>
@ -251,36 +350,34 @@ def interpret_expression(self, expr, local_vars, allow_recursion):
(?P<op>{"|".join(map(re.escape, _OPERATORS))})?
=(?P<expr>.*)$
)|(?P<return>
(?!if|return|true|false|null)(?P<name>{_NAME_RE})$
(?!if|return|true|false|null|undefined)(?P<name>{_NAME_RE})$
)|(?P<indexing>
(?P<in>{_NAME_RE})\[(?P<idx>.+)\]$
)|(?P<attribute>
(?P<var>{_NAME_RE})(?:\.(?P<member>[^(]+)|\[(?P<member2>[^\]]+)\])\s*
)|(?P<function>
(?P<fname>{_NAME_RE})\((?P<args>[\w$,]*)\)$
(?P<fname>{_NAME_RE})\((?P<args>.*)\)$
)''', expr)
if m and m.group('assign'):
if not m.group('op'):
opfunc = lambda curr, right: right
else:
opfunc = _OPERATORS[m.group('op')]
right_val = self.interpret_expression(m.group('expr'), local_vars, allow_recursion)
left_val = local_vars.get(m.group('out'))
if not m.group('index'):
local_vars[m.group('out')] = opfunc(left_val, right_val)
return local_vars[m.group('out')]
local_vars[m.group('out')] = self._operator(
m.group('op'), left_val, m.group('expr'), expr, local_vars, allow_recursion)
return local_vars[m.group('out')], should_return
elif left_val is None:
raise self.Exception(f'Cannot index undefined variable {m.group("out")}', expr)
idx = self.interpret_expression(m.group('index'), local_vars, allow_recursion)
if not isinstance(idx, int):
if not isinstance(idx, (int, float)):
raise self.Exception(f'List index {idx} must be integer', expr)
left_val[idx] = opfunc(left_val[idx], right_val)
return left_val[idx]
idx = int(idx)
left_val[idx] = self._operator(
m.group('op'), left_val[idx], m.group('expr'), expr, local_vars, allow_recursion)
return left_val[idx], should_return
elif expr.isdigit():
return int(expr)
return int(expr), should_return
elif expr == 'break':
raise JS_Break()
@ -288,35 +385,33 @@ def interpret_expression(self, expr, local_vars, allow_recursion):
raise JS_Continue()
elif m and m.group('return'):
return local_vars[m.group('name')]
return local_vars[m.group('name')], should_return
with contextlib.suppress(ValueError):
return json.loads(expr)
return json.loads(js_to_json(expr, strict=True)), should_return
if m and m.group('indexing'):
val = local_vars[m.group('in')]
idx = self.interpret_expression(m.group('idx'), local_vars, allow_recursion)
return val[idx]
return self._index(val, idx), should_return
for op, opfunc in _OPERATORS.items():
for op in _OPERATORS:
separated = list(self._separate(expr, op))
if len(separated) < 2:
continue
right_val = separated.pop()
left_val = op.join(separated)
left_val, should_abort = self.interpret_statement(
left_val, local_vars, allow_recursion - 1)
if should_abort:
raise self.Exception(f'Premature left-side return of {op}', expr)
right_val, should_abort = self.interpret_statement(
right_val, local_vars, allow_recursion - 1)
if should_abort:
raise self.Exception(f'Premature right-side return of {op}', expr)
return opfunc(left_val or 0, right_val)
right_expr = separated.pop()
while op == '-' and len(separated) > 1 and not separated[-1].strip():
right_expr = f'-{right_expr}'
separated.pop()
left_val = self.interpret_expression(op.join(separated), local_vars, allow_recursion)
return self._operator(op, 0 if left_val is None else left_val,
right_expr, expr, local_vars, allow_recursion), should_return
if m and m.group('attribute'):
variable = m.group('var')
member = remove_quotes(m.group('member') or m.group('member2'))
member = m.group('member')
if not member:
member = self.interpret_expression(m.group('member2'), local_vars, allow_recursion)
arg_str = expr[m.end():]
if arg_str.startswith('('):
arg_str, remaining = self._separate_at_paren(arg_str, ')')
@ -329,20 +424,24 @@ def assertion(cndn, msg):
raise self.Exception(f'{member} {msg}', expr)
def eval_method():
if variable == 'String':
obj = str
elif variable in local_vars:
obj = local_vars[variable]
else:
if (variable, member) == ('console', 'debug'):
if Debugger.ENABLED:
Debugger.write(self.interpret_expression(f'[{arg_str}]', local_vars, allow_recursion))
return
types = {
'String': str,
'Math': float,
}
obj = local_vars.get(variable, types.get(variable, NO_DEFAULT))
if obj is NO_DEFAULT:
if variable not in self._objects:
self._objects[variable] = self.extract_object(variable)
obj = self._objects[variable]
# Member access
if arg_str is None:
if member == 'length':
return len(obj)
return obj[member]
return self._index(obj, member)
# Function call
argvals = [
@ -353,12 +452,17 @@ def eval_method():
if member == 'fromCharCode':
assertion(argvals, 'takes one or more arguments')
return ''.join(map(chr, argvals))
raise self.Exception(f'Unsupported string method {member}', expr)
raise self.Exception(f'Unsupported String method {member}', expr)
elif obj == float:
if member == 'pow':
assertion(len(argvals) == 2, 'takes two arguments')
return argvals[0] ** argvals[1]
raise self.Exception(f'Unsupported Math method {member}', expr)
if member == 'split':
assertion(argvals, 'takes one or more arguments')
assertion(argvals == [''], 'with arguments is not implemented')
return list(obj)
assertion(len(argvals) == 1, 'with limit argument is not implemented')
return obj.split(argvals[0]) if argvals[0] else list(obj)
elif member == 'join':
assertion(isinstance(obj, list), 'must be applied on a list')
assertion(len(argvals) == 1, 'takes exactly one argument')
@ -404,7 +508,7 @@ def eval_method():
assertion(argvals, 'takes one or more arguments')
assertion(len(argvals) <= 2, 'takes at-most 2 arguments')
f, this = (argvals + [''])[:2]
return [f((item, idx, obj), this=this) for idx, item in enumerate(obj)]
return [f((item, idx, obj), {'this': this}, allow_recursion) for idx, item in enumerate(obj)]
elif member == 'indexOf':
assertion(argvals, 'takes one or more arguments')
assertion(len(argvals) <= 2, 'takes at-most 2 arguments')
@ -414,27 +518,35 @@ def eval_method():
except ValueError:
return -1
return obj[int(member) if isinstance(obj, list) else member](argvals)
idx = int(member) if isinstance(obj, list) else member
return obj[idx](argvals, allow_recursion=allow_recursion)
if remaining:
return self.interpret_expression(
ret, should_abort = self.interpret_statement(
self._named_object(local_vars, eval_method()) + remaining,
local_vars, allow_recursion)
return ret, should_return or should_abort
else:
return eval_method()
return eval_method(), should_return
elif m and m.group('function'):
fname = m.group('fname')
argvals = tuple(
int(v) if v.isdigit() else local_vars[v]
for v in self._separate(m.group('args')))
argvals = [self.interpret_expression(v, local_vars, allow_recursion)
for v in self._separate(m.group('args'))]
if fname in local_vars:
return local_vars[fname](argvals)
return local_vars[fname](argvals, allow_recursion=allow_recursion), should_return
elif fname not in self._functions:
self._functions[fname] = self.extract_function(fname)
return self._functions[fname](argvals)
return self._functions[fname](argvals, allow_recursion=allow_recursion), should_return
raise self.Exception('Unsupported JS expression', expr)
raise self.Exception(
f'Unsupported JS expression {truncate_string(expr, 20, 20) if expr != stmt else ""}', stmt)
def interpret_expression(self, expr, local_vars, allow_recursion):
ret, should_return = self.interpret_statement(expr, local_vars, allow_recursion)
if should_return:
raise self.Exception('Cannot return from an expression', expr)
return ret
def extract_object(self, objname):
_FUNC_NAME_RE = r'''(?:[a-zA-Z$0-9]+|"[a-zA-Z$0-9]+"|'[a-zA-Z$0-9]+')'''
@ -446,6 +558,8 @@ def extract_object(self, objname):
}\s*;
''' % (re.escape(objname), _FUNC_NAME_RE),
self.code)
if not obj_m:
raise self.Exception(f'Could not find object {objname}')
fields = obj_m.group('fields')
# Currently, it only supports function definitions
fields_m = re.finditer(
@ -462,19 +576,19 @@ def extract_object(self, objname):
def extract_function_code(self, funcname):
""" @returns argnames, code """
func_m = re.search(
r'''(?x)
r'''(?xs)
(?:
function\s+%(name)s|
[{;,]\s*%(name)s\s*=\s*function|
var\s+%(name)s\s*=\s*function
)\s*
\((?P<args>[^)]*)\)\s*
(?P<code>{(?:(?!};)[^"]|"([^"]|\\")*")+})''' % {'name': re.escape(funcname)},
(?P<code>{.+})''' % {'name': re.escape(funcname)},
self.code)
code, _ = self._separate_at_paren(func_m.group('code'), '}') # refine the match
code, _ = self._separate_at_paren(func_m.group('code'), '}')
if func_m is None:
raise self.Exception(f'Could not find JS function "{funcname}"')
return func_m.group('args').split(','), code
return [x.strip() for x in func_m.group('args').split(',')], code
def extract_function(self, funcname):
return self.extract_function_from_code(*self.extract_function_code(funcname))
@ -498,16 +612,15 @@ def call_function(self, funcname, *args):
def build_function(self, argnames, code, *global_stack):
global_stack = list(global_stack) or [{}]
argnames = tuple(argnames)
def resf(args, **kwargs):
def resf(args, kwargs={}, allow_recursion=100):
global_stack[0].update({
**dict(zip(argnames, args)),
**dict(itertools.zip_longest(argnames, args, fillvalue=None)),
**kwargs
})
var_stack = LocalNameSpace(*global_stack)
for stmt in self._separate(code.replace('\n', ''), ';'):
ret, should_abort = self.interpret_statement(stmt, var_stack)
if should_abort:
break
return ret
ret, should_abort = self.interpret_statement(code.replace('\n', ''), var_stack, allow_recursion - 1)
if should_abort:
return ret
return resf

View File

@ -150,6 +150,16 @@ def random_user_agent():
'juillet', 'août', 'septembre', 'octobre', 'novembre', 'décembre'],
}
# From https://github.com/python/cpython/blob/3.11/Lib/email/_parseaddr.py#L36-L42
TIMEZONE_NAMES = {
'UT': 0, 'UTC': 0, 'GMT': 0, 'Z': 0,
'AST': -4, 'ADT': -3, # Atlantic (used in Canada)
'EST': -5, 'EDT': -4, # Eastern
'CST': -6, 'CDT': -5, # Central
'MST': -7, 'MDT': -6, # Mountain
'PST': -8, 'PDT': -7 # Pacific
}
# needed for sanitizing filenames in restricted mode
ACCENT_CHARS = dict(zip('ÂÃÄÀÁÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖŐØŒÙÚÛÜŰÝÞßàáâãäåæçèéêëìíîïðñòóôõöőøœùúûüűýþÿ',
itertools.chain('AAAAAA', ['AE'], 'CEEEEIIIIDNOOOOOOO', ['OE'], 'UUUUUY', ['TH', 'ss'],
@ -1684,7 +1694,11 @@ def extract_timezone(date_str):
$)
''', date_str)
if not m:
timezone = datetime.timedelta()
m = re.search(r'\d{1,2}:\d{1,2}(?:\.\d+)?(?P<tz>\s*[A-Z]+)$', date_str)
timezone = TIMEZONE_NAMES.get(m and m.group('tz').strip())
if timezone is not None:
date_str = date_str[:-len(m.group('tz'))]
timezone = datetime.timedelta(hours=timezone or 0)
else:
date_str = date_str[:-len(m.group('tz'))]
if not m.group('sign'):
@ -1746,7 +1760,8 @@ def unified_timestamp(date_str, day_first=True):
if date_str is None:
return None
date_str = re.sub(r'[,|]', '', date_str)
date_str = re.sub(r'\s+', ' ', re.sub(
r'(?i)[,|]|(mon|tues?|wed(nes)?|thu(rs)?|fri|sat(ur)?)(day)?', '', date_str))
pm_delta = 12 if re.search(r'(?i)PM', date_str) else 0
timezone, date_str = extract_timezone(date_str)
@ -1768,9 +1783,10 @@ def unified_timestamp(date_str, day_first=True):
with contextlib.suppress(ValueError):
dt = datetime.datetime.strptime(date_str, expression) - timezone + datetime.timedelta(hours=pm_delta)
return calendar.timegm(dt.timetuple())
timetuple = email.utils.parsedate_tz(date_str)
if timetuple:
return calendar.timegm(timetuple) + pm_delta * 3600
return calendar.timegm(timetuple) + pm_delta * 3600 - timezone.total_seconds()
def determine_ext(url, default_ext='unknown_video'):
@ -3199,7 +3215,7 @@ def strip_jsonp(code):
r'\g<callback_data>', code)
def js_to_json(code, vars={}):
def js_to_json(code, vars={}, *, strict=False):
# vars is a dict of var, val pairs to substitute
COMMENT_RE = r'/\*(?:(?!\*/).)*?\*/|//[^\n]*\n'
SKIP_RE = fr'\s*(?:{COMMENT_RE})?\s*'
@ -3233,14 +3249,17 @@ def fix_kv(m):
if v in vars:
return vars[v]
if strict:
raise ValueError(f'Unknown value: {v}')
return '"%s"' % v
def create_map(mobj):
return json.dumps(dict(json.loads(js_to_json(mobj.group(1) or '[]', vars=vars))))
code = re.sub(r'new Date\((".+")\)', r'\g<1>', code)
code = re.sub(r'new Map\((\[.*?\])?\)', create_map, code)
if not strict:
code = re.sub(r'new Date\((".+")\)', r'\g<1>', code)
return re.sub(r'''(?sx)
"(?:[^"\\]*(?:\\\\|\\['"nurtbfx/\n]))*[^"\\]*"|