-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cape_manager.py
executable file
·142 lines (118 loc) · 4.76 KB
/
cape_manager.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/bin/env python3
"""
Cape Manager
Easy utilities to manager SnR dev capes
"""
import argparse
import sys
import urllib.request
import json
from uuid import UUID
from typing import Any
def uname_to_uuid(uname: str) -> str:
with urllib.request.urlopen("https://api.ashcon.app/mojang/v2/user/"+uname) as f:
dat = json.load(f)
return normalize_uuid(dat["uuid"])
def uuid_to_uname(uuid: str) -> str:
with urllib.request.urlopen("https://api.ashcon.app/mojang/v2/user/"+uuid) as f:
dat = json.load(f)
return dat["username"]
def normalize_uuid(uuid: str) -> str:
return str(UUID(uuid))
if __name__ == "__main__":
parser = argparse.ArgumentParser(
prog="Cape Manager",
description="Manage SnR dev capes")
subparsers = parser.add_subparsers(dest="command", required=True, help="Available actions")
# Add subcommand
add_parser = subparsers.add_parser("add", help="Add a cape to a user")
add_parser.add_argument("identifier", metavar="username", help="The username of the user")
add_parser.add_argument("-u", "--uuid", action="store_true",
help="Treat the identifier as a UUID")
# Remove subcommand
remove_parser = subparsers.add_parser("remove", help="Remove a cape from a user")
remove_parser.add_argument("identifier", metavar="username", help="The username of the user")
remove_parser.add_argument("-u", "--uuid", action="store_true",
help="Treat the identifier as a UUID")
# Update subcommand
update_parser = subparsers.add_parser("update", help="Update a user's username")
update_parser.add_argument("identifier", nargs="?", metavar="username",
help="The username of the user")
update_parser.add_argument("-u", "--uuid", action="store_true",
help="Treat the identifier as a UUID")
# List subcommand
list_parser = subparsers.add_parser("list", help="List users with capes")
args = parser.parse_args(sys.argv[1:])
with open("dev_capes.json") as f:
full_data: dict[str, Any] = json.load(f)
dev_capes: list[dict[str, str]] = full_data["dev"]
write = True
if args.command == "add":
if args.uuid:
uid = normalize_uuid(args.identifier)
name = uuid_to_uname(uid)
dev_capes.append({
"id": uid,
"name": name
})
print(f"Added cape to {name} ({uid})")
else:
uid = uname_to_uuid(args.identifier)
name = args.identifier
dev_capes.append({
"id": uid,
"name": name
})
print(f"Added cape to {name} ({uid})")
elif args.command == "remove":
if args.uuid:
args.identifier = normalize_uuid(args.identifier)
pre_len = len(dev_capes)
dev_capes = [
v for v in dev_capes
if normalize_uuid(v["id"]) != args.identifier
]
if pre_len == len(dev_capes):
print(f"{args.identifier} didn't have a cape")
else:
print(f"Removed cape from {args.identifier}")
else:
pre_len = len(dev_capes)
dev_capes = [
v for v in dev_capes
if v["name"] != args.identifier
]
if pre_len == len(dev_capes):
print(f"{args.identifier} didn't have a cape")
else:
print(f"Removed cape from {args.identifier}")
elif args.command == "update":
any = False
for entry in dev_capes:
if args.identifier:
if args.uuid and normalize_uuid(entry["id"]) != normalize_uuid(args.identifer):
continue
if not args.uuid and entry["name"] != args.identifier:
continue
any = True
print(f"Updating username for {entry['name']} ({entry['id']})")
old_name = entry["name"]
entry["name"] = uuid_to_uname(entry["name"])
if entry["name"] == old_name:
print("> Unchanged")
else:
print(f"> New username: {entry['name']}")
if not any:
print(f"Could not find user {args.identifier}")
elif args.command == "list":
write = False
print("Cape holders:")
for entry in dev_capes:
print(f"\t[{entry['id']}] {entry['name']}")
else:
raise ValueError(f"Invalid command {args.command}")
if write:
dev_capes = sorted(dev_capes, key=lambda v: v["id"])
full_data["dev"] = dev_capes
with open("dev_capes.json", "w") as f:
json.dump(full_data, f, indent=4)