forked from nexmo-community/python-websocket-recorder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
executable file
·106 lines (87 loc) · 2.85 KB
/
server.py
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import tornado.httpserver
import tornado.ioloop
import tornado.web
import tornado.websocket
import json
import os
from string import Template
import wave
import time
HOSTNAME = 'example.ngrok.io' #Change to the hostname of your server
def convert():
f = wave.open('static/audio.wav', 'wb')
f.setnchannels(1)
f.setsampwidth(2)
f.setframerate(16000)
with open('static/audio.raw', 'rb') as src:
f.writeframes(src.read())
f.close()
def timestamps(d):
p = d[0]
output = []
for ts in d:
td = ts-p
output.append(td*1000)
p=ts
with open('static/timestamps.json', "w") as f:
f.write(json.dumps(output))
class MainHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
with open('static/index.html') as f:
file = f.read()
self.content_type = 'text/html'
self.write(file)
self.finish()
class CallHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
data={}
data['hostname'] = HOSTNAME
filein = open('ncco.json')
src = Template(filein.read())
filein.close()
ncco = json.loads(src.substitute(data))
self.write(json.dumps(ncco))
self.set_header("Content-Type", 'application/json; charset="utf-8"')
self.finish()
class EventHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def post(self):
print(self.request.body)
self.content_type = 'text/plain'
self.write('ok')
self.finish()
class WSHandler(tornado.websocket.WebSocketHandler):
buffer = b''
ts = []
def check_origin(self, origin):
return True
def open(self):
print("Websocket Call Connected")
def on_message(self, message):
self.ts.append(time.time())
if type(message) != str:
self.buffer += message
else:
print(message)
def on_close(self):
print("Websocket Call Disconnected")
with open('static/audio.raw', 'wb') as f:
f.write(self.buffer)
convert()
timestamps(self.ts)
def main():
static_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static')
application = tornado.web.Application([(r"/", MainHandler),
(r"/event", EventHandler),
(r"/ncco", CallHandler),
(r"/socket", WSHandler),
(r'/s/(.*)', tornado.web.StaticFileHandler, {'path': static_path}),
])
http_server = tornado.httpserver.HTTPServer(application)
port = int(os.environ.get("PORT", 8004))
http_server.listen(port)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()