forked from jmhobbs/cultofthepartyparrot.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload_parrots.py
106 lines (85 loc) · 3.85 KB
/
upload_parrots.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
#!/usr/bin/env python
import sys
import glob
import argparse
import os
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class ParrotUploader():
browser = None
loggged_in = False
def __init__(self, slack_team):
self.browser = webdriver.Chrome()
self.slack_team = slack_team
def login(self, username, password, google):
if self.loggged_in:
return
self.browser.get('https://{}.slack.com/signin'.format(self.slack_team))
if google:
signin_button = self.browser.find_element_by_partial_link_text("Sign in with Google")
signin_button.click()
username_field = WebDriverWait(self.browser, 30).until(EC.presence_of_element_located((By.ID, "identifierId")))
username_field.send_keys(username)
username_next = self.browser.find_element_by_id("identifierNext")
username_next.click()
password_field = WebDriverWait(self.browser, 30).until(EC.presence_of_element_located((By.NAME, "password")))
password_field.send_keys(password)
password_next = self.browser.find_element_by_id("passwordNext")
password_next.click()
else:
username_field = self.browser.find_element(value="email")
password_field = self.browser.find_element(value='password')
username_field.send_keys(username)
password_field.send_keys(password)
username_field.submit()
self.loggged_in = True
def upload(self, parrot_path):
if not self.loggged_in:
raise ValueError("you must first login before uploading")
emoji_url = 'https://{}.slack.com/customize/emoji'.format(self.slack_team)
if emoji_url not in self.browser.current_url:
self.browser.get(emoji_url)
emoji_table = WebDriverWait(
self.browser, 30).until(EC.presence_of_element_located(
(By.ID, "custom_emoji")))
parrot_name = os.path.basename(parrot_path).split('.')[0]
if ":{}:".format(parrot_name) in emoji_table.text:
print("emoji :{}: already exists".format(parrot_name))
return
emoji_name = self.browser.find_element(value='emojiname')
emoji_name.clear()
emoji_name.send_keys(parrot_name)
emoji_file = self.browser.find_element(value='emojiimg')
emoji_file .send_keys(parrot_path)
emoji_file.submit()
print("emoji :{}: uploaded".format(parrot_name))
def cleanup(self):
if self.browser:
self.browser.quit()
def main():
parser = argparse.ArgumentParser(
description='Use a browser to automatically upload all party parrots into a Slack account')
parser.add_argument('--team', dest='slack_team',
help='The slack team in which to upload')
parser.add_argument('--username',
help='The slack username with which to upload')
parser.add_argument('--password',
help='The slack password with which to login')
parser.add_argument('--google', action='store_true',
help='Only add this arg if you use Google signin for your Slack team')
args = parser.parse_args()
uploader = ParrotUploader(slack_team=args.slack_team)
dir_path = os.path.dirname(os.path.realpath(__file__))
all_the_parrots = glob.glob(os.path.join(dir_path, 'parrots/hd/*.gif'))
all_the_parrots += glob.glob(os.path.join(dir_path, 'parrots/*.gif'))
try:
uploader.login(args.username, args.password, args.google)
for parrot in all_the_parrots:
uploader.upload(parrot)
finally:
uploader.cleanup()
if __name__ == '__main__':
sys.exit(main())