test_youtube_signature.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/env python
  2. # Allow direct execution
  3. import os
  4. import sys
  5. import unittest
  6. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  7. import io
  8. import re
  9. import string
  10. from youtube_dl.extractor import YoutubeIE
  11. from youtube_dl.utils import compat_str, compat_urlretrieve
  12. _TESTS = [
  13. (
  14. u'https://s.ytimg.com/yts/jsbin/html5player-vflHOr_nV.js',
  15. u'js',
  16. 86,
  17. u'>=<;:/.-[+*)(\'&%$#"!ZYX0VUTSRQPONMLKJIHGFEDCBA\\yxwvutsrqponmlkjihgfedcba987654321',
  18. ),
  19. (
  20. u'https://s.ytimg.com/yts/jsbin/html5player-vfldJ8xgI.js',
  21. u'js',
  22. 85,
  23. u'3456789a0cdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRS[UVWXYZ!"#$%&\'()*+,-./:;<=>?@',
  24. ),
  25. (
  26. u'https://s.ytimg.com/yts/jsbin/html5player-vfle-mVwz.js',
  27. u'js',
  28. 90,
  29. u']\\[@?>=<;:/.-,+*)(\'&%$#"hZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjiagfedcb39876',
  30. ),
  31. (
  32. u'https://s.ytimg.com/yts/jsbin/html5player-en_US-vfl0Cbn9e.js',
  33. u'js',
  34. 84,
  35. u'O1I3456789abcde0ghijklmnopqrstuvwxyzABCDEFGHfJKLMN2PQRSTUVW@YZ!"#$%&\'()*+,-./:;<=',
  36. ),
  37. (
  38. u'https://s.ytimg.com/yts/jsbin/html5player-en_US-vflXGBaUN.js',
  39. u'js',
  40. u'2ACFC7A61CA478CD21425E5A57EBD73DDC78E22A.2094302436B2D377D14A3BBA23022D023B8BC25AA',
  41. u'A52CB8B320D22032ABB3A41D773D2B6342034902.A22E87CDD37DBE75A5E52412DC874AC16A7CFCA2',
  42. ),
  43. (
  44. u'http://s.ytimg.com/yts/swfbin/player-vfl5vIhK2/watch_as3.swf',
  45. u'swf',
  46. 86,
  47. u'O1I3456789abcde0ghijklmnopqrstuvwxyzABCDEFGHfJKLMN2PQRSTUVWXY\\!"#$%&\'()*+,-./:;<=>?'
  48. ),
  49. (
  50. u'http://s.ytimg.com/yts/swfbin/player-vflmDyk47/watch_as3.swf',
  51. u'swf',
  52. u'F375F75BF2AFDAAF2666E43868D46816F83F13E81C46.3725A8218E446A0DECD33F79DC282994D6AA92C92C9',
  53. u'9C29AA6D499282CD97F33DCED0A644E8128A5273.64C18E31F38361864D86834E6662FAADFA2FB57F'
  54. ),
  55. (
  56. u'https://s.ytimg.com/yts/jsbin/html5player-en_US-vflBb0OQx.js',
  57. u'js',
  58. 84,
  59. u'123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQ0STUVWXYZ!"#$%&\'()*+,@./:;<=>'
  60. )
  61. ]
  62. class TestSignature(unittest.TestCase):
  63. def setUp(self):
  64. TEST_DIR = os.path.dirname(os.path.abspath(__file__))
  65. self.TESTDATA_DIR = os.path.join(TEST_DIR, 'testdata')
  66. if not os.path.exists(self.TESTDATA_DIR):
  67. os.mkdir(self.TESTDATA_DIR)
  68. def make_tfunc(url, stype, sig_input, expected_sig):
  69. m = re.match(r'.*-([a-zA-Z0-9_-]+)(?:/watch_as3)?\.[a-z]+$', url)
  70. assert m, '%r should follow URL format' % url
  71. test_id = m.group(1)
  72. def test_func(self):
  73. basename = 'player-%s.%s' % (test_id, stype)
  74. fn = os.path.join(self.TESTDATA_DIR, basename)
  75. if not os.path.exists(fn):
  76. compat_urlretrieve(url, fn)
  77. ie = YoutubeIE()
  78. if stype == 'js':
  79. with io.open(fn, encoding='utf-8') as testf:
  80. jscode = testf.read()
  81. func = ie._parse_sig_js(jscode)
  82. else:
  83. assert stype == 'swf'
  84. with open(fn, 'rb') as testf:
  85. swfcode = testf.read()
  86. func = ie._parse_sig_swf(swfcode)
  87. src_sig = (
  88. compat_str(string.printable[:sig_input])
  89. if isinstance(sig_input, int) else sig_input)
  90. got_sig = func(src_sig)
  91. self.assertEqual(got_sig, expected_sig)
  92. test_func.__name__ = str('test_signature_' + stype + '_' + test_id)
  93. setattr(TestSignature, test_func.__name__, test_func)
  94. for test_spec in _TESTS:
  95. make_tfunc(*test_spec)
  96. if __name__ == '__main__':
  97. unittest.main()