-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
190 lines (167 loc) · 8.96 KB
/
manage.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import requests
import json
import shutil
import time
import os
import sys
import argparse
from PIL import Image
import generate
import tokenmanager
import slack
from requests_toolbelt.multipart.encoder import MultipartEncoder
IMAGE_EXTENSIONS = (".png", ".jpg", ".jpeg", ".gif", ".mng")
argument_parser = argparse.ArgumentParser(description="Slackmoji manager")
tokenmanager.add_command_args(argument_parser)
argument_parser.add_argument("--collect", action='store_true',
help="Collect emojis to a folder", required=False)
argument_parser.add_argument("--create", action='store',
help="Folder to upload emojis from using the file name as the name", required=False)
argument_parser.add_argument("--batch_size", action='store', default=8, type=int,
help="Number of files to upload before sleeping to avoid rate limit. Used with --create, (optional, default 8)", required=False)
argument_parser.add_argument("--dryrun", action='store_true', required=False, help="Dryrun", default=False)
argument_parser.add_argument("--recursive", "-r", action='store_true', required=False, help="search recursively from the given directory for images to create", default=False)
argument_parser.add_argument("--approve", action='store',
help="Search term to find the user image and create an approve emoji", required=False)
argument_parser.add_argument("--name", action='store',
help="Name to use as prefix for the generated emoji", required=False)
argument_parser.add_argument("--offset", action='store',
help="Ratio of image to shift by to make room for the icon", required=False, default=.1)
argument_parser.add_argument("--push", action='store_true', required=False, help="Upload the new approve emoji instead of just creating it", default=False)
class ProgressBar:
loading_bars = "▁▂▃▄▅▆▇█"
def __init__(self, elements):
self.step_size = int(elements / 800)
self.loading = [" " for i in range(100)]
self.loading_bar_position = 0
self.curr = 0
def progress(self, suffix=""):
self.curr += 1
if self.curr % self.step_size == 0:
if self.curr/self.step_size == 8:
self.loading_bar_position += 1
self.curr = 0
if self.loading_bar_position < len(self.loading):
display_char_index = int(self.curr/self.step_size)
if display_char_index < 0:
display_char_index = 0
if display_char_index > len(self.loading_bars):
display_char_index = len(self.loading_bars) - 1
self.loading[self.loading_bar_position] = self.loading_bars[display_char_index]
print("\x1b[1K\r[" + "".join(self.loading) + "]" + suffix, end="")
if __name__== "__main__":
if len(sys.argv) < 2:
argument_parser.print_help()
exit(0)
args = argument_parser.parse_args(sys.argv[1:])
token = tokenmanager.get_token(args.workspace, args.token, args.configfile)
if not token:
exit(1)
emojimap = slack.emoji_listing(token)
alias_list = {}
if emojimap:
print(str(len(emojimap.keys())) + " existing emoji")
if not (args.collect or args.create or args.approve):
print("Requires create with folder path, collect arg, or approve with search term")
exit(1)
if args.approve:
users = slack.user_listing(token)
# print(json.dumps(users[1], indent=2))
approveTarget = args.approve.lower()
target_user = [u for u in users if u['id'].lower() == approveTarget or
u['profile']['real_name_normalized'].lower() == approveTarget or
u['profile']['display_name_normalized'].lower() == approveTarget]
if len(target_user) != 1:
print("Unable to find user")
print(json.dumps(target_user))
exit(1)
# print(json.dumps(target_user, indent=2))
# print(target_user[0]['profile'])
im = Image.open(requests.get(target_user[0]['profile']['image_192'], stream=True).raw)
name = target_user[0]['profile']['display_name_normalized']
if not name or len(name) == 0:
name = target_user[0]['profile']['real_name_normalized']
if args.name:
name = args.name
name = name.replace('.', '').replace(' ', '')
result = generate.approves((name + '.png').lower(), im, args.offset)
if args.push:
responsepayload = slack.upload_emoji(token, result['name'], result['path'], args.dryrun)
# print(responsepayload)
if args.collect:
if not os.path.exists("data/"):
os.mkdir("data/")
if not os.path.exists("data/" + args.workspace):
os.mkdir("data/" + args.workspace)
if not os.path.exists("data/" + args.workspace + "/emojis"):
os.mkdir("data/" + args.workspace + "/emojis")
with open("data/" + args.workspace + "/" + str(int(time.time())), "w") as listing:
listing.write(json.dumps(emojimap, sort_keys=True, indent=4))
with open("data/" + args.workspace + "/keys" + str(int(time.time())), "w") as listing:
l = list(emojimap.keys())
l.sort()
listing.write(json.dumps(l, sort_keys=True, indent=4))
progress_bar = ProgressBar(len(emojimap.keys()))
for x in emojimap.keys():
progress_bar.progress(x)
if emojimap[x].startswith("alias:"):
alias_list[x] = emojimap[x]
else:
ext = emojimap[x][-3:]
location = "data/" + args.workspace + "/emojis/" + x + "." + ext
if not os.path.exists(location):
if not args.dryrun:
imageresponse = requests.get(emojimap[x], stream=True)
file = open(location, "wb")
imageresponse.raw.decode_content = True
# Copy the response stream raw data to local image file.
shutil.copyfileobj(imageresponse.raw, file)
print("")
print(json.dumps(alias_list))
if args.create:
count = 0
total_mismatch = 0
failure_map = {}
total_fail_count = 0
batch_size = int(args.batch_size)
batch_remaining = batch_size
calculated_path = os.path.join(args.create, "")
emoji_files = {}
if args.recursive:
for dirpath, dirs, filenames in os.walk(calculated_path):
for filename in filenames:
emojiname = filename.split('.')[0]
if filename.endswith(IMAGE_EXTENSIONS) and not (emojiname in emojimap) and not (emojiname in emoji_files):
emoji_files[emojiname] = os.path.join(dirpath, filename)
total_mismatch += 1
else:
for filename in os.listdir(calculated_path):
emojiname = filename.split('.')[0]
if filename.endswith(IMAGE_EXTENSIONS) and not emojiname in emojimap and not emojiname in emoji_files:
emoji_files[emojiname] = os.path.join(calculated_path, filename)
total_mismatch += 1
print(emoji_files)
for emojiname, filepath in emoji_files.items():
if not emojiname == "" and not emojiname in emojimap:
responsepayload = slack.upload_emoji(token, emojiname, filepath, args.dryrun)
batch_remaining -= 1
if not responsepayload['ok']:
if not responsepayload['error'] in failure_map:
failure_map[responsepayload['error']] = 0
total_fail_count += 1
failure_map[responsepayload['error']] += 1
print(filename + " " + emojiname)
print("not okay, sleeping")
print('\033[93m' + responsepayload['error'] + '\033[0m')
time.sleep(5)
else:
count += 1
print(str(count) + "/" + str(total_mismatch - total_fail_count) + " " + emojiname)
if batch_remaining == 0:
print('\033[34mshhhh... sleeeping\033[0m')
time.sleep(10)
batch_remaining = batch_size
print('\033[92m' + str(count) + "/" + str(total_mismatch) + '\033[0m success \033[31m' + str(total_fail_count) + " failed\033[0m")
print(json.dumps(failure_map, sort_keys=True, indent=4))
else:
print("Fail?")