-
Notifications
You must be signed in to change notification settings - Fork 3
/
sync_stat.py
executable file
·121 lines (99 loc) · 3.26 KB
/
sync_stat.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
#!/usr/bin/env python
'''
sync-stat - Sync a status to multiple workspaces from the command line
Version: 0.2.1
'''
from ruamel.yaml import YAML
from slackclient import SlackClient
import sys
import argparse
import os
yaml = YAML()
class Post(object):
def __init__(self, status, workspaces):
self.update_status(status, workspaces)
def update_status(self, status, workspaces):
for space in workspaces:
sc = SlackClient(space['auth'])
sc.api_call(
"users.profile.set",
profile = status
)
class Status(object):
def set():
status = input("Status: ")
emoji = input("Emoji: ")
return { "status_text" : status, "status_emoji" : emoji }
class Spaces(object):
home = os.path.expanduser('~')
saveTo = os.path.join(home, '.config/spaces.conf')
def __init__(self):
try:
with open(self.saveTo, 'r') as f:
self.spaces = yaml.load(f)
except:
makeNew = input("No workspaces found. Would you like to setup a workspace?[y/n]: ")
if makeNew == 'y' or makeNew == 'Y' or makeNew == 'yes' or makeNew == 'Yes' or makeNew == 'YES':
self.spaces= {}
self.add_workspace()
with open(self.saveTo, 'r') as f:
self.spaces = yaml.load(f)
else:
quit()
def add_workspace(self):
add = True
while add:
workspace = input("Workspace nickname: ")
#client = input("What chat client is this for? [Slack/Discord]: ")
client = "Slack"
token = input("Workspace legacy token or App token: ")
self.spaces[workspace] = { "auth" : token, "client" : client }
next = input("Add another workspace?: [y/n]")
if next == 'y' or next == "Y" or next == "yes":
pass
else:
add = False
with open(self.saveTo, 'w') as f:
yaml.dump(self.spaces, f)
def select_workspaces(self):
syncTo = []
for workspace in self.spaces:
selection = input("Sync status to %s? [y/n]" % workspace)
if selection == "y" or selection == "Y" or selection == "yes":
syncTo.append(self.spaces[workspace])
return syncTo
def select_all(self):
syncTo = []
for workspace in self.spaces:
syncTo.append(self.spaces[workspace])
return syncTo
def parse():
parser = argparse.ArgumentParser(description="Post status to multiple workspaces at once")
parser.add_argument('-a', '--add', help="Add a new workspace", dest='add', action='store_true')
parser.add_argument('-s', '--status', help="Status to sync", dest='status')
parser.add_argument('-w', '--workspaces', help="Workspaces to sync to, by nickname or 'all' for all spaces",
dest='workspaces')
parser.add_argument('-e', '--emoji', help="Emoji to sync", dest='emoji')
return vars(parser.parse_args())
def main(argv):
#Load workspaces if they exist, query to add workspaces if they do not
work = Spaces()
args = parse()
if args['add'] == True:
work.add_workspace()
if args['status'] != None:
if args['emoji'] != None:
status = { "status_text" : args['status'], "status_emoji" : args['emoji'] }
else:
status = { "status_text" : args['status'], "status_emoji" : "" }
else:
status = Status.set()
if args['workspaces'] != None:
if args['workspaces'] == "all":
toSync = work.select_all()
else:
toSync = work.select_workspaces()
#Get status, select workspaces to sync to, and post to selected workspaces
Post(status, toSync)
if __name__ == "__main__":
main(sys.argv)