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.

83 lines
3.0 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
  21. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!#$%^&*()_+={[};?/>.<",
  22. "D.>/?;}[{=+_)(*&^%$#!MNBVCXeAS<FGHJKLPOIUYTREWZ0987654321mnbvcxzasdfghjklpoiuytrQ"),
  23. # 82
  24. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKHGFDSAZXCVBNM!@#$%^&*(-+={[};?/>.<",
  25. "Q>/?;}[{=+-(*<^%$#@!MNBVCXZASDFGHKLPOIUY8REWT0q&7654321mnbvcxzasdfghjklpoiuytrew9"),
  26. ]
  27. def find_matching(wrong, right):
  28. idxs = [wrong.index(c) for c in right]
  29. return compress(idxs)
  30. return ('s[%d]' % i for i in idxs)
  31. def compress(idxs):
  32. def _genslice(start, end, step):
  33. starts = '' if start == 0 else str(start)
  34. ends = ':%d' % (end+step)
  35. steps = '' if step == 1 else (':%d' % step)
  36. return 's[%s%s%s]' % (starts, ends, steps)
  37. step = None
  38. for i, prev in zip(idxs[1:], idxs[:-1]):
  39. if step is not None:
  40. if i - prev == step:
  41. continue
  42. yield _genslice(start, prev, step)
  43. step = None
  44. continue
  45. if i - prev in [-1, 1]:
  46. step = i - prev
  47. start = prev
  48. continue
  49. else:
  50. yield 's[%d]' % prev
  51. if step is None:
  52. yield 's[%d]' % i
  53. else:
  54. yield _genslice(start, i, step)
  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. def gen(wrong, right, indent):
  64. code = ' + '.join(find_matching(wrong, right))
  65. return 'if len(s) == %d:\n%s return %s\n' % (len(wrong), indent, code)
  66. def genall(tests):
  67. indent = ' ' * 8
  68. return indent + (indent + 'el').join(gen(wrong, right, indent) for wrong,right in tests)
  69. def main():
  70. print(genall(tests))
  71. if __name__ == '__main__':
  72. main()