aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/test_jsinterp.py66
-rw-r--r--test/test_youtube_lists.py37
-rw-r--r--test/test_youtube_signature.py99
3 files changed, 151 insertions, 51 deletions
diff --git a/test/test_jsinterp.py b/test/test_jsinterp.py
index c24b8ca74..acdabffb1 100644
--- a/test/test_jsinterp.py
+++ b/test/test_jsinterp.py
@@ -112,6 +112,72 @@ class TestJSInterpreter(unittest.TestCase):
''')
self.assertEqual(jsi.call_function('z'), 5)
+ def test_for_loop(self):
+ # function x() { a=0; for (i=0; i-10; i++) {a++} a }
+ jsi = JSInterpreter('''
+ function x() { a=0; for (i=0; i-10; i = i + 1) {a++} a }
+ ''')
+ self.assertEqual(jsi.call_function('x'), 10)
+
+ def test_switch(self):
+ jsi = JSInterpreter('''
+ function x(f) { switch(f){
+ case 1:f+=1;
+ case 2:f+=2;
+ case 3:f+=3;break;
+ case 4:f+=4;
+ default:f=0;
+ } return f }
+ ''')
+ self.assertEqual(jsi.call_function('x', 1), 7)
+ self.assertEqual(jsi.call_function('x', 3), 6)
+ self.assertEqual(jsi.call_function('x', 5), 0)
+
+ def test_switch_default(self):
+ jsi = JSInterpreter('''
+ function x(f) { switch(f){
+ case 2: f+=2;
+ default: f-=1;
+ case 5:
+ case 6: f+=6;
+ case 0: break;
+ case 1: f+=1;
+ } return f }
+ ''')
+ self.assertEqual(jsi.call_function('x', 1), 2)
+ self.assertEqual(jsi.call_function('x', 5), 11)
+ self.assertEqual(jsi.call_function('x', 9), 14)
+
+ def test_try(self):
+ jsi = JSInterpreter('''
+ function x() { try{return 10} catch(e){return 5} }
+ ''')
+ self.assertEqual(jsi.call_function('x'), 10)
+
+ def test_for_loop_continue(self):
+ jsi = JSInterpreter('''
+ function x() { a=0; for (i=0; i-10; i++) { continue; a++ } 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 }
+ ''')
+ self.assertEqual(jsi.call_function('x'), 0)
+
+ def test_literal_list(self):
+ jsi = JSInterpreter('''
+ function x() { [1, 2, "asdf", [5, 6, 7]][3] }
+ ''')
+ self.assertEqual(jsi.call_function('x'), [5, 6, 7])
+
+ def test_comma(self):
+ jsi = JSInterpreter('''
+ function x() { a=5; a -= 1, a+=3; return a }
+ ''')
+ self.assertEqual(jsi.call_function('x'), 7)
+
if __name__ == '__main__':
unittest.main()
diff --git a/test/test_youtube_lists.py b/test/test_youtube_lists.py
index cf2fdf14f..07a6b6d06 100644
--- a/test/test_youtube_lists.py
+++ b/test/test_youtube_lists.py
@@ -1,4 +1,5 @@
#!/usr/bin/env python
+# -*- coding: utf-8 -*-
from __future__ import unicode_literals
# Allow direct execution
@@ -9,11 +10,10 @@ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from test.helper import FakeYDL
-
from youtube_dl.extractor import (
+ YoutubeIE,
YoutubePlaylistIE,
YoutubeTabIE,
- YoutubeIE,
)
@@ -25,38 +25,23 @@ class TestYoutubeLists(unittest.TestCase):
def test_youtube_playlist_noplaylist(self):
dl = FakeYDL()
dl.params['noplaylist'] = True
+ dl.params['format'] = 'best'
ie = YoutubePlaylistIE(dl)
result = ie.extract('https://www.youtube.com/watch?v=FXxLjLQi3Fg&list=PLwiyx1dc3P2JR9N8gQaQN_BCvlSlap7re')
self.assertEqual(result['_type'], 'url')
+ result = dl.extract_info(result['url'], download=False, ie_key=result.get('ie_key'), process=False)
self.assertEqual(YoutubeIE().extract_id(result['url']), 'FXxLjLQi3Fg')
- def test_youtube_course(self):
- dl = FakeYDL()
- ie = YoutubePlaylistIE(dl)
- # TODO find a > 100 (paginating?) videos course
- result = ie.extract('https://www.youtube.com/course?list=ECUl4u3cNGP61MdtwGTqZA0MreSaDybji8')
- entries = list(result['entries'])
- self.assertEqual(YoutubeIE().extract_id(entries[0]['url']), 'j9WZyLZCBzs')
- self.assertEqual(len(entries), 25)
- self.assertEqual(YoutubeIE().extract_id(entries[-1]['url']), 'rYefUsYuEp0')
-
def test_youtube_mix(self):
dl = FakeYDL()
- ie = YoutubePlaylistIE(dl)
- result = ie.extract('https://www.youtube.com/watch?v=W01L70IGBgE&index=2&list=RDOQpdSVF_k_w')
- entries = result['entries']
+ dl.params['format'] = 'best'
+ ie = YoutubeTabIE(dl)
+ result = dl.extract_info('https://www.youtube.com/watch?v=uVJ0Il5WvbE&list=PLhQjrBD2T381k8ul4WQ8SQ165XqY149WW',
+ download=False, ie_key=ie.ie_key(), process=True)
+ entries = (result or {}).get('entries', [{'id': 'not_found', }])
self.assertTrue(len(entries) >= 50)
original_video = entries[0]
- self.assertEqual(original_video['id'], 'OQpdSVF_k_w')
-
- def test_youtube_toptracks(self):
- print('Skipping: The playlist page gives error 500')
- return
- dl = FakeYDL()
- ie = YoutubePlaylistIE(dl)
- result = ie.extract('https://www.youtube.com/playlist?list=MCUS')
- entries = result['entries']
- self.assertEqual(len(entries), 100)
+ self.assertEqual(original_video['id'], 'uVJ0Il5WvbE')
def test_youtube_flat_playlist_extraction(self):
dl = FakeYDL()
@@ -67,7 +52,7 @@ class TestYoutubeLists(unittest.TestCase):
entries = list(result['entries'])
self.assertTrue(len(entries) == 1)
video = entries[0]
- self.assertEqual(video['_type'], 'url_transparent')
+ self.assertEqual(video['_type'], 'url')
self.assertEqual(video['ie_key'], 'Youtube')
self.assertEqual(video['id'], 'BaW_jenozKc')
self.assertEqual(video['url'], 'BaW_jenozKc')
diff --git a/test/test_youtube_signature.py b/test/test_youtube_signature.py
index 627d4cb92..fc5e9828e 100644
--- a/test/test_youtube_signature.py
+++ b/test/test_youtube_signature.py
@@ -14,9 +14,10 @@ import string
from test.helper import FakeYDL
from youtube_dl.extractor import YoutubeIE
+from youtube_dl.jsinterp import JSInterpreter
from youtube_dl.compat import compat_str, compat_urlretrieve
-_TESTS = [
+_SIG_TESTS = [
(
'https://s.ytimg.com/yts/jsbin/html5player-vflHOr_nV.js',
86,
@@ -64,6 +65,33 @@ _TESTS = [
)
]
+_NSIG_TESTS = [
+ (
+ 'https://www.youtube.com/s/player/9216d1f7/player_ias.vflset/en_US/base.js',
+ 'SLp9F5bwjAdhE9F-', 'gWnb9IK2DJ8Q1w',
+ ),
+ (
+ 'https://www.youtube.com/s/player/f8cb7a3b/player_ias.vflset/en_US/base.js',
+ 'oBo2h5euWy6osrUt', 'ivXHpm7qJjJN',
+ ),
+ (
+ 'https://www.youtube.com/s/player/2dfe380c/player_ias.vflset/en_US/base.js',
+ 'oBo2h5euWy6osrUt', '3DIBbn3qdQ',
+ ),
+ (
+ 'https://www.youtube.com/s/player/f1ca6900/player_ias.vflset/en_US/base.js',
+ 'cu3wyu6LQn2hse', 'jvxetvmlI9AN9Q',
+ ),
+ (
+ 'https://www.youtube.com/s/player/8040e515/player_ias.vflset/en_US/base.js',
+ 'wvOFaY-yjgDuIEg5', 'HkfBFDHmgw4rsw',
+ ),
+ (
+ 'https://www.youtube.com/s/player/e06dea74/player_ias.vflset/en_US/base.js',
+ 'AiuodmaDDYw8d3y4bf', 'ankd8eza2T6Qmw',
+ ),
+]
+
class TestPlayerInfo(unittest.TestCase):
def test_youtube_extract_player_info(self):
@@ -90,40 +118,61 @@ class TestPlayerInfo(unittest.TestCase):
class TestSignature(unittest.TestCase):
def setUp(self):
TEST_DIR = os.path.dirname(os.path.abspath(__file__))
- self.TESTDATA_DIR = os.path.join(TEST_DIR, 'testdata')
+ self.TESTDATA_DIR = os.path.join(TEST_DIR, 'testdata/sigs')
if not os.path.exists(self.TESTDATA_DIR):
os.mkdir(self.TESTDATA_DIR)
+ def tearDown(self):
+ try:
+ for f in os.listdir(self.TESTDATA_DIR):
+ os.remove(f)
+ except OSError:
+ pass
+
+
+def t_factory(name, sig_func, url_pattern):
+ def make_tfunc(url, sig_input, expected_sig):
+ m = url_pattern.match(url)
+ assert m, '%r should follow URL format' % url
+ test_id = m.group('id')
+
+ def test_func(self):
+ basename = 'player-{0}-{1}.js'.format(name, test_id)
+ fn = os.path.join(self.TESTDATA_DIR, basename)
+
+ if not os.path.exists(fn):
+ compat_urlretrieve(url, fn)
+ with io.open(fn, encoding='utf-8') as testf:
+ jscode = testf.read()
+ self.assertEqual(sig_func(jscode, sig_input), expected_sig)
+
+ test_func.__name__ = str('test_{0}_js_{1}'.format(name, test_id))
+ setattr(TestSignature, test_func.__name__, test_func)
+ return make_tfunc
-def make_tfunc(url, sig_input, expected_sig):
- m = re.match(r'.*-([a-zA-Z0-9_-]+)(?:/watch_as3|/html5player)?\.[a-z]+$', url)
- assert m, '%r should follow URL format' % url
- test_id = m.group(1)
- def test_func(self):
- basename = 'player-%s.js' % test_id
- fn = os.path.join(self.TESTDATA_DIR, basename)
+def signature(jscode, sig_input):
+ func = YoutubeIE(FakeYDL())._parse_sig_js(jscode)
+ src_sig = (
+ compat_str(string.printable[:sig_input])
+ if isinstance(sig_input, int) else sig_input)
+ return func(src_sig)
- if not os.path.exists(fn):
- compat_urlretrieve(url, fn)
- ydl = FakeYDL()
- ie = YoutubeIE(ydl)
- with io.open(fn, encoding='utf-8') as testf:
- jscode = testf.read()
- func = ie._parse_sig_js(jscode)
- src_sig = (
- compat_str(string.printable[:sig_input])
- if isinstance(sig_input, int) else sig_input)
- got_sig = func(src_sig)
- self.assertEqual(got_sig, expected_sig)
+def n_sig(jscode, sig_input):
+ funcname = YoutubeIE(FakeYDL())._extract_n_function_name(jscode)
+ return JSInterpreter(jscode).call_function(funcname, sig_input)
- test_func.__name__ = str('test_signature_js_' + test_id)
- setattr(TestSignature, test_func.__name__, test_func)
+make_sig_test = t_factory(
+ 'signature', signature, re.compile(r'.*-(?P<id>[a-zA-Z0-9_-]+)(?:/watch_as3|/html5player)?\.[a-z]+$'))
+for test_spec in _SIG_TESTS:
+ make_sig_test(*test_spec)
-for test_spec in _TESTS:
- make_tfunc(*test_spec)
+make_nsig_test = t_factory(
+ 'nsig', n_sig, re.compile(r'.+/player/(?P<id>[a-zA-Z0-9_-]+)/.+.js$'))
+for test_spec in _NSIG_TESTS:
+ make_nsig_test(*test_spec)
if __name__ == '__main__':