forked from DragonMinded/PyStreaming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanage.py
More file actions
217 lines (188 loc) · 6.89 KB
/
Copy pathmanage.py
File metadata and controls
217 lines (188 loc) · 6.89 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import argparse
import sys
import yaml
from typing import Any, Dict, Optional
from data import Data, DBCreateException
def create(config: Dict[str, Any]) -> None:
data = Data(config)
data.create()
data.close()
def generate(config: Dict[str, Any], message: Optional[str], allow_empty: bool) -> None:
if message is None:
raise Exception('Please provide a message!')
data = Data(config)
data.generate(message, allow_empty)
data.close()
def upgrade(config: Dict[str, Any]) -> None:
data = Data(config)
data.upgrade()
data.close()
def addstreamer(config: Dict[str, Any], username: Optional[str], key: Optional[str]) -> None:
if username is None:
raise Exception('Please provide a username!')
if key is None:
raise Exception('Please provide a stream key!')
data = Data(config)
data.execute(
"INSERT INTO streamersettings (`username`, `key`) VALUES (:username, :key)",
{'username': username, 'key': key},
)
data.close()
def dropstreamer(config: Dict[str, Any], username: Optional[str]) -> None:
if username is None:
raise Exception('Please provide a username!')
data = Data(config)
data.execute(
"DELETE FROM streamersettings WHERE username = :username LIMIT 1",
{'username': username},
)
data.close()
def liststreamers(config: Dict[str, Any]) -> None:
data = Data(config)
cursor = data.execute("SELECT username FROM streamersettings")
for result in cursor.fetchall():
print(f"Streamer: {result['username']}")
data.close()
def streamdescription(config: Dict[str, Any], username: Optional[str], description: Optional[str]) -> None:
if username is None:
raise Exception('Please provide a username!')
data = Data(config)
data.execute(
"UPDATE streamersettings SET description = :description WHERE username = :username",
{'username': username, 'description': description},
)
data.close()
def streampassword(config: Dict[str, Any], username: Optional[str], password: Optional[str]) -> None:
if username is None:
raise Exception('Please provide a username!')
data = Data(config)
data.execute(
"UPDATE streamersettings SET streampass = :password WHERE username = :username",
{'username': username, 'password': password},
)
data.close()
def addemote(config: Dict[str, Any], alias: Optional[str], uri: Optional[str]) -> None:
if alias is None:
raise Exception('Please provide an alias!')
if uri is None:
raise Exception('Please provide a URI!')
if ':' in alias:
raise Exception('Aliases should not contain the ":" character!')
data = Data(config)
data.execute(
"INSERT INTO emotes (`alias`, `uri`) VALUES (:alias, :uri)",
{'alias': alias, 'uri': uri},
)
data.close()
def dropemote(config: Dict[str, Any], alias: Optional[str]) -> None:
if alias is None:
raise Exception('Please provide an alias!')
if ':' in alias:
raise Exception('Aliases should not contain the ":" character!')
data = Data(config)
data.execute(
"DELETE FROM emotes WHERE alias = :alias LIMIT 1",
{'alias': alias},
)
data.close()
def listemotes(config: Dict[str, Any]) -> None:
data = Data(config)
cursor = data.execute("SELECT alias, uri FROM emotes")
for result in cursor.fetchall():
print(f"{result['alias']}: {result['uri']}")
data.close()
def main() -> None:
parser = argparse.ArgumentParser(description="A utility for initializing and updating the streaming backend DB.")
parser.add_argument(
"operation",
help="Operation to perform, options include 'create', 'generate', 'upgrade', 'addstreamer', 'dropstreamer', 'liststreamers', 'streamdescription', 'streampassword', 'addemote', 'dropemote', 'listemotes'.",
type=str,
)
parser.add_argument(
"-m",
"--message",
help="Message to use for auto-generated migration scripts.",
type=str,
)
parser.add_argument(
"-e",
"--allow-empty",
help="Allow empty migration script to be generated. Useful for data-only migrations.",
action='store_true',
)
parser.add_argument(
"-u",
"--username",
help="Streamer username to use when adding or dropping a streamer and when updating the streamer description or password.",
type=str,
)
parser.add_argument(
"-k",
"--key",
help="Stream key in OBS or other streaming program when adding a streamer.",
type=str,
)
parser.add_argument(
"-a",
"--alias",
help="Alias to use for an emote when adding or dropping a custom emote.",
type=str,
)
parser.add_argument(
"-l",
"--uri",
help="URI of the emote when adding a custom emote.",
type=str,
)
parser.add_argument(
"-d",
"--description",
help="Provide the description for streamer page when setting the streamer description.",
type=str,
)
parser.add_argument(
"-p",
"--password",
help="Provide the password for streamer page when setting the streamer password.",
type=str,
)
parser.add_argument(
"-n",
"--no-password",
help="Unset the password for streamer page when setting the streamer password.",
action="store_true",
)
parser.add_argument("-c", "--config", help="Core configuration. Defaults to config.yaml", type=str, default="config.yaml")
args = parser.parse_args()
config = yaml.safe_load(open(args.config))
config['database']['engine'] = Data.create_engine(config)
try:
if args.operation == "create":
create(config)
elif args.operation == "generate":
generate(config, args.message, args.allow_empty)
elif args.operation == "upgrade":
upgrade(config)
elif args.operation == "addstreamer":
addstreamer(config, args.username, args.key)
elif args.operation == "dropstreamer":
dropstreamer(config, args.username)
elif args.operation == "liststreamers":
liststreamers(config)
elif args.operation == "streamdescription":
streamdescription(config, args.username, args.description)
elif args.operation == "streampassword":
streampassword(config, args.username, None if args.no_password else args.password)
elif args.operation == "addemote":
addemote(config, args.alias, args.uri)
elif args.operation == "dropemote":
dropemote(config, args.alias)
elif args.operation == "listemotes":
listemotes(config)
else:
raise Exception(f"Unknown operation '{args.operation}'")
except DBCreateException as e:
print(str(e))
sys.exit(1)
if __name__ == '__main__':
main()