Skip to content

Instantly share code, notes, and snippets.

@raphaelcastaneda
Last active April 13, 2018 20:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save raphaelcastaneda/1be9fc2db6b902f6ce33 to your computer and use it in GitHub Desktop.
Save raphaelcastaneda/1be9fc2db6b902f6ce33 to your computer and use it in GitHub Desktop.
Batch upload pidgin emote packs to slack

Pidgin emote pack uploader for Slack

Downloaded pidgin icon packs can be uploaded (one at a time) to Slack by posting the form. Note that this does not (yet) handle name conflicts with existing emotes. Failures to upload will be printed to the console.

Usage

install the requirements

pip install -r requirements.txt

Extract your icon pack into the working directory, under a new folder called "emote_files" Set the following environment variables:

SLACK_TEAM

SLACK_COOKIE

Then execute:

python pidgin_emotes_to_slack.py

import os
import requests
from bs4 import BeautifulSoup
team_name = os.getenv('SLACK_TEAM')
cookie = os.getenv('SLACK_COOKIE')
url = "https://{}.slack.com/customize/emoji".format(team_name)
emote_dir = './emote_files/'
emotes_to_upload = {}
with open(os.path.join(emote_dir, 'theme'), 'r') as f:
header_read = False
for line in f:
if not header_read and '[' in line:
header_read = True
continue
elif header_read:
emote_line = line.replace('!', '').strip().split('\t')
if emote_line[1].startswith(':') and emote_line[1].endswith(':'):
emotes_to_upload[emote_line[1].replace(':', '')] = os.path.join(emote_dir, emote_line[0])
else:
emote_name = os.path.splitext(os.path.basename(emote_line[0].replace('emot-', '')))[0]
emotes_to_upload[emote_name] = os.path.join(emote_dir, emote_line[0])
for emoji_name, filename in emotes_to_upload.items():
print("Processing {}.".format(filename))
headers = {
'Cookie': cookie,
}
# Fetch the form first, to generate a crumb.
r = requests.get(url, headers=headers)
r.raise_for_status()
soup = BeautifulSoup(r.text)
crumb = soup.find("input", attrs={"name": "crumb"})["value"]
data = {
'add': 1,
'crumb': crumb,
'name': emoji_name,
'mode': 'data',
}
try:
files = {'img': open(filename, 'rb')}
r = requests.post(url, headers=headers, data=data, files=files, allow_redirects=False)
r.raise_for_status()
print("{} complete.".format(filename))
except Exception as e:
print("{} failed. Exception: {}".format(filename, e))
beautifulsoup4
requests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment