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.

103 lines
4.2 KiB

  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. # 90
  9. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[]}|:;?/>.<'`",
  10. "mrtyuioplkjhgfdsazxcvbne1234567890QWER[YUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={`]}|"),
  11. # 88
  12. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[]}|:;?/>.<",
  13. "J:|}][{=+-_)(*&;%$#@>MNBVCXZASDFGH^KLPOIUYTREWQ0987654321mnbvcxzasdfghrklpoiuytej"),
  14. # 87 - vflART1Nf 2013/07/24
  15. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$^&*()_-+={[]}|:;?/>.<",
  16. "tyuioplkjhgfdsazxcv<nm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$^&*()_-+={[]}|:;?/>"),
  17. # 86 - vflm_D8eE 2013/07/31
  18. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[|};?/>.<",
  19. ">.1}|[{=+-_)(*&^%$#@!MNBVCXZASDFGHJK<POIUYTREW509876L432/mnbvcxzasdfghjklpoiuytre"),
  20. # 85 - vflSAFCP9 2013/07/19
  21. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[};?/>.<",
  22. "ertyuiqplkjhgfdsazx$vbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#<%^&*()_-+={[};?/c"),
  23. # 84
  24. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[};?>.<",
  25. "<.>?;}[{=+-_)(*&^%$#@!MNBVCXZASDFGHJKLPOIUYTREWe098765432rmnbvcxzasdfghjklpoiuyt1"),
  26. # 83 - vflcaqGO8 2013/07/11
  27. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!#$%^&*()_+={[};?/>.<",
  28. "urty8ioplkjhgfdsazxcvbqm1234567S90QWERTYUIOPLKJHGFDnAZXCVBNM!#$%^&*()_+={[};?/>.<"),
  29. # 82
  30. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKHGFDSAZXCVBNM!@#$%^&*(-+={[};?/>.<",
  31. "Q>/?;}[{=+-(*<^%$#@!MNBVCXZASDFGHKLPOIUY8REWT0q&7654321mnbvcxzasdfghjklpoiuytrew9"),
  32. # 81 - vflLC8JvQ 2013/07/25
  33. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKHGFDSAZXCVBNM!@#$%^&*(-+={[};?/>.",
  34. "C>/?;}[{=+-(*&^%$#@!MNBVYXZASDFGHKLPOIU.TREWQ0q87659321mnbvcxzasdfghjkl4oiuytrewp"),
  35. # 79 - vflLC8JvQ 2013/07/25 (sporadic)
  36. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKHGFDSAZXCVBNM!@#$%^&*(-+={[};?/",
  37. "Z?;}[{=+-(*&^%$#@!MNBVCXRASDFGHKLPOIUYT/EWQ0q87659321mnbvcxzasdfghjkl4oiuytrewp"),
  38. ]
  39. tests_age_gate = [
  40. # 86 - vflqinMWD
  41. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[|};?/>.<",
  42. "ertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!/#$%^&*()_-+={[|};?@"),
  43. ]
  44. def find_matching(wrong, right):
  45. idxs = [wrong.index(c) for c in right]
  46. return compress(idxs)
  47. return ('s[%d]' % i for i in idxs)
  48. def compress(idxs):
  49. def _genslice(start, end, step):
  50. starts = '' if start == 0 else str(start)
  51. ends = ':%d' % (end+step)
  52. steps = '' if step == 1 else (':%d' % step)
  53. return 's[%s%s%s]' % (starts, ends, steps)
  54. step = None
  55. for i, prev in zip(idxs[1:], idxs[:-1]):
  56. if step is not None:
  57. if i - prev == step:
  58. continue
  59. yield _genslice(start, prev, step)
  60. step = None
  61. continue
  62. if i - prev in [-1, 1]:
  63. step = i - prev
  64. start = prev
  65. continue
  66. else:
  67. yield 's[%d]' % prev
  68. if step is None:
  69. yield 's[%d]' % i
  70. else:
  71. yield _genslice(start, i, step)
  72. def _assert_compress(inp, exp):
  73. res = list(compress(inp))
  74. if res != exp:
  75. print('Got %r, expected %r' % (res, exp))
  76. assert res == exp
  77. _assert_compress([0,2,4,6], ['s[0]', 's[2]', 's[4]', 's[6]'])
  78. _assert_compress([0,1,2,4,6,7], ['s[:3]', 's[4]', 's[6:8]'])
  79. _assert_compress([8,0,1,2,4,7,6,9], ['s[8]', 's[:3]', 's[4]', 's[7:5:-1]', 's[9]'])
  80. def gen(wrong, right, indent):
  81. code = ' + '.join(find_matching(wrong, right))
  82. return 'if len(s) == %d:\n%s return %s\n' % (len(wrong), indent, code)
  83. def genall(tests):
  84. indent = ' ' * 8
  85. return indent + (indent + 'el').join(gen(wrong, right, indent) for wrong,right in tests)
  86. def main():
  87. print(genall(tests))
  88. print(u' Age gate:')
  89. print(genall(tests_age_gate))
  90. if __name__ == '__main__':
  91. main()