From 6d3e7424bfe8cfdbd5931a37519ca7faafff642d Mon Sep 17 00:00:00 2001 From: pukkandan Date: Tue, 16 Aug 2022 06:53:45 +0530 Subject: [PATCH] [jsinterp] Fix for youtube player c81bbb4a --- test/test_jsinterp.py | 5 +++++ test/test_youtube_signature.py | 4 ++++ yt_dlp/jsinterp.py | 30 +++++++++++++++--------------- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/test/test_jsinterp.py b/test/test_jsinterp.py index c97f6dcfb9..665af4668a 100644 --- a/test/test_jsinterp.py +++ b/test/test_jsinterp.py @@ -212,6 +212,11 @@ def test_comma(self): ''') self.assertEqual(jsi.call_function('x'), 7) + jsi = JSInterpreter(''' + function x() { return (l=[0,1,2,3], function(a, b){return a+b})((l[1], l[2]), l[3]) } + ''') + self.assertEqual(jsi.call_function('x'), 5) + def test_void(self): jsi = JSInterpreter(''' function x() { return void 42; } diff --git a/test/test_youtube_signature.py b/test/test_youtube_signature.py index 79bbfc3237..0ac4fd6028 100644 --- a/test/test_youtube_signature.py +++ b/test/test_youtube_signature.py @@ -102,6 +102,10 @@ 'https://www.youtube.com/s/player/4c3f79c5/player_ias.vflset/en_US/base.js', 'TDCstCG66tEAO5pR9o', 'dbxNtZ14c-yWyw', ), + ( + 'https://www.youtube.com/s/player/c81bbb4a/player_ias.vflset/en_US/base.js', + 'gre3EcLurNY2vqp94', 'Z9DfGxWP115WTg', + ), ] diff --git a/yt_dlp/jsinterp.py b/yt_dlp/jsinterp.py index 87f141476c..47cca11761 100644 --- a/yt_dlp/jsinterp.py +++ b/yt_dlp/jsinterp.py @@ -33,19 +33,19 @@ '==': operator.eq, '!=': operator.ne, - '<=': operator.le, - '>=': operator.ge, - '<': operator.lt, - '>': operator.gt, + '<=': lambda a, b: (a or 0) <= (b or 0), + '>=': lambda a, b: (a or 0) >= (b or 0), + '<': lambda a, b: (a or 0) < (b or 0), + '>': lambda a, b: (a or 0) > (b or 0), '>>': operator.rshift, '<<': operator.lshift, - '+': operator.add, - '-': operator.sub, + '+': lambda a, b: (a or 0) + (b or 0), + '-': lambda a, b: (a or 0) - (b or 0), - '*': operator.mul, - '/': operator.truediv, + '*': lambda a, b: (a or 0) * (b or 0), + '/': lambda a, b: (a or 0) / b, '%': operator.mod, '**': operator.pow, @@ -339,11 +339,12 @@ def interpret_statement(self, stmt, local_vars, allow_recursion=100): # Comma separated statements sub_expressions = list(self._separate(expr)) - expr = sub_expressions.pop().strip() if sub_expressions else '' - for sub_expr in sub_expressions: - ret, should_abort = self.interpret_statement(sub_expr, local_vars, allow_recursion) - if should_abort: - return ret, True + if len(sub_expressions) > 1: + for sub_expr in sub_expressions: + ret, should_abort = self.interpret_statement(sub_expr, local_vars, allow_recursion) + if should_abort: + return ret, True + return ret, False for m in re.finditer(rf'''(?x) (?P\+\+|--)(?P{_NAME_RE})| @@ -422,8 +423,7 @@ def interpret_statement(self, stmt, local_vars, allow_recursion=100): if not separated: continue 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 + return self._operator(op, left_val, right_expr, expr, local_vars, allow_recursion), should_return if m and m.group('attribute'): variable = m.group('var')