Merge branch 'master' into subtitles_rework
[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
18     ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[]}|:;?/>.<",
19      "J:|}][{=+-_)(*&;%$#@>MNBVCXZASDFGH^KLPOIUYTREWQ0987654321mnbvcxzasdfghrklpoiuytej"),
20     # 87
21     ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$^&*()_-+={[]}|:;?/>.<",
22      "uioplkjhgfdsazxcvbnm1t34567890QWE2TYUIOPLKJHGFDSAZXCVeNM!@#$^&*()_-+={[]}|:;?/>.<"),
23     # 86
24     ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[|};?/>.<",
25      "yuioplkjhgfdsazecvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[|};?/>.<"),
26     # 85
27     ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[};?/>.<",
28      ".>/?;}[{=+-_)(*&^%$#@!MNBVCXZASDFGHJKLPOIUYTREWQ0q876543r1mnbvcx9asdfghjklpoiuyt2"),
29     # 84
30     ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[};?>.<",
31      "<.>?;}[{=+-_)(*&^%$#@!MNBVCXZASDFGHJKLPOIUYTREWQ09876543q1mnbvcxzasdfghjklpoiuew2"),
32     # 83
33     ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!#$%^&*()_+={[};?/>.<",
34      ".>/?;}[{=+_)(*&^%<#!MNBVCXZASPFGHJKLwOIUYTREWQ0987654321mnbvcxzasdfghjklpoiuytreq"),
35     # 82
36     ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKHGFDSAZXCVBNM!@#$%^&*(-+={[};?/>.<",
37      "Q>/?;}[{=+-(*<^%$#@!MNBVCXZASDFGHKLPOIUY8REWT0q&7654321mnbvcxzasdfghjklpoiuytrew9"),
38     # 81 - vflLC8JvQ 2013/07/25
39     ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKHGFDSAZXCVBNM!@#$%^&*(-+={[};?/>.",
40      "C>/?;}[{=+-(*&^%$#@!MNBVYXZASDFGHKLPOIU.TREWQ0q87659321mnbvcxzasdfghjkl4oiuytrewp"),
41     # 79 - vflLC8JvQ 2013/07/25 (sporadic)
42     ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKHGFDSAZXCVBNM!@#$%^&*(-+={[};?/",
43      "Z?;}[{=+-(*&^%$#@!MNBVCXRASDFGHKLPOIUYT/EWQ0q87659321mnbvcxzasdfghjkl4oiuytrewp"),
44 ]
45
46 tests_age_gate = [
47     # 86 - vflqinMWD
48     ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[|};?/>.<",
49      "ertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!/#$%^&*()_-+={[|};?@"),
50 ]
51
52 def find_matching(wrong, right):
53     idxs = [wrong.index(c) for c in right]
54     return compress(idxs)
55     return ('s[%d]' % i for i in idxs)
56
57 def compress(idxs):
58     def _genslice(start, end, step):
59         starts = '' if start == 0 else str(start)
60         ends = ':%d' % (end+step)
61         steps = '' if step == 1 else (':%d' % step)
62         return 's[%s%s%s]' % (starts, ends, steps)
63
64     step = None
65     for i, prev in zip(idxs[1:], idxs[:-1]):
66         if step is not None:
67             if i - prev == step:
68                 continue
69             yield _genslice(start, prev, step)
70             step = None
71             continue
72         if i - prev in [-1, 1]:
73             step = i - prev
74             start = prev
75             continue
76         else:
77             yield 's[%d]' % prev
78     if step is None:
79         yield 's[%d]' % i
80     else:
81         yield _genslice(start, i, step)
82
83 def _assert_compress(inp, exp):
84     res = list(compress(inp))
85     if res != exp:
86         print('Got %r, expected %r' % (res, exp))
87         assert res == exp
88 _assert_compress([0,2,4,6], ['s[0]', 's[2]', 's[4]', 's[6]'])
89 _assert_compress([0,1,2,4,6,7], ['s[:3]', 's[4]', 's[6:8]'])
90 _assert_compress([8,0,1,2,4,7,6,9], ['s[8]', 's[:3]', 's[4]', 's[7:5:-1]', 's[9]'])
91
92 def gen(wrong, right, indent):
93     code = ' + '.join(find_matching(wrong, right))
94     return 'if len(s) == %d:\n%s    return %s\n' % (len(wrong), indent, code)
95
96 def genall(tests):
97     indent = ' ' * 8
98     return indent + (indent + 'el').join(gen(wrong, right, indent) for wrong,right in tests)
99
100 def main():
101     print(genall(tests))
102     print(u'    Age gate:')
103     print(genall(tests_age_gate))
104
105 if __name__ == '__main__':
106     main()