]> Raphaƫl G. Git Repositories - youtubedl/blob - test/test_youtube_signature.py
Update changelog.
[youtubedl] / test / test_youtube_signature.py
1 #!/usr/bin/env python
2
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
9 from test.helper import global_setup
10 global_setup()
11
12
13 import io
14 import re
15 import string
16
17 from youtube_dl.extractor import YoutubeIE
18 from youtube_dl.utils import compat_str, compat_urlretrieve
19
20 _TESTS = [
21 (
22 u'https://s.ytimg.com/yts/jsbin/html5player-vflHOr_nV.js',
23 u'js',
24 86,
25 u'>=<;:/.-[+*)(\'&%$#"!ZYX0VUTSRQPONMLKJIHGFEDCBA\\yxwvutsrqponmlkjihgfedcba987654321',
26 ),
27 (
28 u'https://s.ytimg.com/yts/jsbin/html5player-vfldJ8xgI.js',
29 u'js',
30 85,
31 u'3456789a0cdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRS[UVWXYZ!"#$%&\'()*+,-./:;<=>?@',
32 ),
33 (
34 u'https://s.ytimg.com/yts/swfbin/watch_as3-vflg5GhxU.swf',
35 u'swf',
36 82,
37 u':/.-,+*)=\'&%$#"!ZYX0VUTSRQPONMLKJIHGFEDCBAzyxw>utsrqponmlkjihgfedcba987654321'
38 ),
39 ]
40
41
42 class TestSignature(unittest.TestCase):
43 def setUp(self):
44 TEST_DIR = os.path.dirname(os.path.abspath(__file__))
45 self.TESTDATA_DIR = os.path.join(TEST_DIR, 'testdata')
46 if not os.path.exists(self.TESTDATA_DIR):
47 os.mkdir(self.TESTDATA_DIR)
48
49
50 def make_tfunc(url, stype, sig_length, expected_sig):
51 basename = url.rpartition('/')[2]
52 m = re.match(r'.*-([a-zA-Z0-9_-]+)\.[a-z]+$', basename)
53 assert m, '%r should follow URL format' % basename
54 test_id = m.group(1)
55
56 def test_func(self):
57 fn = os.path.join(self.TESTDATA_DIR, basename)
58
59 if not os.path.exists(fn):
60 compat_urlretrieve(url, fn)
61
62 ie = YoutubeIE()
63 if stype == 'js':
64 with io.open(fn, encoding='utf-8') as testf:
65 jscode = testf.read()
66 func = ie._parse_sig_js(jscode)
67 else:
68 assert stype == 'swf'
69 with open(fn, 'rb') as testf:
70 swfcode = testf.read()
71 func = ie._parse_sig_swf(swfcode)
72 src_sig = compat_str(string.printable[:sig_length])
73 got_sig = func(src_sig)
74 self.assertEqual(got_sig, expected_sig)
75
76 test_func.__name__ = str('test_signature_' + stype + '_' + test_id)
77 setattr(TestSignature, test_func.__name__, test_func)
78
79 for test_spec in _TESTS:
80 make_tfunc(*test_spec)
81
82
83 if __name__ == '__main__':
84 unittest.main()