|
|
@ -14,9 +14,10 @@ import string
|
|
|
|
|
|
|
|
|
|
|
|
from test.helper import FakeYDL
|
|
|
|
from test.helper import FakeYDL
|
|
|
|
from youtube_dl.extractor import YoutubeIE
|
|
|
|
from youtube_dl.extractor import YoutubeIE
|
|
|
|
|
|
|
|
from youtube_dl.jsinterp import JSInterpreter
|
|
|
|
from youtube_dl.compat import compat_str, compat_urlretrieve
|
|
|
|
from youtube_dl.compat import compat_str, compat_urlretrieve
|
|
|
|
|
|
|
|
|
|
|
|
_TESTS = [
|
|
|
|
_SIG_TESTS = [
|
|
|
|
(
|
|
|
|
(
|
|
|
|
'https://s.ytimg.com/yts/jsbin/html5player-vflHOr_nV.js',
|
|
|
|
'https://s.ytimg.com/yts/jsbin/html5player-vflHOr_nV.js',
|
|
|
|
86,
|
|
|
|
86,
|
|
|
@ -64,6 +65,25 @@ _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',
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestPlayerInfo(unittest.TestCase):
|
|
|
|
class TestPlayerInfo(unittest.TestCase):
|
|
|
|
def test_youtube_extract_player_info(self):
|
|
|
|
def test_youtube_extract_player_info(self):
|
|
|
@ -95,35 +115,54 @@ class TestSignature(unittest.TestCase):
|
|
|
|
os.mkdir(self.TESTDATA_DIR)
|
|
|
|
os.mkdir(self.TESTDATA_DIR)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def make_tfunc(url, sig_input, expected_sig):
|
|
|
|
def t_factory(name, sig_func, url_pattern):
|
|
|
|
m = re.match(r'.*-([a-zA-Z0-9_-]+)(?:/watch_as3|/html5player)?\.[a-z]+$', url)
|
|
|
|
def make_tfunc(url, sig_input, expected_sig):
|
|
|
|
assert m, '%r should follow URL format' % url
|
|
|
|
m = url_pattern.match(url)
|
|
|
|
test_id = m.group(1)
|
|
|
|
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 test_func(self):
|
|
|
|
def signature(jscode, sig_input):
|
|
|
|
basename = 'player-%s.js' % test_id
|
|
|
|
func = YoutubeIE(FakeYDL())._parse_sig_js(jscode)
|
|
|
|
fn = os.path.join(self.TESTDATA_DIR, basename)
|
|
|
|
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()
|
|
|
|
def n_sig(jscode, sig_input):
|
|
|
|
ie = YoutubeIE(ydl)
|
|
|
|
# Pending implementation of _extract_n_function_name() or similar in
|
|
|
|
with io.open(fn, encoding='utf-8') as testf:
|
|
|
|
# youtube.py, hard-code here
|
|
|
|
jscode = testf.read()
|
|
|
|
# funcname = YoutubeIE(FakeYDL())._extract_n_function_name(jscode)
|
|
|
|
func = ie._parse_sig_js(jscode)
|
|
|
|
import re
|
|
|
|
src_sig = (
|
|
|
|
funcname = re.search(r'[=(,&|](\w+)\(\w+\),\w+\.set\("n",', jscode)
|
|
|
|
compat_str(string.printable[:sig_input])
|
|
|
|
funcname = funcname and funcname.group(1)
|
|
|
|
if isinstance(sig_input, int) else sig_input)
|
|
|
|
return JSInterpreter(jscode).call_function(funcname, sig_input)
|
|
|
|
got_sig = func(src_sig)
|
|
|
|
|
|
|
|
self.assertEqual(got_sig, expected_sig)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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_nsig_test = t_factory(
|
|
|
|
make_tfunc(*test_spec)
|
|
|
|
'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__':
|
|
|
|
if __name__ == '__main__':
|
|
|
|