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