forked from smashwilson/slack-emojinator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.py
executable file
·110 lines (92 loc) · 3.24 KB
/
upload.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env python
# 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 requests
from bs4 import BeautifulSoup
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
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 https://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():
args = _argparse()
session = _session(args)
existing_emojis = get_current_emoji_list(session)
uploaded = 0
skipped = 0
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(session, emoji_name, filename)
print("{} upload complete.".format(filename))
uploaded += 1
print('\nUploaded {} emojis. ({} already existed)'.format(uploaded, skipped))
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(session, emoji_name, filename):
# Fetch the form first, to generate a crumb.
r = session.get(session.url)
r.raise_for_status()
soup = BeautifulSoup(r.text, "html.parser")
crumb = soup.find("input", attrs={"name": "crumb"})["value"]
data = {
'add': 1,
'crumb': crumb,
'name': emoji_name,
'mode': 'data',
}
files = {'img': open(filename, 'rb')}
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:
soup = BeautifulSoup(r.text, "html.parser")
crumb = soup.find("p", attrs={"class": "alert_error"})
print("Error with uploading %s: %s" % (emoji_name, crumb.text))
if __name__ == '__main__':
main()