-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebui.lua
98 lines (81 loc) · 2.31 KB
/
webui.lua
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
socket = require("socket")
require("webui-page")
host = "0.0.0.0"
port = "8000"
server = assert(socket.bind(host, port))
server:settimeout(0)
commands = {
pause = function()
local curr = mp.get_property_bool("pause")
mp.set_property_bool("pause", not curr)
end,
seek = function(t)
mp.command("seek "..t)
end,
volume = function(v)
local curr = mp.get_property_number("volume")
mp.set_property_number("volume", curr + v)
end
}
function header(code, content_type)
local h = ""
if code == 200 then
h = h.."HTTP/1.1 200 OK\n"
if content_type == "html" then
h = h.."Content-Type: text/html; charset=UTF-8\n"
elseif content_type == "json" then
h = h.."Content-Type: application/json; charset=UTF-8\n"
end
elseif code == 404 then
h = h.."HTTP/1.1 404 Not Found\n"
end
return h.."Connection: close\n\n"
end
function listen()
local connection = server:accept()
if connection == nil then
return
end
local line = connection:receive()
while line ~= nil and line ~= "" do
local request = string.gmatch(line, "%S+")
local method = request()
local path = string.sub(request(), 2)
if method == "POST" then
local components = string.gmatch(path, "[^/]+")
local command = components() or path
local param = components() or ""
local f = commands[command]
if f ~= nil then
f(param);
connection:send(header(200, "html"))
else
connection:send(header(404, nil))
end
connection:close()
return
elseif method == "GET" then
local response = ""
if (path == "") then
connection:send(header(200, "html"))
connection:send(page)
connection:close()
return
else if (path == "status") then
connection:send(header(200, "json"))
local json = [[{"file":"]]..mp.get_property("path")..'",'
json = json..'"length":"'..mp.get_property("length")..'",'
json = json..'"pos":"'..mp.get_property("time-pos")..'",'
json = json..'"volume":"'..mp.get_property("volume")..'"}'
connection:send(json)
connection:close()
return
else
connection:send(header(404, nil))
return
end
end
end
end
end
mp.add_periodic_timer(0.2, listen)