You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.6 KiB

  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-vflXGBaUN.js',
  33. u'js',
  34. u'2ACFC7A61CA478CD21425E5A57EBD73DDC78E22A.2094302436B2D377D14A3BBA23022D023B8BC25AA',
  35. u'A52CB8B320D22032ABB3A41D773D2B6342034902.A22E87CDD37DBE75A5E52412DC874AC16A7CFCA2',
  36. ),
  37. ]
  38. class TestSignature(unittest.TestCase):
  39. def setUp(self):
  40. TEST_DIR = os.path.dirname(os.path.abspath(__file__))
  41. self.TESTDATA_DIR = os.path.join(TEST_DIR, 'testdata')
  42. if not os.path.exists(self.TESTDATA_DIR):
  43. os.mkdir(self.TESTDATA_DIR)
  44. def make_tfunc(url, stype, sig_input, expected_sig):
  45. basename = url.rpartition('/')[2]
  46. m = re.match(r'.*-([a-zA-Z0-9_-]+)\.[a-z]+$', basename)
  47. assert m, '%r should follow URL format' % basename
  48. test_id = m.group(1)
  49. def test_func(self):
  50. fn = os.path.join(self.TESTDATA_DIR, basename)
  51. if not os.path.exists(fn):
  52. compat_urlretrieve(url, fn)
  53. ie = YoutubeIE()
  54. if stype == 'js':
  55. with io.open(fn, encoding='utf-8') as testf:
  56. jscode = testf.read()
  57. func = ie._parse_sig_js(jscode)
  58. else:
  59. assert stype == 'swf'
  60. with open(fn, 'rb') as testf:
  61. swfcode = testf.read()
  62. func = ie._parse_sig_swf(swfcode)
  63. src_sig = (
  64. compat_str(string.printable[:sig_input])
  65. if isinstance(sig_input, int) else sig_input)
  66. got_sig = func(src_sig)
  67. self.assertEqual(got_sig, expected_sig)
  68. test_func.__name__ = str('test_signature_' + stype + '_' + test_id)
  69. setattr(TestSignature, test_func.__name__, test_func)
  70. for test_spec in _TESTS:
  71. make_tfunc(*test_spec)
  72. if __name__ == '__main__':
  73. unittest.main()