You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
3.2 KiB

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