summaryrefslogtreecommitdiff
path: root/on_publish.py
diff options
context:
space:
mode:
Diffstat (limited to 'on_publish.py')
-rwxr-xr-xon_publish.py83
1 files changed, 83 insertions, 0 deletions
diff --git a/on_publish.py b/on_publish.py
new file mode 100755
index 0000000..8337db5
--- /dev/null
+++ b/on_publish.py
@@ -0,0 +1,83 @@
+#!/usr/bin/python3
+
+import asyncio
+import websockets
+
+import datetime
+import cgi
+import cgitb
+
+import settings
+import utils
+
+cgitb.enable()
+
+form = cgi.FieldStorage()
+
+valid_psks = utils.parse_streamers_file(settings.streamers_file)
+
+psk = form.getvalue("psk")
+source = form.getvalue("source")
+title = form.getvalue("title")
+
+#import ssl
+#ctx = ssl.create_default_context()
+#ctx.check_hostname = False
+#ctx.verify_mode = ssl.CERT_NONE
+
+def log(msg):
+ now = datetime.datetime.now()
+
+ with open(settings.log_file, "a") as f:
+ f.write("[{}] {}\n".format(now, msg))
+
+@asyncio.coroutine
+def sen():
+ #websocket = yield from websockets.connect("wss://127.0.0.1:6969", ssl=ctx)
+ websocket = yield from websockets.connect("ws://127.0.0.1:6969")
+
+ try:
+ yield from websocket.send("SETSOURCE:{}".format(source))
+ if title:
+ yield from websocket.send("SETTITLE:{}".format(title))
+ response = yield from websocket.recv()
+ print(response)
+ finally:
+ yield from websocket.close()
+
+def notify():
+ state = utils.load_state(settings.state_file)
+ state['source'] = source
+ if title:
+ state['title'] = title
+ utils.save_state(state, settings.state_file)
+
+ asyncio.get_event_loop().run_until_complete(sen())
+
+def perform():
+ if source == "live":
+ # Revisar pass
+ if psk in valid_psks:
+ log("Inició stream {}".format(valid_psks[psk]))
+ notify()
+
+ return "200 OK"
+ else:
+ return "403 Forbidden"
+ elif source == "off":
+ log("Terminó stream")
+ notify()
+
+ return "200 OK"
+
+ return "501 Not Implemented"
+
+http_status = perform()
+
+print("Status: " + http_status)
+print("Content-type: text/html")
+print()
+
+print(http_status)
+print(source)
+