Last active
April 2, 2023 12:23
-
-
Save SpiritCroc/7993854e7711ed393977f41afff2de89 to your computer and use it in GitHub Desktop.
Convert stickers from a maunium stickerpicker to a MSC2545-enabled sticker pack to paste into /devtools, while reusing existing mxc-urls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import argparse | |
import emoji | |
import urllib.request | |
import json | |
import sys | |
parser = argparse.ArgumentParser(description="Convert maunium stickerpicker to MSC2545 sticker packs") | |
parser.add_argument("--baseurl", help="Maunium sticker picker URL") | |
parser.add_argument("--name", help="Output name of the pack") | |
parser.add_argument("--prefix", help="Add prefix to emote shortcodes") | |
parser.add_argument("--filter", action="append", help="Only include keys that include a keyword") | |
parser.add_argument("--exclude", action="append", help="Exclude some keys that include a keyword. Higher precedence than --filter.") | |
parser.add_argument("pack_id", help="Maunium's internal ID of the pack (name of the json in web/packs)") | |
args = parser.parse_args() | |
if args.baseurl: | |
maunium_base_url = args.baseurl | |
else: | |
maunium_base_url = "https://stickerpicker.example.com" | |
maunium_url = f"{maunium_base_url}/packs/{args.pack_id}.json" | |
print(f"Using pack url {maunium_url}", file=sys.stderr) | |
maunium_json = urllib.request.urlopen(maunium_url).read().decode() | |
maunium = json.loads(maunium_json) | |
pack_title = maunium['title'] | |
pack_id = maunium['id'] | |
out_title = args.name | |
if not out_title: | |
out_title = pack_title | |
stickers = dict() | |
print(f"Loading {pack_title} ({pack_id})", file=sys.stderr) | |
for mausticker in maunium['stickers']: | |
body = mausticker['body'] | |
name = emoji.demojize(body).replace("::", "_").replace(":", "") | |
# Some filtering for huge packs | |
if args.filter and not any(map(lambda x: x in name, args.filter)): | |
continue | |
if args.exclude and any(map(lambda x: x in name, args.exclude)): | |
continue | |
if args.prefix: | |
name = f"{args.prefix}_{name}" | |
info = mausticker["info"] | |
sticker = { | |
"url": mausticker["url"], | |
"info": info, | |
"body": body, | |
} | |
# Remove useless thumbnail info that matches the original | |
sticker["info"].pop("thumbnail_url") | |
sticker["info"].pop("thumbnail_info") | |
stickers[name] = sticker | |
pack = dict() | |
pack["images"] = stickers | |
pack["pack"] = { | |
"display_name": out_title | |
} | |
print(json.dumps(pack, indent=4)) | |
print(f"\n\nFound {len(stickers)} stickers! To activate, send a `im.ponies.room_emotes` room state event in /devtools, for example with state key `{args.pack_id}`!", file=sys.stderr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment