conv.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/usr/bin/env python3
  2. # Read the wiki for more information
  3. # https://github.com/lennylxx/ipv6-hosts/wiki/sn-domains
  4. import sys
  5. table = '1023456789abcdefghijklmnopqrstuvwxyz'
  6. def iata2sn(iata):
  7. global table
  8. sn = ''
  9. for v in iata:
  10. if v in table:
  11. i = ((ord(v) - ord('a')) * 7 + 5) % 36
  12. sn += table[i]
  13. else:
  14. sn += v
  15. return sn
  16. def sn2iata(sn):
  17. global table
  18. iata = ''
  19. for v in sn:
  20. if v in table:
  21. i = table.index(v)
  22. i = (5 - i % 7) * 5 + i // 7 + 10
  23. iata += table[i]
  24. else:
  25. iata += v
  26. return iata
  27. def num2code(num):
  28. global table
  29. code = ''
  30. for v in num:
  31. if v in table:
  32. i = ((ord(v) - ord('0') + 1) * 7) % 36
  33. code += table[i]
  34. else:
  35. code += v
  36. return code
  37. def code2num(code):
  38. global table
  39. num = ''
  40. for v in code:
  41. if v in table:
  42. i = table.index(v)
  43. i = i // 7 + i % 7 - 1
  44. num += str(i)
  45. else:
  46. num += v
  47. return num
  48. def main():
  49. if len(sys.argv) != 3:
  50. print('usage:\tconv -i iata\n\tconv -s sn\n\tconv -p isp\n\tconv -g ggc')
  51. sys.exit(1)
  52. input = sys.argv[2]
  53. ret = ''
  54. if sys.argv[1] == '-i':
  55. ret += iata2sn(input[0:3])
  56. ret += num2code(input[3:5])
  57. if (len(input) > 3):
  58. ret += 'n'
  59. ret += num2code(input[6:8])
  60. elif sys.argv[1] == '-s':
  61. ret += sn2iata(input[0:3])
  62. ret += code2num(input[3:5])
  63. if (len(input) > 3):
  64. ret += 's'
  65. ret += code2num(input[6:8])
  66. elif sys.argv[1] == '-p':
  67. lst = input.split('-')
  68. ret += iata2sn(lst[0])
  69. if (len(lst) == 2):
  70. ret += '-'
  71. ret += iata2sn(lst[1][0:3])
  72. ret += num2code(lst[1][3:])
  73. elif sys.argv[1] == '-g':
  74. lst = input.split('-')
  75. ret += sn2iata(lst[0])
  76. if (len(lst) == 2):
  77. ret += '-'
  78. ret += sn2iata(lst[1][0:3])
  79. ret += code2num(lst[1][3:])
  80. else:
  81. print('Unknown option.')
  82. sys.exit(1)
  83. print(ret)
  84. sys.exit(0)
  85. if __name__ == '__main__':
  86. main()