refactor to use argparse and fewer globals
This commit is contained in:
parent
150980c9b5
commit
8380a341f7
70
upload.py
70
upload.py
|
@ -3,9 +3,11 @@
|
|||
# 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
|
||||
|
@ -15,46 +17,76 @@ try:
|
|||
except NameError:
|
||||
raw_input = input
|
||||
|
||||
team_name = os.getenv('SLACK_TEAM')
|
||||
if not team_name:
|
||||
team_name = raw_input("Please enter the team name: ")
|
||||
cookie = os.getenv('SLACK_COOKIE')
|
||||
if not cookie:
|
||||
cookie = raw_input("Please enter the \"emoji\" cookie: ")
|
||||
URL = "https://{team_name}.slack.com/customize/emoji"
|
||||
|
||||
url = "https://{}.slack.com/customize/emoji".format(team_name)
|
||||
headers = {
|
||||
'Cookie': cookie,
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
|
||||
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"]
|
||||
|
@ -66,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:
|
||||
|
|
Loading…
Reference in New Issue