Skip to content

Commit be10aad

Browse files
committed
Some final changes
1 parent 984d3a5 commit be10aad

File tree

2 files changed

+55
-13
lines changed

2 files changed

+55
-13
lines changed

config.ini

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
http_bind_to_address = 127.0.0.1
2-
http_bind_to_port = 4445
1+
[http]
2+
bind_to_address = 127.0.0.1
3+
bind_to_port = 4445
4+
authentication_key = # Leave empty if no authentication is required.
35

46
[obsws]
57
ws_address = 127.0.0.1
68
ws_port = 4444
7-
ws_password =
9+
ws_password = # Only necessary if "Enable authentication" is checked in the obs-websocket settings menu.

main.py

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,69 @@
11
import asyncio
22
import json
33
import simpleobsws
4-
import liteconfig
54
import aiohttp
65
from aiohttp import web
6+
from configparser import ConfigParser
77

8-
cfg = liteconfig.Config("config.ini")
8+
config = ConfigParser()
9+
config.read('config.ini')
10+
httpAddress = config.get('http', 'bind_to_address')
11+
httpPort = config.getint('http', 'bind_to_port')
12+
httpAuthKey = config.get('http', 'authentication_key')
13+
if httpAuthKey == '':
14+
print('Starting HTTP server without authentication.')
15+
httpAuthKey = None
16+
else:
17+
print('Starting HTTP server with AuthKey set to "{}"'.format(httpAuthKey))
18+
wsAddress = config.get('obsws', 'ws_address')
19+
wsPort = config.getint('obsws', 'ws_port')
20+
wsPassword = config.get('obsws', 'ws_password')
921
loop = asyncio.get_event_loop()
10-
ws = simpleobsws.obsws(host=cfg.obsws.ws_address, port=cfg.obsws.ws_port, password=cfg.obsws.ws_password, loop=loop)
22+
ws = simpleobsws.obsws(host=wsAddress, port=wsPort, password=wsPassword, loop=loop)
23+
24+
def statusmessage(message):
25+
print(str(message) + '... ', end='', flush=True)
1126

1227
async def handle_emit_request(request):
1328
"""Handler function for all emit-based HTTP requests. Assumes that you know what you are doing because it will never return an error."""
14-
requesttype = request.match_info['type']
15-
requestdata = await request.json()
16-
await ws.emit(requesttype, requestdata)
17-
return web.json_response({'status':'ok'})
29+
if ('AuthKey' not in request.headers) and httpAuthKey != None:
30+
return web.json_response({'status':'error', 'error':'AuthKey header is required.'})
31+
if httpAuthKey == None or (request.headers['AuthKey'] == httpAuthKey):
32+
requesttype = request.match_info['type']
33+
try:
34+
requestdata = await request.json()
35+
except json.decoder.JSONDecodeError:
36+
requestdata = None
37+
await ws.emit(requesttype, requestdata)
38+
return web.json_response({'status':'ok'})
39+
else:
40+
return web.json_response({'status':'error', 'error':'Bad AuthKey'})
1841

1942
async def handle_call_request(request):
2043
"""Handler function for all call-based HTTP requests."""
21-
pass
44+
if ('AuthKey' not in request.headers) and httpAuthKey != None:
45+
return web.json_response({'status':'error', 'error':'AuthKey header is required.'})
46+
if httpAuthKey == None or (request.headers['AuthKey'] == httpAuthKey):
47+
requesttype = request.match_info['type']
48+
try:
49+
requestdata = await request.json()
50+
except json.decoder.JSONDecodeError:
51+
if (await request.text()) == '':
52+
requestdata = None
53+
try:
54+
responsedata = await ws.call(requesttype, requestdata)
55+
except simpleobsws.MessageTimeout:
56+
responsedata = {'status':'error', 'error':'The obs-websocket request timed out.'}
57+
return web.json_response(responsedata)
58+
else:
59+
return web.json_response({'status':'error', 'error':'Bad AuthKey'})
2260

2361
app = web.Application()
24-
app.add_routes([web.post('/emit/{type}, handle_emit_request), web.post('/call/{type}, handle_call_request)])
62+
app.add_routes([web.post('/emit/{type}', handle_emit_request), web.post('/call/{type}', handle_call_request)])
63+
statusmessage('Connecting to obs-websocket')
2564
loop.run_until_complete(ws.connect())
65+
print('[Connected.]')
2666
try:
27-
web.run_app(app)
67+
web.run_app(app, host=httpAddress, port=httpPort)
2868
except KeyboardInterrupt:
2969
print('Shutting down...')

0 commit comments

Comments
 (0)