-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode.py
More file actions
54 lines (37 loc) · 1.18 KB
/
decode.py
File metadata and controls
54 lines (37 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import base64
import urllib.parse
import html
import json
import sys
def decode_base64(text: str) -> str:
return base64.b64decode(text).decode()
def decode_json(text: str) -> str:
decoded = json.loads(text)
# Support both "hello" -> hello and {"a":1} -> {"a": 1}
if isinstance(decoded, str):
return decoded
else:
return json.dumps(decoded)
def decode_url(text: str) -> str:
return urllib.parse.unquote(text)
def decode_html(text: str) -> str:
return html.unescape(text)
def decode(name: str, decode_func) -> dict:
try:
result: str = decode_func()
return {"title": result, "subtitle": name, "arg": result}
except:
return {"title": "-", "subtitle": f"{name} (invalid)", "valid": False}
def main(args: list[str]) -> dict:
if len(args) < 1:
return {"items": []}
text = ' '.join(args)
items = [
decode("Base64", lambda: decode_base64(text)),
decode("JSON", lambda: decode_json(text)),
decode("URL", lambda: decode_url(text)),
decode("HTML", lambda: decode_html(text)),
]
return {"items": items}
if __name__ == "__main__":
print(json.dumps(main(sys.argv[1:])))