From 73d0a52df486aa247673cbd3ad0c9a8cbcfb2e0b Mon Sep 17 00:00:00 2001 From: Artemis Tosini Date: Thu, 31 Oct 2024 18:59:30 +0000 Subject: [PATCH] get_discord_emoji.py: also get banner and icon --- scripts/emoji/get_discord_emoji.py | 39 +++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/scripts/emoji/get_discord_emoji.py b/scripts/emoji/get_discord_emoji.py index 5afb654..d901fb4 100755 --- a/scripts/emoji/get_discord_emoji.py +++ b/scripts/emoji/get_discord_emoji.py @@ -16,10 +16,13 @@ def download_file(url, path): print("Already downloaded, skipping...") return out_file = open(path, "wb") - req = urllib.request.urlopen( - urllib.request.Request(url, headers={"User-Agent": "curl/8.6.0"}) - ) - shutil.copyfileobj(req, out_file) + try: + req = urllib.request.urlopen( + urllib.request.Request(url, headers={"User-Agent": "curl/8.6.0"}) + ) + shutil.copyfileobj(req, out_file) + except urllib.error.HTTPError: + print(f"Failed to download {url}") time.sleep(0.1) @@ -65,8 +68,32 @@ def main(*args): emoji_objs = guild["emojis"] dir_name = safe_name(name) - sticker_dir = base_dir / "stickers" / dir_name - emoji_dir = base_dir / "emoji" / dir_name + guild_dir = base_dir / dir_name + sticker_dir = guild_dir / "stickers" + emoji_dir = guild_dir / "emoji" + + guild_dir.mkdir(exist_ok=True, parents=True) + + properties = guild["properties"] + guild_id = properties["id"] + subset_properties = { + "id": properties["id"], + "name": properties["name"], + "description": properties["description"], + } + with open(guild_dir / "properties.json", "w") as properties_file: + json.dump(subset_properties, properties_file) + + if properties["icon"] is not None: + download_file( + f"https://cdn.discordapp.com/icons/{guild_id}/{properties['icon']}.png?size=4096", + guild_dir / "icon.png", + ) + if properties["banner"] is not None: + download_file( + f"https://cdn.discordapp.com/banners/{guild_id}/{properties['banner']}.png?size=4096", + guild_dir / "banner.png", + ) if len(sticker_objs) > 0: sticker_dir.mkdir(exist_ok=True, parents=True)