Skip to content

Commit

Permalink
Merge pull request smashwilson#11 from samstav/samstav/args
Browse files Browse the repository at this point in the history
improve ux
  • Loading branch information
smashwilson authored Dec 9, 2016
2 parents f61f661 + 8380a34 commit daf1a3e
Showing 1 changed file with 56 additions and 19 deletions.
75 changes: 56 additions & 19 deletions upload.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,90 @@
# Upload files named on ARGV as Slack emoji.
# https://github.com/smashwilson/slack-emojinator

from __future__ import print_function

import argparse
import os
import re
import sys
import requests

from bs4 import BeautifulSoup

team_name = os.getenv('SLACK_TEAM')
if not team_name:
team_name = input("Please enter the team name: ")
cookie = os.getenv('SLACK_COOKIE')
if not cookie:
cookie = input("Please enter the \"emoji\" cookie: ")
try:
raw_input
except NameError:
raw_input = input

URL = "https://{team_name}.slack.com/customize/emoji"


def _session(args):
assert args.cookie, "Cookie required"
assert args.team_name, "Team name required"
session = requests.session()
session.headers = {'Cookie': args.cookie}
session.url = URL.format(team_name=args.team_name)
return session


url = "https://{}.slack.com/customize/emoji".format(team_name)
headers = {
'Cookie': cookie,
}
def _argparse():
parser = argparse.ArgumentParser(
description='Bulk upload emoji to slack'
)
parser.add_argument(
'--team-name', '-t',
default=os.getenv('SLACK_TEAM'),
help='Defaults to the $SLACK_TEAM environment variable.'
)
parser.add_argument(
'--cookie', '-c',
default=os.getenv('SLACK_COOKIE'),
help='Defaults to the $SLACK_COOKIE environment variable.'
)
parser.add_argument(
'slackmoji_files',
nargs='+',
help=('Paths to slackmoji, e.g. if you '
'unzipped http://cultofthepartyparrot.com/parrots.zip '
'in your home dir, then use ~/parrots/*'),
)
args = parser.parse_args()
if not args.team_name:
args.team_name = raw_input('Please enter the team name: ').strip()
if not args.cookie:
args.cookie = raw_input('Please enter the "emoji" cookie: ').strip()
return args


def main():
existing_emojis = get_current_emoji_list()
args = _argparse()
session = _session(args)
existing_emojis = get_current_emoji_list(session)
uploaded = 0
skipped = 0
for filename in sys.argv[1:]:
for filename in args.slackmoji_files:
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)
upload_emoji(session, emoji_name, filename)
print("{} upload complete.".format(filename))
uploaded += 1
print('\nUploaded {} emojis. ({} already existed)'.format(uploaded, skipped))


def get_current_emoji_list():
r = requests.get(url, headers=headers)
def get_current_emoji_list(session):
r = session.get(session.url)
r.raise_for_status()
x = re.findall("data-emoji-name=\"(.*)\"", r.text)
return x


def upload_emoji(emoji_name, filename):
def upload_emoji(session, emoji_name, filename):
# Fetch the form first, to generate a crumb.
r = requests.get(url, headers=headers)
r = session.get(session.url)
r.raise_for_status()
soup = BeautifulSoup(r.text, "html.parser")
crumb = soup.find("input", attrs={"name": "crumb"})["value"]
Expand All @@ -61,7 +98,7 @@ def upload_emoji(emoji_name, filename):
'mode': 'data',
}
files = {'img': open(filename, 'rb')}
r = requests.post(url, headers=headers, data=data, files=files, allow_redirects=False)
r = session.post(session.url, data=data, files=files, allow_redirects=False)
r.raise_for_status()
# Slack returns 200 OK even if upload fails, so check for status of 'alert_error' info box
if b'alert_error' in r.content:
Expand Down

0 comments on commit daf1a3e

Please sign in to comment.