Skip to content

Commit 7f38435

Browse files
committed
Basic update for script
I just wanted to get this basic version done. I'll come back later to implement more features and changes.
1 parent 9dc7ce0 commit 7f38435

File tree

1 file changed

+35
-13
lines changed

1 file changed

+35
-13
lines changed

main.py

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import asyncio
23
import json
34
import simpleobsws
@@ -10,39 +11,50 @@
1011
httpAddress = config.get('http', 'bind_to_address')
1112
httpPort = config.getint('http', 'bind_to_port')
1213
httpAuthKey = config.get('http', 'authentication_key')
13-
if httpAuthKey == '':
14+
if httpAuthKey:
15+
print('Starting HTTP server with AuthKey set to "{}"'.format(httpAuthKey))
16+
else:
1417
print('Starting HTTP server without authentication.')
1518
httpAuthKey = None
16-
else:
17-
print('Starting HTTP server with AuthKey set to "{}"'.format(httpAuthKey))
1819
wsAddress = config.get('obsws', 'ws_address')
1920
wsPort = config.getint('obsws', 'ws_port')
2021
wsPassword = config.get('obsws', 'ws_password')
2122
loop = asyncio.get_event_loop()
22-
ws = simpleobsws.obsws(host=wsAddress, port=wsPort, password=wsPassword, loop=loop)
23+
ws = simpleobsws.WebSocketClient(url='ws://{}:{}'.format(wsAddress, wsPort), password=wsPassword)
2324

2425
def statusmessage(message):
2526
print(str(message) + '... ', end='', flush=True)
2627

28+
def response_to_object(response: simpleobsws.RequestResponse):
29+
ret = {}
30+
ret['requestType'] = response.requestType
31+
ret['requestStatus'] = {'result': response.requestStatus.result, 'code': response.requestStatus.code}
32+
if response.requestStatus.comment:
33+
ret['requestStatus']['comment'] = response.requestStatus.comment
34+
if ret.has_data():
35+
ret['responseData'] = response.responseData
36+
return ret
37+
2738
async def handle_emit_request(request):
2839
"""Handler function for all emit-based HTTP requests. Assumes that you know what you are doing because it will never return an error."""
2940
if ('AuthKey' not in request.headers) and httpAuthKey != None:
30-
return web.json_response({'status':'error', 'error':'AuthKey header is required.'})
41+
return web.json_response({'status': False, 'comment': 'AuthKey header is required.'})
3142
if httpAuthKey == None or (request.headers['AuthKey'] == httpAuthKey):
3243
requesttype = request.match_info['type']
3344
try:
3445
requestdata = await request.json()
3546
except json.decoder.JSONDecodeError:
3647
requestdata = None
37-
await ws.emit(requesttype, requestdata)
38-
return web.json_response({'status':'ok'})
48+
req = simpleobsws.Request(requesttype, requestdata)
49+
await ws.emit(req)
50+
return web.json_response({'status': True})
3951
else:
40-
return web.json_response({'status':'error', 'error':'Bad AuthKey'})
52+
return web.json_response({'status': False, 'comment': 'Bad AuthKey'})
4153

4254
async def handle_call_request(request):
4355
"""Handler function for all call-based HTTP requests."""
4456
if ('AuthKey' not in request.headers) and httpAuthKey != None:
45-
return web.json_response({'status':'error', 'error':'AuthKey header is required.'})
57+
return web.json_response({'status': False, 'comment': 'AuthKey header is required.'})
4658
if httpAuthKey == None or (request.headers['AuthKey'] == httpAuthKey):
4759
requesttype = request.match_info['type']
4860
try:
@@ -51,17 +63,27 @@ async def handle_call_request(request):
5163
if (await request.text()) == '':
5264
requestdata = None
5365
try:
54-
responsedata = await ws.call(requesttype, requestdata)
66+
req = simpleobsws.Request(requesttype, requestdata)
67+
ret = await ws.call(req)
68+
responsedata = {'status': True, 'response': response_to_object(ret)}
5569
except simpleobsws.MessageTimeout:
56-
responsedata = {'status':'error', 'error':'The obs-websocket request timed out.'}
70+
responsedata = {'status': False, 'comment': 'The obs-websocket request timed out.'}
5771
return web.json_response(responsedata)
5872
else:
59-
return web.json_response({'status':'error', 'error':'Bad AuthKey'})
73+
return web.json_response({'status': False, 'comment': 'Bad AuthKey'})
74+
75+
async def init_ws():
76+
await ws.connect()
77+
if not await ws.wait_until_identified():
78+
print('Identification with obs-websocket timed out. Could it be using 4.x?')
79+
return False
80+
return True
6081

6182
app = web.Application()
6283
app.add_routes([web.post('/emit/{type}', handle_emit_request), web.post('/call/{type}', handle_call_request)])
6384
statusmessage('Connecting to obs-websocket')
64-
loop.run_until_complete(ws.connect())
85+
if not loop.run_until_complete(init_ws()):
86+
os._exit(1)
6587
print('[Connected.]')
6688
try:
6789
web.run_app(app, host=httpAddress, port=httpPort)

0 commit comments

Comments
 (0)