Skip to content

Instantly share code, notes, and snippets.

@llimllib
Last active March 31, 2023 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save llimllib/7f834e108b93f188482670f9a3af7149 to your computer and use it in GitHub Desktop.
Save llimllib/7f834e108b93f188482670f9a3af7149 to your computer and use it in GitHub Desktop.
# download_all_emoji.py: A script to query the slack API for all emojis on a
# given instance, and then download them into an "emojis" folder
#
# USAGE
#
# first, set a `SLACK_TOKEN` environment variable to a token that has
# `emoji:read` permission. Then, run this script with
# `python download_all_emoji.py`.
#
# it does not run in parallel, so it may take a while. Are you really
# that busy that you need to get it done in a hurry?
import json
import os
import requests
import subprocess
if not os.path.isfile("res.json"):
url = "https://slack.com/api/emoji.list"
res = requests.get(
url, headers={"Authorization": f'Bearer {os.environ["SLACK_TOKEN"]}'}
)
assert res.status_code == 200
emoji = res.json()["emoji"]
with open("res.json", "w") as out:
json.dump(emoji, out, indent=4)
else:
emoji = json.load(open("res.json"))
if not os.path.isdir("emoji"):
os.mkdir("emoji")
for name, val in emoji.items():
if val.startswith("alias:"):
try:
url = emoji[val[6:]]
except KeyError:
# seems like some of these are aliases to standard emoji, and are
# not associated with a custom URL. proceed without downloading.
continue
else:
url = val
extension = os.path.splitext(url)[1]
assert extension
# no-clobber means don't download if we already have it
cmd = f'wget --no-clobber -O "emoji/{name}{extension}" "{url}"'
print(cmd)
subprocess.run(cmd, shell=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment