2013-06-27 17:15:29 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
# Generate youtube signature algorithm from test cases
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
tests = [
|
2013-07-18 15:51:39 +08:00
|
|
|
# 92 - vflQw-fB4 2013/07/17
|
2013-07-17 23:43:44 +08:00
|
|
|
("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[]}|:;?/>.<'`~\"",
|
|
|
|
"mrtyuioplkjhgfdsazxcvbnq1234567890QWERTY}IOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[]\"|:;"),
|
2013-07-18 18:24:16 +08:00
|
|
|
# 90
|
|
|
|
("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[]}|:;?/>.<'`",
|
|
|
|
"mrtyuioplkjhgfdsazxcvbne1234567890QWER[YUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={`]}|"),
|
2013-08-12 16:48:40 +08:00
|
|
|
# 89
|
|
|
|
("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[]}|:;?/>.<'",
|
|
|
|
"/?;:|}<[{=+-_)(*&^%$#@!MqBVCXZASDFGHJKLPOIUYTREWQ0987654321mnbvcxzasdfghjklpoiuyt"),
|
2013-08-28 16:26:44 +08:00
|
|
|
# 88 - vflapUV9V 2013/08/28
|
2013-06-27 17:15:29 +08:00
|
|
|
("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[]}|:;?/>.<",
|
2013-08-28 16:26:44 +08:00
|
|
|
"ioplkjhgfdsazxcvbnm12<4567890QWERTYUIOZLKJHGFDSAeXCVBNM!@#$%^&*()_-+={[]}|:;?/>.3"),
|
2013-08-12 16:48:40 +08:00
|
|
|
# 87
|
2013-06-27 17:15:29 +08:00
|
|
|
("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$^&*()_-+={[]}|:;?/>.<",
|
2013-08-18 17:10:39 +08:00
|
|
|
"uioplkjhgfdsazxcvbnm1t34567890QWE2TYUIOPLKJHGFDSAZXCVeNM!@#$^&*()_-+={[]}|:;?/>.<"),
|
2013-09-06 15:49:50 +08:00
|
|
|
# 86 - vfluy6kdb 2013/09/06
|
2013-06-27 17:15:29 +08:00
|
|
|
("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[|};?/>.<",
|
2013-09-06 15:49:50 +08:00
|
|
|
"yuioplkjhgfdsazxcvbnm12345678q0QWrRTYUIOELKJHGFD-AZXCVBNM!@#$%^&*()_<+={[|};?/>.S"),
|
2013-09-11 17:24:58 +08:00
|
|
|
# 85 - vflkuzxcs 2013/09/11
|
2013-09-17 22:59:12 +08:00
|
|
|
('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[',
|
|
|
|
'3456789a0cdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRS[UVWXYZ!"#$%&\'()*+,-./:;<=>?@'),
|
2013-08-30 04:44:29 +08:00
|
|
|
# 84 - vflg0g8PQ 2013/08/29 (sporadic)
|
2013-06-27 17:15:29 +08:00
|
|
|
("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[};?>.<",
|
2013-08-30 04:44:29 +08:00
|
|
|
">?;}[{=+-_)(*&^%$#@!MNBVCXZASDFGHJKLPOIUYTREWq0987654321mnbvcxzasdfghjklpoiuytr"),
|
2013-08-18 17:10:39 +08:00
|
|
|
# 83
|
2013-06-27 17:15:29 +08:00
|
|
|
("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!#$%^&*()_+={[};?/>.<",
|
2013-08-18 17:10:39 +08:00
|
|
|
".>/?;}[{=+_)(*&^%<#!MNBVCXZASPFGHJKLwOIUYTREWQ0987654321mnbvcxzasdfghjklpoiuytreq"),
|
2013-09-13 04:04:09 +08:00
|
|
|
# 82 - vflGNjMhJ 2013/09/12
|
2013-06-27 17:15:29 +08:00
|
|
|
("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKHGFDSAZXCVBNM!@#$%^&*(-+={[};?/>.<",
|
2013-09-13 04:04:09 +08:00
|
|
|
".>/?;}[<=+-(*&^%$#@!MNBVCXeASDFGHKLPOqUYTREWQ0987654321mnbvcxzasdfghjklpoiuytrIwZ"),
|
2013-07-26 04:06:53 +08:00
|
|
|
# 81 - vflLC8JvQ 2013/07/25
|
2013-07-12 05:47:54 +08:00
|
|
|
("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKHGFDSAZXCVBNM!@#$%^&*(-+={[};?/>.",
|
2013-07-26 04:06:53 +08:00
|
|
|
"C>/?;}[{=+-(*&^%$#@!MNBVYXZASDFGHKLPOIU.TREWQ0q87659321mnbvcxzasdfghjkl4oiuytrewp"),
|
2013-08-23 17:33:56 +08:00
|
|
|
# 80 - vflZK4ZYR 2013/08/23 (sporadic)
|
|
|
|
("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKHGFDSAZXCVBNM!@#$%^&*(-+={[};?/>",
|
|
|
|
"wertyuioplkjhgfdsaqxcvbnm1234567890QWERTYUIOPLKHGFDSAZXCVBNM!@#$%^&z(-+={[};?/>"),
|
2013-07-26 04:50:24 +08:00
|
|
|
# 79 - vflLC8JvQ 2013/07/25 (sporadic)
|
|
|
|
("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKHGFDSAZXCVBNM!@#$%^&*(-+={[};?/",
|
|
|
|
"Z?;}[{=+-(*&^%$#@!MNBVCXRASDFGHKLPOIUYT/EWQ0q87659321mnbvcxzasdfghjkl4oiuytrewp"),
|
2013-06-27 17:15:29 +08:00
|
|
|
]
|
|
|
|
|
2013-07-31 16:45:13 +08:00
|
|
|
tests_age_gate = [
|
|
|
|
# 86 - vflqinMWD
|
|
|
|
("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[|};?/>.<",
|
|
|
|
"ertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!/#$%^&*()_-+={[|};?@"),
|
|
|
|
]
|
|
|
|
|
2013-06-27 17:15:29 +08:00
|
|
|
def find_matching(wrong, right):
|
|
|
|
idxs = [wrong.index(c) for c in right]
|
|
|
|
return compress(idxs)
|
|
|
|
return ('s[%d]' % i for i in idxs)
|
|
|
|
|
|
|
|
def compress(idxs):
|
|
|
|
def _genslice(start, end, step):
|
|
|
|
starts = '' if start == 0 else str(start)
|
|
|
|
ends = ':%d' % (end+step)
|
|
|
|
steps = '' if step == 1 else (':%d' % step)
|
|
|
|
return 's[%s%s%s]' % (starts, ends, steps)
|
|
|
|
|
|
|
|
step = None
|
|
|
|
for i, prev in zip(idxs[1:], idxs[:-1]):
|
|
|
|
if step is not None:
|
|
|
|
if i - prev == step:
|
|
|
|
continue
|
|
|
|
yield _genslice(start, prev, step)
|
|
|
|
step = None
|
|
|
|
continue
|
|
|
|
if i - prev in [-1, 1]:
|
|
|
|
step = i - prev
|
|
|
|
start = prev
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
yield 's[%d]' % prev
|
|
|
|
if step is None:
|
|
|
|
yield 's[%d]' % i
|
|
|
|
else:
|
|
|
|
yield _genslice(start, i, step)
|
|
|
|
|
|
|
|
def _assert_compress(inp, exp):
|
|
|
|
res = list(compress(inp))
|
|
|
|
if res != exp:
|
|
|
|
print('Got %r, expected %r' % (res, exp))
|
|
|
|
assert res == exp
|
|
|
|
_assert_compress([0,2,4,6], ['s[0]', 's[2]', 's[4]', 's[6]'])
|
|
|
|
_assert_compress([0,1,2,4,6,7], ['s[:3]', 's[4]', 's[6:8]'])
|
|
|
|
_assert_compress([8,0,1,2,4,7,6,9], ['s[8]', 's[:3]', 's[4]', 's[7:5:-1]', 's[9]'])
|
|
|
|
|
|
|
|
def gen(wrong, right, indent):
|
|
|
|
code = ' + '.join(find_matching(wrong, right))
|
|
|
|
return 'if len(s) == %d:\n%s return %s\n' % (len(wrong), indent, code)
|
|
|
|
|
|
|
|
def genall(tests):
|
|
|
|
indent = ' ' * 8
|
|
|
|
return indent + (indent + 'el').join(gen(wrong, right, indent) for wrong,right in tests)
|
|
|
|
|
|
|
|
def main():
|
|
|
|
print(genall(tests))
|
2013-07-31 16:45:13 +08:00
|
|
|
print(u' Age gate:')
|
|
|
|
print(genall(tests_age_gate))
|
2013-06-27 17:15:29 +08:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|