Skip to content

Instantly share code, notes, and snippets.

@will
Forked from raphaelcastaneda/README.md
Last active March 1, 2016 22:30
Show Gist options
  • Save will/27f1dcc098a7897544ff to your computer and use it in GitHub Desktop.
Save will/27f1dcc098a7897544ff 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.

#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 '[default]' 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:
print("{} failed.".format(filename))
beautifulsoup4
requests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment