1
+ import os
1
2
import asyncio
2
3
import json
3
4
import simpleobsws
10
11
httpAddress = config .get ('http' , 'bind_to_address' )
11
12
httpPort = config .getint ('http' , 'bind_to_port' )
12
13
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 :
14
17
print ('Starting HTTP server without authentication.' )
15
18
httpAuthKey = None
16
- else :
17
- print ('Starting HTTP server with AuthKey set to "{}"' .format (httpAuthKey ))
18
19
wsAddress = config .get ('obsws' , 'ws_address' )
19
20
wsPort = config .getint ('obsws' , 'ws_port' )
20
21
wsPassword = config .get ('obsws' , 'ws_password' )
21
22
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 )
23
24
24
25
def statusmessage (message ):
25
26
print (str (message ) + '... ' , end = '' , flush = True )
26
27
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
+
27
38
async def handle_emit_request (request ):
28
39
"""Handler function for all emit-based HTTP requests. Assumes that you know what you are doing because it will never return an error."""
29
40
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.' })
31
42
if httpAuthKey == None or (request .headers ['AuthKey' ] == httpAuthKey ):
32
43
requesttype = request .match_info ['type' ]
33
44
try :
34
45
requestdata = await request .json ()
35
46
except json .decoder .JSONDecodeError :
36
47
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 })
39
51
else :
40
- return web .json_response ({'status' :'error' , 'error' : 'Bad AuthKey' })
52
+ return web .json_response ({'status' : False , 'comment' : 'Bad AuthKey' })
41
53
42
54
async def handle_call_request (request ):
43
55
"""Handler function for all call-based HTTP requests."""
44
56
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.' })
46
58
if httpAuthKey == None or (request .headers ['AuthKey' ] == httpAuthKey ):
47
59
requesttype = request .match_info ['type' ]
48
60
try :
@@ -51,17 +63,27 @@ async def handle_call_request(request):
51
63
if (await request .text ()) == '' :
52
64
requestdata = None
53
65
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 )}
55
69
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.' }
57
71
return web .json_response (responsedata )
58
72
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
60
81
61
82
app = web .Application ()
62
83
app .add_routes ([web .post ('/emit/{type}' , handle_emit_request ), web .post ('/call/{type}' , handle_call_request )])
63
84
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 )
65
87
print ('[Connected.]' )
66
88
try :
67
89
web .run_app (app , host = httpAddress , port = httpPort )
0 commit comments