From f36c6669700273465f3d753b29d52f5dbf472267 Mon Sep 17 00:00:00 2001 From: Kyle Varga Date: Sun, 31 Jul 2016 12:26:08 -0500 Subject: [PATCH] Refactoring, check if emoji exists before uploading, and improved error handling --- upload.py | 47 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/upload.py b/upload.py index fe46ba6..be93136 100644 --- a/upload.py +++ b/upload.py @@ -1,6 +1,10 @@ +#!/usr/bin/env python + # Upload files named on ARGV as Slack emoji. +# https://github.com/smashwilson/slack-emojinator import os +import re import sys import requests @@ -10,20 +14,40 @@ team_name = os.getenv('SLACK_TEAM') cookie = os.getenv('SLACK_COOKIE') url = "https://{}.slack.com/customize/emoji".format(team_name) +headers = { + 'Cookie': cookie, +} -for filename in sys.argv[1:]: - print("Processing {}.".format(filename)) - emoji_name = os.path.splitext(os.path.basename(filename))[0] +def main(): + existing_emojis = get_current_emoji_list() + uploaded = 0 + skipped = 0 + for filename in sys.argv[1:]: + print("Processing {}.".format(filename)) + emoji_name = os.path.splitext(os.path.basename(filename))[0] + if emoji_name in existing_emojis: + print("Skipping {}. Emoji already exists").format(emoji_name) + skipped += 1 + else: + upload_emoji(emoji_name, filename) + print("{} upload complete.".format(filename)) + uploaded += 1 + print('\nUploaded {} emojis. ({} already existed)'.format(uploaded, skipped)) - headers = { - 'Cookie': cookie, - } +def get_current_emoji_list(): + r = requests.get(url, headers=headers) + r.raise_for_status() + x = re.findall("data-emoji-name=\"(.*)\"", r.text) + return x + + +def upload_emoji(emoji_name, filename): # Fetch the form first, to generate a crumb. r = requests.get(url, headers=headers) r.raise_for_status() - soup = BeautifulSoup(r.text) + soup = BeautifulSoup(r.text, "html.parser") crumb = soup.find("input", attrs={"name": "crumb"})["value"] data = { @@ -35,4 +59,11 @@ for filename in sys.argv[1:]: 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)) + # Slack returns 200 OK even if upload fails, so check for status of 'alert_error' info box + if 'alert_error' in r.content: + soup = BeautifulSoup(r.text, "html.parser") + crumb = soup.find("p", attrs={"class": "alert_error"}) + raise Exception("Error with uploading %s: %s" % (emoji_name, crumb.text)) + +if __name__ == '__main__': + main()