aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordirkf <fieldhouse@gmx.net>2024-08-06 20:44:30 +0100
committerdirkf <fieldhouse@gmx.net>2024-08-06 20:51:38 +0100
commitdbc08fba83a0e6c5215e8cec97eb4076f85eee5f (patch)
tree3bd333f665ca84cadc1f2dcc9d69ec0c6951a885
parent71223bff39551a11b6959a3de2dd9e2f070f3c4f (diff)
[jsinterp] Improve slice implementation for player b12cc44b
Partly taken from yt-dlp/yt-dlp#10664, thx seproDev Fixes #32896
-rw-r--r--test/test_jsinterp.py28
-rw-r--r--test/test_youtube_signature.py4
-rw-r--r--youtube_dl/jsinterp.py13
3 files changed, 42 insertions, 3 deletions
diff --git a/test/test_jsinterp.py b/test/test_jsinterp.py
index 104e766be..c7a4f2cbf 100644
--- a/test/test_jsinterp.py
+++ b/test/test_jsinterp.py
@@ -425,6 +425,34 @@ class TestJSInterpreter(unittest.TestCase):
self._test(jsi, [''], args=['', '-'])
self._test(jsi, [], args=['', ''])
+ def test_slice(self):
+ self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice()}', [0, 1, 2, 3, 4, 5, 6, 7, 8])
+ self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(0)}', [0, 1, 2, 3, 4, 5, 6, 7, 8])
+ self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(5)}', [5, 6, 7, 8])
+ self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(99)}', [])
+ self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(-2)}', [7, 8])
+ self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(-99)}', [0, 1, 2, 3, 4, 5, 6, 7, 8])
+ self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(0, 0)}', [])
+ self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(1, 0)}', [])
+ self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(0, 1)}', [0])
+ self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(3, 6)}', [3, 4, 5])
+ self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(1, -1)}', [1, 2, 3, 4, 5, 6, 7])
+ self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(-1, 1)}', [])
+ self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(-3, -1)}', [6, 7])
+ self._test('function f(){return "012345678".slice()}', '012345678')
+ self._test('function f(){return "012345678".slice(0)}', '012345678')
+ self._test('function f(){return "012345678".slice(5)}', '5678')
+ self._test('function f(){return "012345678".slice(99)}', '')
+ self._test('function f(){return "012345678".slice(-2)}', '78')
+ self._test('function f(){return "012345678".slice(-99)}', '012345678')
+ self._test('function f(){return "012345678".slice(0, 0)}', '')
+ self._test('function f(){return "012345678".slice(1, 0)}', '')
+ self._test('function f(){return "012345678".slice(0, 1)}', '0')
+ self._test('function f(){return "012345678".slice(3, 6)}', '345')
+ self._test('function f(){return "012345678".slice(1, -1)}', '1234567')
+ self._test('function f(){return "012345678".slice(-1, 1)}', '')
+ self._test('function f(){return "012345678".slice(-3, -1)}', '67')
+
if __name__ == '__main__':
unittest.main()
diff --git a/test/test_youtube_signature.py b/test/test_youtube_signature.py
index 1c5f667f5..56e92fac5 100644
--- a/test/test_youtube_signature.py
+++ b/test/test_youtube_signature.py
@@ -178,6 +178,10 @@ _NSIG_TESTS = [
'https://www.youtube.com/s/player/20dfca59/player_ias.vflset/en_US/base.js',
'-fLCxedkAk4LUTK2', 'O8kfRq1y1eyHGw',
),
+ (
+ 'https://www.youtube.com/s/player/b12cc44b/player_ias.vflset/en_US/base.js',
+ 'keLa5R2U00sR9SQK', 'N1OGyujjEwMnLw',
+ ),
]
diff --git a/youtube_dl/jsinterp.py b/youtube_dl/jsinterp.py
index 949f77775..a616ad070 100644
--- a/youtube_dl/jsinterp.py
+++ b/youtube_dl/jsinterp.py
@@ -925,9 +925,16 @@ class JSInterpreter(object):
obj.reverse()
return obj
elif member == 'slice':
- assertion(isinstance(obj, list), 'must be applied on a list')
- assertion(len(argvals) == 1, 'takes exactly one argument')
- return obj[argvals[0]:]
+ assertion(isinstance(obj, (list, compat_str)), 'must be applied on a list or string')
+ # From [1]:
+ # .slice() - like [:]
+ # .slice(n) - like [n:] (not [slice(n)]
+ # .slice(m, n) - like [m:n] or [slice(m, n)]
+ # [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
+ assertion(len(argvals) <= 2, 'takes between 0 and 2 arguments')
+ if len(argvals) < 2:
+ argvals += (None,)
+ return obj[slice(*argvals)]
elif member == 'splice':
assertion(isinstance(obj, list), 'must be applied on a list')
assertion(argvals, 'takes one or more arguments')