summaryrefslogblamecommitdiff
path: root/on_publish.py
blob: 8337db51325579fd3ae6cdd33ad6f65814a4211c (plain) (tree)


















































































                                                                               
#!/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)