]> Raphaƫl G. Git Repositories - youtubedl/blob - devscripts/youtube_genalgo.py
Add bug ID of one of the closed bugs.
[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 ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[]}|:;?/>.<",
9 "J:|}][{=+-_)(*&;%$#@>MNBVCXZASDFGH^KLPOIUYTREWQ0987654321mnbvcxzasdfghrklpoiuytej"),
10 ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$^&*()_-+={[]}|:;?/>.<",
11 "!?;:|}][{=+-_)(*&^$#@/MNBVCXZASqFGHJKLPOIUYTREWQ0987654321mnbvcxzasdfghjklpoiuytr"),
12 ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[|};?/>.<",
13 "ertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!/#$%^&*()_-+={[|};?@"),
14 ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[};?/>.<",
15 "{>/?;}[.=+-_)(*&^%$#@!MqBVCXZASDFwHJKLPOIUYTREWQ0987654321mnbvcxzasdfghjklpoiuytr"),
16 ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[};?>.<",
17 "<.>?;}[{=+-_)(*&^%$#@!MNBVCXZASDFGHJKLPOIUYTREWe098765432rmnbvcxzasdfghjklpoiuyt1"),
18 ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!#$%^&*()_+={[};?/>.<",
19 "D.>/?;}[{=+_)(*&^%$#!MNBVCXeAS<FGHJKLPOIUYTREWZ0987654321mnbvcxzasdfghjklpoiuytrQ"),
20 ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKHGFDSAZXCVBNM!@#$%^&*(-+={[};?/>.<",
21 "Q>/?;}[{=+-(*<^%$#@!MNBVCXZASDFGHKLPOIUY8REWT0q&7654321mnbvcxzasdfghjklpoiuytrew9"),
22 ]
23
24 def find_matching(wrong, right):
25 idxs = [wrong.index(c) for c in right]
26 return compress(idxs)
27 return ('s[%d]' % i for i in idxs)
28
29 def compress(idxs):
30 def _genslice(start, end, step):
31 starts = '' if start == 0 else str(start)
32 ends = ':%d' % (end+step)
33 steps = '' if step == 1 else (':%d' % step)
34 return 's[%s%s%s]' % (starts, ends, steps)
35
36 step = None
37 for i, prev in zip(idxs[1:], idxs[:-1]):
38 if step is not None:
39 if i - prev == step:
40 continue
41 yield _genslice(start, prev, step)
42 step = None
43 continue
44 if i - prev in [-1, 1]:
45 step = i - prev
46 start = prev
47 continue
48 else:
49 yield 's[%d]' % prev
50 if step is None:
51 yield 's[%d]' % i
52 else:
53 yield _genslice(start, i, step)
54
55 def _assert_compress(inp, exp):
56 res = list(compress(inp))
57 if res != exp:
58 print('Got %r, expected %r' % (res, exp))
59 assert res == exp
60 _assert_compress([0,2,4,6], ['s[0]', 's[2]', 's[4]', 's[6]'])
61 _assert_compress([0,1,2,4,6,7], ['s[:3]', 's[4]', 's[6:8]'])
62 _assert_compress([8,0,1,2,4,7,6,9], ['s[8]', 's[:3]', 's[4]', 's[7:5:-1]', 's[9]'])
63
64 def gen(wrong, right, indent):
65 code = ' + '.join(find_matching(wrong, right))
66 return 'if len(s) == %d:\n%s return %s\n' % (len(wrong), indent, code)
67
68 def genall(tests):
69 indent = ' ' * 8
70 return indent + (indent + 'el').join(gen(wrong, right, indent) for wrong,right in tests)
71
72 def main():
73 print(genall(tests))
74
75 if __name__ == '__main__':
76 main()