-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstateserver.py
executable file
·435 lines (361 loc) · 15.2 KB
/
stateserver.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#!flask/bin/python
## IMPORTANT ASSUMPTIONS MADE:
# show timestamps start on the hour.
# normal shows in real studios aren't currently a thing!!!
import subprocess
from typing import List, Any, Dict, Optional
from flask import Flask, jsonify, request
from flask_cors import CORS # type: ignore
import requests
import datetime
import random
from telnetlib import Telnet
import configparser
config = configparser.RawConfigParser()
config.read("serverconfig.ini")
api_key = config.get("stateserver", "myradio_key")
app = Flask(__name__)
CORS(app) # Enable Cors access-all
SUSTAINER_AUTONEWS = config.get("stateserver", "sustainer_autonews") == "True"
def do_ws_srv_telnet(source: str) -> None:
HOST = "localhost"
print(
"telnet {} {} SEL {}".format(
HOST, config.get("shittyserver", "telnet_port"), source
)
)
tn = Telnet(HOST, int(config.get("shittyserver", "telnet_port")))
tn.write(b"SEL " + str.encode(source) + b"\n")
try:
print(tn.read_until(b"\n").decode("utf-8"))
except EOFError:
pass
else:
tn.close()
def genFail(reason: str, code: int = 400) -> Any:
return jsonify({"status": "FAIL", "reason": reason})
def genPayload(payload: Any) -> Any:
return jsonify({"status": "OK", "payload": payload})
def myradioApiRequest(url: str) -> Any:
res = requests.get("https://ury.org.uk/api/v2/" + url + "?api_key=" + api_key)
if res.ok:
return res.json()["payload"]
else:
raise Exception("err {} {}".format(res.status_code, res.text))
def getNextHourTimestamp() -> int:
current = datetime.datetime.now()
currentPlusHour = current + datetime.timedelta(hours=1)
nextHourStart = currentPlusHour.replace(minute=0, second=0)
nextTimestamp = int(nextHourStart.timestamp())
return nextTimestamp
# sadly we're on python 3.7 so we can't use TypedDict
Connection = Dict[str, Any]
def getConnByID(connID: str) -> Optional[Connection]:
for conn in connections:
if conn["connid"] == connID:
return conn
return None
SOURCE_JUKEBOX = 3 # Set to 8 for testing.
SOURCE_OB = 4
SOURCE_WS = 5
SOURCE_OFFAIR = 8
SOURCES = [SOURCE_JUKEBOX, SOURCE_OB, SOURCE_WS, SOURCE_OFFAIR]
# This array will only hold connections we've validated to be authorised to broadcast.
connections: List[Connection] = []
wsSessions: Dict[str, Dict[str, str]] = {}
def getCurrentShowConnection() -> Optional[Connection]:
for connection in connections:
if (connection["startTimestamp"] <= datetime.datetime.now().timestamp()) and (
connection["endTimestamp"] >= getNextHourTimestamp()
):
return connection
return None
def getNextHourConnection() -> Optional[Connection]:
nextHourTimestamp = getNextHourTimestamp()
isConnectionEnding = False
for connection in connections:
if connection["startTimestamp"] == nextHourTimestamp:
return connection
if connection["endTimestamp"] == nextHourTimestamp:
isConnectionEnding = True
if not isConnectionEnding:
# There isn't a show that starts at the next hour, so we're returning the current connection.
return getCurrentShowConnection()
else:
# The show is ending, return no next show.
return None
def cleanOldConnections() -> None:
global connections
# Go backwards round the loop so that pop's don't interfere with the index.
for i in range(len(connections) - 1, -1, -1):
if connections[i]["endTimestamp"] < datetime.datetime.now().timestamp():
connections.pop(i)
def stateDecider() -> Dict[str, Any]:
currentConnection = getCurrentShowConnection()
nextConnection = getNextHourConnection()
print("currentConnection:", currentConnection)
print("nextConnection:", nextConnection)
willRunAutoNews = True
switchAudioAtMin = 2
newSelSource = None
newWSSource = None
if currentConnection != nextConnection:
print("Will be transitioning")
# The show is transitioning this hour.
if currentConnection:
print("There's a current connection.")
# Current show wants to end their show at 2 mins past
if currentConnection["autoNewsEnd"] == False:
print("This show doesn't want to end with news.")
willRunAutoNews = False
switchAudioAtMin = 2 # no real change here
if nextConnection:
print("There's a next connection.")
# next show wants to begin at 0 mins past hour.
if nextConnection["autoNewsBeginning"] == False:
print("The next connection doesn't want news at start.")
willRunAutoNews = False
switchAudioAtMin = 0
newSelSource = nextConnection["sourceid"]
newWSSource = nextConnection["wsid"] # None if show is not a WS.
else:
print("No next show, going back to jukebox.")
# There isn't a next show, go back to sustainer
newSelSource = SOURCE_JUKEBOX
else:
# Show/sustainer is continuing for another hour.
print("Show / Sustainer is continuing this hour.")
if currentConnection:
print("We're currently doing a show, so check if they want middle news.")
willRunAutoNews = currentConnection["autoNewsMiddle"]
print("(conclusion: {})".format("yes" if willRunAutoNews else "no"))
newSelSource = currentConnection["sourceid"]
newWSSource = currentConnection["wsid"]
elif SUSTAINER_AUTONEWS:
print(
"There's no show on currently, so we're going to AutoNEWS on sustainer"
)
# Jukebox -> NEWS -> Jukebox
newSelSource = SOURCE_JUKEBOX
else:
print(
"There's no show on currently, but AutoNews on sustainer is disabled, so don't do news"
)
# Jukebox -> Jukebox
newSelSource = SOURCE_JUKEBOX
switchAudioAtMin = 0
willRunAutoNews = False
nextState = {
"autoNews": willRunAutoNews,
"switchAudioAtMin": switchAudioAtMin,
"selSource": newSelSource,
"wsSource": newWSSource,
}
return nextState
@app.route("/api/v1/status", methods=["GET"])
def get_status() -> Any:
print(getNextHourTimestamp())
global connections
cleanOldConnections()
return genPayload({"connections": connections, "wsSessions": wsSessions})
@app.route("/api/v1/nextTransition", methods=["GET"])
def get_next_transition() -> Any:
cleanOldConnections()
return genPayload(stateDecider())
@app.route("/api/v1/cancelTimeslot", methods=["POST"])
def post_cancelCheck() -> Any:
global connections
content = request.json
if not content:
return genFail("No parameters provided.")
if not isinstance(content["connid"], int):
return genFail("Request missing valid connid.")
# We're gonna cancel their show.
currentShow = getCurrentShowConnection()
if currentShow and currentShow["connid"] == content["connid"]:
# this show is (at least supposed to be) live now.
# kill their show
# but don't kill it during the news, or after the end time, to avoid unexpected jukeboxing
now = datetime.datetime.now().timestamp()
if now < (currentShow["endTimestamp"] - 15):
print(
"Jukeboxing due to {}'s ({}, {}) cancellation".format(
currentShow["connid"],
currentShow["timeslotid"],
currentShow["wsid"],
)
)
do_ws_srv_telnet("NUL")
subprocess.Popen(["sel", str(SOURCE_JUKEBOX)])
# yeet the connection
for i in range(len(connections)):
if connections[i]["connid"] == content["connid"]:
connections.pop(i)
return genPayload("Connection cancelled.")
return genFail("Connection not found.")
@app.route("/api/v1/registerTimeslot", methods=["POST"])
def post_registerCheck() -> Any:
global connections
content = request.json
if not content:
return genFail("No parameters provided.")
if not isinstance(content["timeslotid"], int):
return genFail("Request missing valid timeslotid.")
if not isinstance(content["memberid"], int):
return genFail("Request missing valid memberid.")
if not isinstance(content["sourceid"], int):
return genFail("Request missing valid source.")
if not content["sourceid"] in SOURCES:
return genFail("Request missing valid source.")
if not isinstance(content["wsid"], str):
return genFail("Request missing valid wsID")
member = myradioApiRequest("user/" + str(content["memberid"]))
if not member:
return genFail("Could not get member.")
timeslot = myradioApiRequest("timeslot/" + str(content["timeslotid"]))
if not timeslot:
return genFail("Could not get tiemslot.")
found_credit = False
for credit in timeslot["credits"]:
if content["memberid"] == credit["memberid"]:
found_credit = True
break
if not found_credit:
return genFail("You are not authorised to broadcast for this timeslot.")
start_time = datetime.datetime.strptime(timeslot["start_time"], "%d/%m/%Y %H:%M")
duration = timeslot["duration"].split(":")
duration_time = datetime.timedelta(hours=int(duration[0]), minutes=int(duration[1]))
end_time = start_time + duration_time
now_time = datetime.datetime.now()
connection: Optional[Connection] = None
for conn in connections:
if content["timeslotid"] == conn["timeslotid"]:
# they've already registered, return the existing session
print(
"found existing connection {} for {}".format(
conn["connid"], conn["timeslotid"]
)
)
connection = conn
# make sure we update their wsID
if "wsid" in content:
connection["wsid"] = content["wsid"]
new_connection = False
if connection is None:
new_connection = True
if start_time - now_time > datetime.timedelta(hours=1):
return genFail(
"This show too far away, please try again within an hour of starting your show."
)
if start_time + duration_time < now_time:
return genFail("This show has already ended.")
if (
start_time - datetime.timedelta(minutes=1)
< now_time
< start_time + datetime.timedelta(minutes=2)
):
return genFail(
"You registered too late. Please re-register after the news."
)
random.seed(a=timeslot["timeslot_id"], version=2)
connection = {
"connid": random.randint(
0, 100000000
), # TODO: this is horrible. I'll sort this later.
"timeslotid": timeslot["timeslot_id"],
"startTimestamp": int(start_time.timestamp()),
"endTimestamp": int(end_time.timestamp()),
"sourceid": content["sourceid"],
"autoNewsBeginning": True,
"autoNewsMiddle": True,
"autoNewsEnd": True,
"wsid": content["wsid"],
}
if start_time + datetime.timedelta(minutes=2) < now_time:
if connection["wsid"] is not None:
# they're late, bring them live now
print(
"({}, {}) late, bringing on air now".format(
connection["connid"], connection["wsid"]
)
)
do_ws_srv_telnet(connection["wsid"])
subprocess.Popen(["sel", "5"])
assert connection is not None
if new_connection:
connections.append(connection)
print(connections)
return genPayload(connection)
@app.route("/api/v1/changeTimeslot", methods=["POST"])
def post_settingsCheck() -> Any:
global connections
content = request.json
if not content:
return genFail("No parameters provided.")
if not isinstance(content["connid"], int):
return genFail("Request missing valid connID.")
if not isinstance(content["beginning"], bool):
return genFail("Request missing valid beginning bool.")
if not isinstance(content["middle"], bool):
return genFail("Request missing valid middle bool.")
if not isinstance(content["end"], bool):
return genFail("Request missing valid end bool.")
if not isinstance(content["sourceid"], int):
return genFail("Request missing valid sourcid.")
for conn in connections:
if conn["connid"] == content["connid"]:
conn["autoNewsBeginning"] = content["beginning"]
conn["autoNewsMiddle"] = content["middle"]
conn["autoNewsEnd"] = content["end"]
conn["sourceid"] = content["sourceid"]
return genPayload(conn)
return genFail("No connection found.")
@app.route("/api/v1/updateWSSessions", methods=["POST"])
def post_wsSessions() -> Any:
global connections
global wsSessions
content = request.json
assert content is not None
# if not content:
# return genFail("No parameters provided.")
oldSessions = wsSessions
wsSessions = content
print("New wsSessions:", wsSessions)
wsids_to_remove = []
wsids_to_add = []
for session in oldSessions:
if not oldSessions[session]["connection_id"] in wsSessions:
wsids_to_remove.append(oldSessions[session]["connection_id"])
print("wsSessions which have disappeared:", wsids_to_remove)
for session in wsSessions:
if not wsSessions[session]["connection_id"] in oldSessions:
wsids_to_add.append(wsSessions[session]["connection_id"])
print("wsSessions which have appeared:", wsids_to_add)
for conn in connections:
if conn["wsid"] in wsids_to_add:
if conn["startTimestamp"] + 120 < datetime.datetime.now().timestamp():
# they're late, bring them on air now
print(
"({}, {}) late, bringing on air now".format(
conn["connid"], conn["wsid"]
)
)
do_ws_srv_telnet(conn["wsid"])
subprocess.Popen(["sel", "5"])
if conn["wsid"] in wsids_to_remove:
print("({}, {}) gone".format(conn["connid"], conn["wsid"]))
conn["wsid"] = None
currentShow = getCurrentShowConnection()
if currentShow and currentShow["connid"] == conn["connid"]:
# they should be on air now, but they've just died. go to jukebox.
# but don't kill it during the news, or after the end time, to avoid unexpected jukeboxing
# Also, avoid killing them if they're on a non-WS source
if currentShow["sourceid"] == SOURCE_WS:
now = datetime.datetime.now().timestamp()
if now < (currentShow["endTimestamp"] - 15):
print("jukeboxing due to their disappearance...")
subprocess.Popen(["sel", str(SOURCE_JUKEBOX)])
do_ws_srv_telnet("NUL")
return genPayload("Thx, K, bye.")
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0")