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.

113 lines
4.9 KiB

  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. # Generate youtube signature algorithm from test cases
  4. import sys
  5. tests = [
  6. # 93 - vfl79wBKW 2013/07/20
  7. (u"qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[]}|:;?/>.<'`~\"",
  8. u".>/?;:|}][{=+-_)(*&^%$#@!MNBVCXZASDFGHJKLPOIUYTREWQ098765'321mnbvcxzasdfghjklpoiu"),
  9. # 92 - vflQw-fB4 2013/07/17
  10. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[]}|:;?/>.<'`~\"",
  11. "mrtyuioplkjhgfdsazxcvbnq1234567890QWERTY}IOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[]\"|:;"),
  12. # 90
  13. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[]}|:;?/>.<'`",
  14. "mrtyuioplkjhgfdsazxcvbne1234567890QWER[YUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={`]}|"),
  15. # 89
  16. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[]}|:;?/>.<'",
  17. "/?;:|}<[{=+-_)(*&^%$#@!MqBVCXZASDFGHJKLPOIUYTREWQ0987654321mnbvcxzasdfghjklpoiuyt"),
  18. # 88 - vflapUV9V 2013/08/28
  19. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[]}|:;?/>.<",
  20. "ioplkjhgfdsazxcvbnm12<4567890QWERTYUIOZLKJHGFDSAeXCVBNM!@#$%^&*()_-+={[]}|:;?/>.3"),
  21. # 87
  22. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$^&*()_-+={[]}|:;?/>.<",
  23. "uioplkjhgfdsazxcvbnm1t34567890QWE2TYUIOPLKJHGFDSAZXCVeNM!@#$^&*()_-+={[]}|:;?/>.<"),
  24. # 86 - vfluy6kdb 2013/09/06
  25. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[|};?/>.<",
  26. "yuioplkjhgfdsazxcvbnm12345678q0QWrRTYUIOELKJHGFD-AZXCVBNM!@#$%^&*()_<+={[|};?/>.S"),
  27. # 85 - vflkuzxcs 2013/09/11
  28. ('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[',
  29. '3456789a0cdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRS[UVWXYZ!"#$%&\'()*+,-./:;<=>?@'),
  30. # 84 - vflg0g8PQ 2013/08/29 (sporadic)
  31. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[};?>.<",
  32. ">?;}[{=+-_)(*&^%$#@!MNBVCXZASDFGHJKLPOIUYTREWq0987654321mnbvcxzasdfghjklpoiuytr"),
  33. # 83
  34. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!#$%^&*()_+={[};?/>.<",
  35. ".>/?;}[{=+_)(*&^%<#!MNBVCXZASPFGHJKLwOIUYTREWQ0987654321mnbvcxzasdfghjklpoiuytreq"),
  36. # 82 - vflGNjMhJ 2013/09/12
  37. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKHGFDSAZXCVBNM!@#$%^&*(-+={[};?/>.<",
  38. ".>/?;}[<=+-(*&^%$#@!MNBVCXeASDFGHKLPOqUYTREWQ0987654321mnbvcxzasdfghjklpoiuytrIwZ"),
  39. # 81 - vflLC8JvQ 2013/07/25
  40. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKHGFDSAZXCVBNM!@#$%^&*(-+={[};?/>.",
  41. "C>/?;}[{=+-(*&^%$#@!MNBVYXZASDFGHKLPOIU.TREWQ0q87659321mnbvcxzasdfghjkl4oiuytrewp"),
  42. # 80 - vflZK4ZYR 2013/08/23 (sporadic)
  43. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKHGFDSAZXCVBNM!@#$%^&*(-+={[};?/>",
  44. "wertyuioplkjhgfdsaqxcvbnm1234567890QWERTYUIOPLKHGFDSAZXCVBNM!@#$%^&z(-+={[};?/>"),
  45. # 79 - vflLC8JvQ 2013/07/25 (sporadic)
  46. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKHGFDSAZXCVBNM!@#$%^&*(-+={[};?/",
  47. "Z?;}[{=+-(*&^%$#@!MNBVCXRASDFGHKLPOIUYT/EWQ0q87659321mnbvcxzasdfghjkl4oiuytrewp"),
  48. ]
  49. tests_age_gate = [
  50. # 86 - vflqinMWD
  51. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[|};?/>.<",
  52. "ertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!/#$%^&*()_-+={[|};?@"),
  53. ]
  54. def find_matching(wrong, right):
  55. idxs = [wrong.index(c) for c in right]
  56. return compress(idxs)
  57. return ('s[%d]' % i for i in idxs)
  58. def compress(idxs):
  59. def _genslice(start, end, step):
  60. starts = '' if start == 0 else str(start)
  61. ends = ':%d' % (end+step)
  62. steps = '' if step == 1 else (':%d' % step)
  63. return 's[%s%s%s]' % (starts, ends, steps)
  64. step = None
  65. for i, prev in zip(idxs[1:], idxs[:-1]):
  66. if step is not None:
  67. if i - prev == step:
  68. continue
  69. yield _genslice(start, prev, step)
  70. step = None
  71. continue
  72. if i - prev in [-1, 1]:
  73. step = i - prev
  74. start = prev
  75. continue
  76. else:
  77. yield 's[%d]' % prev
  78. if step is None:
  79. yield 's[%d]' % i
  80. else:
  81. yield _genslice(start, i, step)
  82. def _assert_compress(inp, exp):
  83. res = list(compress(inp))
  84. if res != exp:
  85. print('Got %r, expected %r' % (res, exp))
  86. assert res == exp
  87. _assert_compress([0,2,4,6], ['s[0]', 's[2]', 's[4]', 's[6]'])
  88. _assert_compress([0,1,2,4,6,7], ['s[:3]', 's[4]', 's[6:8]'])
  89. _assert_compress([8,0,1,2,4,7,6,9], ['s[8]', 's[:3]', 's[4]', 's[7:5:-1]', 's[9]'])
  90. def gen(wrong, right, indent):
  91. code = ' + '.join(find_matching(wrong, right))
  92. return 'if len(s) == %d:\n%s return %s\n' % (len(wrong), indent, code)
  93. def genall(tests):
  94. indent = ' ' * 8
  95. return indent + (indent + 'el').join(gen(wrong, right, indent) for wrong,right in tests)
  96. def main():
  97. print(genall(tests))
  98. print(u' Age gate:')
  99. print(genall(tests_age_gate))
  100. if __name__ == '__main__':
  101. main()