2015-02-22 16:29:55 +00:00
|
|
|
# Upload files named on ARGV as Slack emoji.
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import requests
|
|
|
|
|
2015-02-25 03:57:48 +00:00
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
|
2015-02-22 16:29:55 +00:00
|
|
|
team_name = os.getenv('SLACK_TEAM')
|
|
|
|
cookie = os.getenv('SLACK_COOKIE')
|
|
|
|
|
|
|
|
url = "https://{}.slack.com/customize/emoji".format(team_name)
|
|
|
|
|
|
|
|
for filename in sys.argv[1:]:
|
|
|
|
print("Processing {}.".format(filename))
|
|
|
|
|
2015-02-25 03:57:25 +00:00
|
|
|
emoji_name = os.path.splitext(os.path.basename(filename))[0]
|
2015-02-22 16:29:55 +00:00
|
|
|
|
|
|
|
headers = {
|
|
|
|
'Cookie': cookie,
|
|
|
|
}
|
2015-02-25 03:57:48 +00:00
|
|
|
|
|
|
|
# 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"]
|
|
|
|
|
2015-02-22 16:29:55 +00:00
|
|
|
data = {
|
|
|
|
'add': 1,
|
|
|
|
'crumb': crumb,
|
|
|
|
'name': emoji_name,
|
|
|
|
'mode': 'data',
|
|
|
|
}
|
|
|
|
files = {'img': open(filename, 'rb')}
|
|
|
|
r = requests.post(url, headers=headers, data=data, files=files, allow_redirects=False)
|
|
|
|
r.raise_for_status()
|
2015-02-25 03:57:48 +00:00
|
|
|
print("{} complete.".format(filename))
|