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.

81 lines
2.3 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/swfbin/watch_as3-vflg5GhxU.swf',
  27. u'swf',
  28. 82,
  29. u':/.-,+*)=\'&%$#"!ZYX0VUTSRQPONMLKJIHGFEDCBAzyxw>utsrqponmlkjihgfedcba987654321'
  30. ),
  31. ]
  32. class TestSignature(unittest.TestCase):
  33. def setUp(self):
  34. TEST_DIR = os.path.dirname(os.path.abspath(__file__))
  35. self.TESTDATA_DIR = os.path.join(TEST_DIR, 'testdata')
  36. if not os.path.exists(self.TESTDATA_DIR):
  37. os.mkdir(self.TESTDATA_DIR)
  38. def make_tfunc(url, stype, sig_length, expected_sig):
  39. basename = url.rpartition('/')[2]
  40. m = re.match(r'.*-([a-zA-Z0-9_-]+)\.[a-z]+$', basename)
  41. assert m, '%r should follow URL format' % basename
  42. test_id = m.group(1)
  43. def test_func(self):
  44. fn = os.path.join(self.TESTDATA_DIR, basename)
  45. if not os.path.exists(fn):
  46. compat_urlretrieve(url, fn)
  47. ie = YoutubeIE()
  48. if stype == 'js':
  49. with io.open(fn, encoding='utf-8') as testf:
  50. jscode = testf.read()
  51. func = ie._parse_sig_js(jscode)
  52. else:
  53. assert stype == 'swf'
  54. with open(fn, 'rb') as testf:
  55. swfcode = testf.read()
  56. func = ie._parse_sig_swf(swfcode)
  57. src_sig = compat_str(string.printable[:sig_length])
  58. got_sig = func(src_sig)
  59. self.assertEqual(got_sig, expected_sig)
  60. test_func.__name__ = str('test_signature_' + stype + '_' + test_id)
  61. setattr(TestSignature, test_func.__name__, test_func)
  62. for test_spec in _TESTS:
  63. make_tfunc(*test_spec)
  64. if __name__ == '__main__':
  65. unittest.main()