]> git.bitcoin.ninja Git - youtube-dl/blob - devscripts/youtube_genalgo.py
66019ee55aa3ecd22b33cd7cf8760dcc3e4fe2aa
[youtube-dl] / devscripts / youtube_genalgo.py
1 #!/usr/bin/env python
2
3 # Generate youtube signature algorithm from test cases
4
5 import sys
6
7 tests = [
8     # 92 - vflQw-fB4 2013/07/17
9     ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[]}|:;?/>.<'`~\"",
10      "mrtyuioplkjhgfdsazxcvbnq1234567890QWERTY}IOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[]\"|:;"),
11     # 90
12     ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[]}|:;?/>.<'`",
13      "mrtyuioplkjhgfdsazxcvbne1234567890QWER[YUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={`]}|"),
14     # 89 
15     ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[]}|:;?/>.<'",
16      "/?;:|}<[{=+-_)(*&^%$#@!MqBVCXZASDFGHJKLPOIUYTREWQ0987654321mnbvcxzasdfghjklpoiuyt"),
17     # 88 - vflapUV9V 2013/08/28
18     ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[]}|:;?/>.<",
19      "ioplkjhgfdsazxcvbnm12<4567890QWERTYUIOZLKJHGFDSAeXCVBNM!@#$%^&*()_-+={[]}|:;?/>.3"),
20     # 87
21     ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$^&*()_-+={[]}|:;?/>.<",
22      "uioplkjhgfdsazxcvbnm1t34567890QWE2TYUIOPLKJHGFDSAZXCVeNM!@#$^&*()_-+={[]}|:;?/>.<"),
23     # 86 - vfluy6kdb 2013/09/06
24     ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[|};?/>.<",
25      "yuioplkjhgfdsazxcvbnm12345678q0QWrRTYUIOELKJHGFD-AZXCVBNM!@#$%^&*()_<+={[|};?/>.S"),
26     # 85 - vflkuzxcs 2013/09/11
27     ('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[',
28      '3456789a0cdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRS[UVWXYZ!"#$%&\'()*+,-./:;<=>?@'),
29     # 84 - vflg0g8PQ 2013/08/29 (sporadic)
30     ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[};?>.<",
31      ">?;}[{=+-_)(*&^%$#@!MNBVCXZASDFGHJKLPOIUYTREWq0987654321mnbvcxzasdfghjklpoiuytr"),
32     # 83
33     ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!#$%^&*()_+={[};?/>.<",
34      ".>/?;}[{=+_)(*&^%<#!MNBVCXZASPFGHJKLwOIUYTREWQ0987654321mnbvcxzasdfghjklpoiuytreq"),
35     # 82 - vflGNjMhJ 2013/09/12
36     ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKHGFDSAZXCVBNM!@#$%^&*(-+={[};?/>.<",
37      ".>/?;}[<=+-(*&^%$#@!MNBVCXeASDFGHKLPOqUYTREWQ0987654321mnbvcxzasdfghjklpoiuytrIwZ"),
38     # 81 - vflLC8JvQ 2013/07/25
39     ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKHGFDSAZXCVBNM!@#$%^&*(-+={[};?/>.",
40      "C>/?;}[{=+-(*&^%$#@!MNBVYXZASDFGHKLPOIU.TREWQ0q87659321mnbvcxzasdfghjkl4oiuytrewp"),
41     # 80 - vflZK4ZYR 2013/08/23 (sporadic)
42     ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKHGFDSAZXCVBNM!@#$%^&*(-+={[};?/>",
43      "wertyuioplkjhgfdsaqxcvbnm1234567890QWERTYUIOPLKHGFDSAZXCVBNM!@#$%^&z(-+={[};?/>"),
44     # 79 - vflLC8JvQ 2013/07/25 (sporadic)
45     ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKHGFDSAZXCVBNM!@#$%^&*(-+={[};?/",
46      "Z?;}[{=+-(*&^%$#@!MNBVCXRASDFGHKLPOIUYT/EWQ0q87659321mnbvcxzasdfghjkl4oiuytrewp"),
47 ]
48
49 tests_age_gate = [
50     # 86 - vflqinMWD
51     ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[|};?/>.<",
52      "ertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!/#$%^&*()_-+={[|};?@"),
53 ]
54
55 def find_matching(wrong, right):
56     idxs = [wrong.index(c) for c in right]
57     return compress(idxs)
58     return ('s[%d]' % i for i in idxs)
59
60 def compress(idxs):
61     def _genslice(start, end, step):
62         starts = '' if start == 0 else str(start)
63         ends = ':%d' % (end+step)
64         steps = '' if step == 1 else (':%d' % step)
65         return 's[%s%s%s]' % (starts, ends, steps)
66
67     step = None
68     for i, prev in zip(idxs[1:], idxs[:-1]):
69         if step is not None:
70             if i - prev == step:
71                 continue
72             yield _genslice(start, prev, step)
73             step = None
74             continue
75         if i - prev in [-1, 1]:
76             step = i - prev
77             start = prev
78             continue
79         else:
80             yield 's[%d]' % prev
81     if step is None:
82         yield 's[%d]' % i
83     else:
84         yield _genslice(start, i, step)
85
86 def _assert_compress(inp, exp):
87     res = list(compress(inp))
88     if res != exp:
89         print('Got %r, expected %r' % (res, exp))
90         assert res == exp
91 _assert_compress([0,2,4,6], ['s[0]', 's[2]', 's[4]', 's[6]'])
92 _assert_compress([0,1,2,4,6,7], ['s[:3]', 's[4]', 's[6:8]'])
93 _assert_compress([8,0,1,2,4,7,6,9], ['s[8]', 's[:3]', 's[4]', 's[7:5:-1]', 's[9]'])
94
95 def gen(wrong, right, indent):
96     code = ' + '.join(find_matching(wrong, right))
97     return 'if len(s) == %d:\n%s    return %s\n' % (len(wrong), indent, code)
98
99 def genall(tests):
100     indent = ' ' * 8
101     return indent + (indent + 'el').join(gen(wrong, right, indent) for wrong,right in tests)
102
103 def main():
104     print(genall(tests))
105     print(u'    Age gate:')
106     print(genall(tests_age_gate))
107
108 if __name__ == '__main__':
109     main()