ghost - annna - Annna the nice friendly bot.
(HTM) git clone git://bitreich.org/annna/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/annna/
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) Tags
(DIR) README
---
ghost (1385B)
---
1 #!/usr/bin/env python
2 # coding=utf-8
3 #
4 # Copy me if you can.
5 # by 20h
6 #
7 # Idea from: https://github.com/xdpirate/ghost-translator/blob/main/ghost-translator.py
8
9 import os
10 import sys
11 import getopt
12
13 def human2ghost(s):
14 r = ""
15 for c in s:
16 binc = bin(ord(c)^96)[2:]
17 for j in binc:
18 if j == "1":
19 r += "O"
20 else:
21 r += "o"
22 r += " "
23 return r
24
25 def ghost2human(s):
26 r = ""
27 if len(s) == 0:
28 return r
29
30 for c in s.split(" "):
31 if len(c) == 0:
32 continue
33 b = ""
34 for j in c:
35 if j == "O":
36 b += "1"
37 elif j == "o":
38 b += "0"
39 r += str(chr(int(b, 2)^96))
40 return r
41
42 def usage(app):
43 app = os.path.basename(app)
44 print("usage: %s [-h] [-e|-d]" % (app), file=sys.stderr)
45 sys.exit(1)
46
47 def main(args):
48 try:
49 opts, largs = getopt.getopt(args[1:], "hed")
50 except getopt.GetoptError as err:
51 print(str(err))
52 usage(args[0])
53
54 dodecode=0
55 doencode=0
56
57 for o, a in opts:
58 if o == "-h":
59 usage(args[0])
60 elif o == "-d":
61 dodecode=1
62 elif o == "-e":
63 doencode=1
64 else:
65 assert False, "unhandled option"
66
67 ins = sys.stdin.read()
68 rs = None
69 if doencode:
70 words = ins.split(" ")
71 rs = " ".join([human2ghost(w) for w in words])
72 if dodecode:
73 words = ins.split(" ")
74 rs = " ".join([ghost2human(w) for w in words])
75 if rs != None:
76 print("%s" % (rs), end='')
77 else:
78 usage(args[0])
79
80 return 0
81
82 if __name__ == "__main__":
83 sys.exit(main(sys.argv))
84