logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

test_youtube_signature.py (7588B)


  1. #!/usr/bin/env python
  2. from __future__ import unicode_literals
  3. # Allow direct execution
  4. import os
  5. import sys
  6. import unittest
  7. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  8. import io
  9. import re
  10. import string
  11. from test.helper import FakeYDL
  12. from youtube_dl.extractor import YoutubeIE
  13. from youtube_dl.jsinterp import JSInterpreter
  14. from youtube_dl.compat import compat_str, compat_urlretrieve
  15. _SIG_TESTS = [
  16. (
  17. 'https://s.ytimg.com/yts/jsbin/html5player-vflHOr_nV.js',
  18. 86,
  19. '>=<;:/.-[+*)(\'&%$#"!ZYX0VUTSRQPONMLKJIHGFEDCBA\\yxwvutsrqponmlkjihgfedcba987654321',
  20. ),
  21. (
  22. 'https://s.ytimg.com/yts/jsbin/html5player-vfldJ8xgI.js',
  23. 85,
  24. '3456789a0cdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRS[UVWXYZ!"#$%&\'()*+,-./:;<=>?@',
  25. ),
  26. (
  27. 'https://s.ytimg.com/yts/jsbin/html5player-vfle-mVwz.js',
  28. 90,
  29. ']\\[@?>=<;:/.-,+*)(\'&%$#"hZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjiagfedcb39876',
  30. ),
  31. (
  32. 'https://s.ytimg.com/yts/jsbin/html5player-en_US-vfl0Cbn9e.js',
  33. 84,
  34. 'O1I3456789abcde0ghijklmnopqrstuvwxyzABCDEFGHfJKLMN2PQRSTUVW@YZ!"#$%&\'()*+,-./:;<=',
  35. ),
  36. (
  37. 'https://s.ytimg.com/yts/jsbin/html5player-en_US-vflXGBaUN.js',
  38. '2ACFC7A61CA478CD21425E5A57EBD73DDC78E22A.2094302436B2D377D14A3BBA23022D023B8BC25AA',
  39. 'A52CB8B320D22032ABB3A41D773D2B6342034902.A22E87CDD37DBE75A5E52412DC874AC16A7CFCA2',
  40. ),
  41. (
  42. 'https://s.ytimg.com/yts/jsbin/html5player-en_US-vflBb0OQx.js',
  43. 84,
  44. '123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQ0STUVWXYZ!"#$%&\'()*+,@./:;<=>'
  45. ),
  46. (
  47. 'https://s.ytimg.com/yts/jsbin/html5player-en_US-vfl9FYC6l.js',
  48. 83,
  49. '123456789abcdefghijklmnopqr0tuvwxyzABCDETGHIJKLMNOPQRS>UVWXYZ!"#$%&\'()*+,-./:;<=F'
  50. ),
  51. (
  52. 'https://s.ytimg.com/yts/jsbin/html5player-en_US-vflCGk6yw/html5player.js',
  53. '4646B5181C6C3020DF1D9C7FCFEA.AD80ABF70C39BD369CCCAE780AFBB98FA6B6CB42766249D9488C288',
  54. '82C8849D94266724DC6B6AF89BBFA087EACCD963.B93C07FBA084ACAEFCF7C9D1FD0203C6C1815B6B'
  55. ),
  56. (
  57. 'https://s.ytimg.com/yts/jsbin/html5player-en_US-vflKjOTVq/html5player.js',
  58. '312AA52209E3623129A412D56A40F11CB0AF14AE.3EE09501CB14E3BCDC3B2AE808BF3F1D14E7FBF12',
  59. '112AA5220913623229A412D56A40F11CB0AF14AE.3EE0950FCB14EEBCDC3B2AE808BF331D14E7FBF3',
  60. )
  61. ]
  62. _NSIG_TESTS = [
  63. (
  64. 'https://www.youtube.com/s/player/9216d1f7/player_ias.vflset/en_US/base.js',
  65. 'SLp9F5bwjAdhE9F-', 'gWnb9IK2DJ8Q1w',
  66. ),
  67. (
  68. 'https://www.youtube.com/s/player/f8cb7a3b/player_ias.vflset/en_US/base.js',
  69. 'oBo2h5euWy6osrUt', 'ivXHpm7qJjJN',
  70. ),
  71. (
  72. 'https://www.youtube.com/s/player/2dfe380c/player_ias.vflset/en_US/base.js',
  73. 'oBo2h5euWy6osrUt', '3DIBbn3qdQ',
  74. ),
  75. (
  76. 'https://www.youtube.com/s/player/f1ca6900/player_ias.vflset/en_US/base.js',
  77. 'cu3wyu6LQn2hse', 'jvxetvmlI9AN9Q',
  78. ),
  79. (
  80. 'https://www.youtube.com/s/player/8040e515/player_ias.vflset/en_US/base.js',
  81. 'wvOFaY-yjgDuIEg5', 'HkfBFDHmgw4rsw',
  82. ),
  83. (
  84. 'https://www.youtube.com/s/player/e06dea74/player_ias.vflset/en_US/base.js',
  85. 'AiuodmaDDYw8d3y4bf', 'ankd8eza2T6Qmw',
  86. ),
  87. (
  88. 'https://www.youtube.com/s/player/5dd88d1d/player-plasma-ias-phone-en_US.vflset/base.js',
  89. 'kSxKFLeqzv_ZyHSAt', 'n8gS8oRlHOxPFA',
  90. ),
  91. (
  92. 'https://www.youtube.com/s/player/324f67b9/player_ias.vflset/en_US/base.js',
  93. 'xdftNy7dh9QGnhW', '22qLGxrmX8F1rA',
  94. ),
  95. (
  96. 'https://www.youtube.com/s/player/4c3f79c5/player_ias.vflset/en_US/base.js',
  97. 'TDCstCG66tEAO5pR9o', 'dbxNtZ14c-yWyw',
  98. ),
  99. (
  100. 'https://www.youtube.com/s/player/c81bbb4a/player_ias.vflset/en_US/base.js',
  101. 'gre3EcLurNY2vqp94', 'Z9DfGxWP115WTg',
  102. ),
  103. ]
  104. class TestPlayerInfo(unittest.TestCase):
  105. def test_youtube_extract_player_info(self):
  106. PLAYER_URLS = (
  107. ('https://www.youtube.com/s/player/4c3f79c5/player_ias.vflset/en_US/base.js', '4c3f79c5'),
  108. ('https://www.youtube.com/s/player/64dddad9/player_ias.vflset/en_US/base.js', '64dddad9'),
  109. ('https://www.youtube.com/s/player/64dddad9/player_ias.vflset/fr_FR/base.js', '64dddad9'),
  110. ('https://www.youtube.com/s/player/64dddad9/player-plasma-ias-phone-en_US.vflset/base.js', '64dddad9'),
  111. ('https://www.youtube.com/s/player/64dddad9/player-plasma-ias-phone-de_DE.vflset/base.js', '64dddad9'),
  112. ('https://www.youtube.com/s/player/64dddad9/player-plasma-ias-tablet-en_US.vflset/base.js', '64dddad9'),
  113. # obsolete
  114. ('https://www.youtube.com/yts/jsbin/player_ias-vfle4-e03/en_US/base.js', 'vfle4-e03'),
  115. ('https://www.youtube.com/yts/jsbin/player_ias-vfl49f_g4/en_US/base.js', 'vfl49f_g4'),
  116. ('https://www.youtube.com/yts/jsbin/player_ias-vflCPQUIL/en_US/base.js', 'vflCPQUIL'),
  117. ('https://www.youtube.com/yts/jsbin/player-vflzQZbt7/en_US/base.js', 'vflzQZbt7'),
  118. ('https://www.youtube.com/yts/jsbin/player-en_US-vflaxXRn1/base.js', 'vflaxXRn1'),
  119. ('https://s.ytimg.com/yts/jsbin/html5player-en_US-vflXGBaUN.js', 'vflXGBaUN'),
  120. ('https://s.ytimg.com/yts/jsbin/html5player-en_US-vflKjOTVq/html5player.js', 'vflKjOTVq'),
  121. )
  122. for player_url, expected_player_id in PLAYER_URLS:
  123. player_id = YoutubeIE._extract_player_info(player_url)
  124. self.assertEqual(player_id, expected_player_id)
  125. class TestSignature(unittest.TestCase):
  126. def setUp(self):
  127. TEST_DIR = os.path.dirname(os.path.abspath(__file__))
  128. self.TESTDATA_DIR = os.path.join(TEST_DIR, 'testdata/sigs')
  129. if not os.path.exists(self.TESTDATA_DIR):
  130. os.mkdir(self.TESTDATA_DIR)
  131. def tearDown(self):
  132. try:
  133. for f in os.listdir(self.TESTDATA_DIR):
  134. os.remove(f)
  135. except OSError:
  136. pass
  137. def t_factory(name, sig_func, url_pattern):
  138. def make_tfunc(url, sig_input, expected_sig):
  139. m = url_pattern.match(url)
  140. assert m, '%r should follow URL format' % url
  141. test_id = m.group('id')
  142. def test_func(self):
  143. basename = 'player-{0}-{1}.js'.format(name, test_id)
  144. fn = os.path.join(self.TESTDATA_DIR, basename)
  145. if not os.path.exists(fn):
  146. compat_urlretrieve(url, fn)
  147. with io.open(fn, encoding='utf-8') as testf:
  148. jscode = testf.read()
  149. self.assertEqual(sig_func(jscode, sig_input), expected_sig)
  150. test_func.__name__ = str('test_{0}_js_{1}'.format(name, test_id))
  151. setattr(TestSignature, test_func.__name__, test_func)
  152. return make_tfunc
  153. def signature(jscode, sig_input):
  154. func = YoutubeIE(FakeYDL())._parse_sig_js(jscode)
  155. src_sig = (
  156. compat_str(string.printable[:sig_input])
  157. if isinstance(sig_input, int) else sig_input)
  158. return func(src_sig)
  159. def n_sig(jscode, sig_input):
  160. funcname = YoutubeIE(FakeYDL())._extract_n_function_name(jscode)
  161. return JSInterpreter(jscode).call_function(funcname, sig_input)
  162. make_sig_test = t_factory(
  163. 'signature', signature, re.compile(r'.*-(?P<id>[a-zA-Z0-9_-]+)(?:/watch_as3|/html5player)?\.[a-z]+$'))
  164. for test_spec in _SIG_TESTS:
  165. make_sig_test(*test_spec)
  166. make_nsig_test = t_factory(
  167. 'nsig', n_sig, re.compile(r'.+/player/(?P<id>[a-zA-Z0-9_-]+)/.+.js$'))
  168. for test_spec in _NSIG_TESTS:
  169. make_nsig_test(*test_spec)
  170. if __name__ == '__main__':
  171. unittest.main()