summaryrefslogtreecommitdiff
path: root/on_publish.py
blob: 8337db51325579fd3ae6cdd33ad6f65814a4211c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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)