get_discord_emoji.py: also get banner and icon

This commit is contained in:
Artemis Tosini 2024-10-31 18:59:30 +00:00
parent 5d7349062d
commit 73d0a52df4
Signed by: artemist
GPG key ID: EE5227935FE3FF18

View file

@ -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)