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.

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