youtube_genalgo.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/env python
  2. # Generate youtube signature algorithm from test cases
  3. import sys
  4. tests = [
  5. # 92 - vflQw-fB4 2013/07/17
  6. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[]}|:;?/>.<'`~\"",
  7. "mrtyuioplkjhgfdsazxcvbnq1234567890QWERTY}IOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[]\"|:;"),
  8. # 88
  9. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[]}|:;?/>.<",
  10. "J:|}][{=+-_)(*&;%$#@>MNBVCXZASDFGH^KLPOIUYTREWQ0987654321mnbvcxzasdfghrklpoiuytej"),
  11. # 87
  12. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$^&*()_-+={[]}|:;?/>.<",
  13. "!?;:|}][{=+-_)(*&^$#@/MNBVCXZASqFGHJKLPOIUYTREWQ0987654321mnbvcxzasdfghjklpoiuytr"),
  14. # 86 - vfl_ymO4Z 2013/06/27
  15. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[|};?/>.<",
  16. "ertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!/#$%^&*()_-+={[|};?@"),
  17. # 85
  18. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[};?/>.<",
  19. "{>/?;}[.=+-_)(*&^%$#@!MqBVCXZASDFwHJKLPOIUYTREWQ0987654321mnbvcxzasdfghjklpoiuytr"),
  20. # 84
  21. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[};?>.<",
  22. "<.>?;}[{=+-_)(*&^%$#@!MNBVCXZASDFGHJKLPOIUYTREWe098765432rmnbvcxzasdfghjklpoiuyt1"),
  23. # 83 - vflcaqGO8 2013/07/11
  24. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!#$%^&*()_+={[};?/>.<",
  25. "urty8ioplkjhgfdsazxcvbqm1234567S90QWERTYUIOPLKJHGFDnAZXCVBNM!#$%^&*()_+={[};?/>.<"),
  26. # 82
  27. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKHGFDSAZXCVBNM!@#$%^&*(-+={[};?/>.<",
  28. "Q>/?;}[{=+-(*<^%$#@!MNBVCXZASDFGHKLPOIUY8REWT0q&7654321mnbvcxzasdfghjklpoiuytrew9"),
  29. # 81
  30. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKHGFDSAZXCVBNM!@#$%^&*(-+={[};?/>.",
  31. "urty8ioplkjhgfdsazxcvbqm1234567e90QWERTYUIOPLKHGFDSnZXCVBNM!@#$%^&*(-+={[};?/>."),
  32. ]
  33. def find_matching(wrong, right):
  34. idxs = [wrong.index(c) for c in right]
  35. return compress(idxs)
  36. return ('s[%d]' % i for i in idxs)
  37. def compress(idxs):
  38. def _genslice(start, end, step):
  39. starts = '' if start == 0 else str(start)
  40. ends = ':%d' % (end+step)
  41. steps = '' if step == 1 else (':%d' % step)
  42. return 's[%s%s%s]' % (starts, ends, steps)
  43. step = None
  44. for i, prev in zip(idxs[1:], idxs[:-1]):
  45. if step is not None:
  46. if i - prev == step:
  47. continue
  48. yield _genslice(start, prev, step)
  49. step = None
  50. continue
  51. if i - prev in [-1, 1]:
  52. step = i - prev
  53. start = prev
  54. continue
  55. else:
  56. yield 's[%d]' % prev
  57. if step is None:
  58. yield 's[%d]' % i
  59. else:
  60. yield _genslice(start, i, step)
  61. def _assert_compress(inp, exp):
  62. res = list(compress(inp))
  63. if res != exp:
  64. print('Got %r, expected %r' % (res, exp))
  65. assert res == exp
  66. _assert_compress([0,2,4,6], ['s[0]', 's[2]', 's[4]', 's[6]'])
  67. _assert_compress([0,1,2,4,6,7], ['s[:3]', 's[4]', 's[6:8]'])
  68. _assert_compress([8,0,1,2,4,7,6,9], ['s[8]', 's[:3]', 's[4]', 's[7:5:-1]', 's[9]'])
  69. def gen(wrong, right, indent):
  70. code = ' + '.join(find_matching(wrong, right))
  71. return 'if len(s) == %d:\n%s return %s\n' % (len(wrong), indent, code)
  72. def genall(tests):
  73. indent = ' ' * 8
  74. return indent + (indent + 'el').join(gen(wrong, right, indent) for wrong,right in tests)
  75. def main():
  76. print(genall(tests))
  77. if __name__ == '__main__':
  78. main()