From 2293284213e261013728a9df0e3fdba39e54eccb Mon Sep 17 00:00:00 2001 From: techno-sam <77073745+techno-sam@users.noreply.github.com> Date: Sun, 1 Dec 2024 11:10:02 -0800 Subject: [PATCH] Better cape manager code --- cape_manager.py | 123 ++++++++++++++++++++++++++++++++++++++++++++++++ gen_json.py | 35 -------------- 2 files changed, 123 insertions(+), 35 deletions(-) create mode 100755 cape_manager.py delete mode 100644 gen_json.py diff --git a/cape_manager.py b/cape_manager.py new file mode 100755 index 0000000..cdd5ed8 --- /dev/null +++ b/cape_manager.py @@ -0,0 +1,123 @@ +#!/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") + + + 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"] + + 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": + 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 + print(f"Updating username for {entry['name']} ({entry['id']})") + entry["name"] = uuid_to_uname(entry["name"]) + print(f"New username: {entry['name']}") + else: + raise ValueError(f"Invalid command {args.command}") + + 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) diff --git a/gen_json.py b/gen_json.py deleted file mode 100644 index 412ed26..0000000 --- a/gen_json.py +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/python3 -import urllib.request -import json - -names = [] - -with open("dev_capes.txt") as f: - for line in f: - names.append(line.strip()) - -names = list(set(names)) -names = [name for name in names if name != ""] - -uuids = {} - -for i, name in enumerate(names): - print(f"[{i+1}/{len(names)}] Fetching UUID for {name}") - with urllib.request.urlopen("https://api.ashcon.app/mojang/v2/user/"+name) as f: - dat = json.load(f) - uuids[name] = dat["uuid"] - - -full_data = { - "dev": [ - { - "id": v, - "name": k - } for k, v in uuids.items() - ] -} - -full_data["dev"] = sorted(full_data["dev"], key=lambda v: v["id"]) - -with open("dev_capes.json", "w") as f: - json.dump(full_data, f, indent=4)