-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode.py
More file actions
45 lines (28 loc) · 880 Bytes
/
encode.py
File metadata and controls
45 lines (28 loc) · 880 Bytes
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
import base64
import urllib.parse
import html
import json
import sys
def encode_base64(text: str) -> str:
return base64.b64encode(text.encode()).decode()
def encode_json(text: str) -> str:
return json.dumps(text)
def encode_url(text: str) -> str:
return urllib.parse.quote(text, safe='')
def encode_html(text: str) -> str:
return html.escape(text)
def item(name: str, result: str) -> dict:
return {"title": result, "subtitle": name, "arg": result}
def main(args: list[str]) -> dict:
if len(args) < 1:
return {"items": []}
text = ' '.join(args)
items = [
item("Base64", encode_base64(text)),
item("JSON", encode_json(text)),
item("URL", encode_url(text)),
item("HTML", encode_html(text)),
]
return {"items": items}
if __name__ == "__main__":
print(json.dumps(main(sys.argv[1:])))