2015-12-07 20:29:14 +01:00
|
|
|
#!/usr/bin/lua
|
|
|
|
|
|
|
|
local host = os.getenv("HTTP_HOST")
|
|
|
|
local proto = os.getenv("SERVER_PROTOCOL")
|
|
|
|
local query = os.getenv("QUERY_STRING")
|
|
|
|
local method = os.getenv("REQUEST_METHOD")
|
|
|
|
local clength = os.getenv("CONTENT_LENGTH")
|
|
|
|
local ctype = os.getenv("CONTENT_TYPE")
|
|
|
|
|
|
|
|
function http_print(s)
|
|
|
|
if s then
|
|
|
|
io.stdout:write(tostring(s).."\r\n")
|
|
|
|
else
|
|
|
|
io.stdout:write("\r\n")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if #arg> 0 then
|
|
|
|
method="GET"
|
|
|
|
query="select=m3u"
|
|
|
|
proto = "HTTP/1.0"
|
|
|
|
end
|
|
|
|
|
|
|
|
function SendError(err,desc)
|
|
|
|
http_print(proto.." "..err)
|
|
|
|
http_print("Content-Type: text/html")
|
|
|
|
http_print()
|
|
|
|
local file = io.open("e404.html")
|
|
|
|
if file then
|
|
|
|
local tmp = file:read("*a")
|
|
|
|
tmp = string.gsub(tmp,"404 Not Found",err .. " " .. desc)
|
|
|
|
http_print(tmp)
|
|
|
|
file:close()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-07 17:50:38 +01:00
|
|
|
function CreateM3U(host)
|
2015-12-09 13:05:26 +01:00
|
|
|
local m3u = {}
|
|
|
|
table.insert(m3u,"#EXTM3U".."\n")
|
2016-01-11 19:17:48 +01:00
|
|
|
|
2016-01-07 17:50:38 +01:00
|
|
|
local file = io.open("/config/ChannelList.json")
|
|
|
|
if file then
|
|
|
|
local json = file:read("*a")
|
|
|
|
file:close()
|
|
|
|
local newdecoder = require("lunajson.decoder")
|
|
|
|
local decode = newdecoder()
|
|
|
|
local channellist = decode(json)
|
|
|
|
for _,group in ipairs(channellist.GroupList) do
|
|
|
|
for _,channel in ipairs(group.ChannelList) do
|
|
|
|
table.insert(m3u,"#EXTINF:0,"..group.Title.." - "..channel.Title.."\n")
|
2016-01-11 19:17:48 +01:00
|
|
|
table.insert(m3u,"rtsp://"..host..":554/"..channel.Request.."\n")
|
2015-12-07 20:29:14 +01:00
|
|
|
end
|
2016-01-07 17:50:38 +01:00
|
|
|
end
|
|
|
|
else
|
|
|
|
local SourceList = LoadSourceList()
|
|
|
|
for _,f in pairs(SourceList) do
|
|
|
|
if f.system == "dvbs" or f.system == "dvbs2" then
|
|
|
|
for _,c in ipairs(f.ChannelList) do
|
|
|
|
table.insert(m3u,"#EXTINF:0,"..f.title.." - "..c.title.."\n")
|
|
|
|
table.insert(m3u,"rtsp://"..host..":554/?src="..f.src.."&"..c.request.."\n")
|
|
|
|
end
|
|
|
|
else
|
|
|
|
for _,c in ipairs(f.ChannelList) do
|
|
|
|
table.insert(m3u,"#EXTINF:0,"..f.title.." - "..c.title.."\n")
|
|
|
|
table.insert(m3u,"rtsp://"..host..":554/?"..c.request.."\n")
|
|
|
|
end
|
2015-12-07 20:29:14 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-12-09 13:05:26 +01:00
|
|
|
return table.concat(m3u)
|
2015-12-07 20:29:14 +01:00
|
|
|
end
|
|
|
|
|
2016-01-24 12:58:42 +01:00
|
|
|
local function CompareTitle(a,b)
|
|
|
|
return a.Title < b.Title
|
|
|
|
end
|
|
|
|
|
|
|
|
function Legacy2JSON()
|
|
|
|
local newencoder = require("lunajson.encoder")
|
|
|
|
local SourceList = LoadSourceList()
|
|
|
|
local ChannelList = {}
|
|
|
|
ChannelList.GroupList = {}
|
|
|
|
local Group
|
|
|
|
local Channel
|
|
|
|
local f,c
|
|
|
|
|
2015-12-08 17:46:03 +01:00
|
|
|
for _,f in pairs(SourceList) do
|
2016-01-24 12:58:42 +01:00
|
|
|
Group = { Title=f.title, ChannelList = { } }
|
|
|
|
table.insert(ChannelList.GroupList,Group)
|
2015-12-08 17:46:03 +01:00
|
|
|
|
2016-01-24 12:58:42 +01:00
|
|
|
if f.system == "dvbs" or f.system == "dvbs2" then
|
|
|
|
src = 'src='..f.src..'&'
|
|
|
|
end
|
|
|
|
for _,c in ipairs(f.ChannelList) do
|
|
|
|
local tracks = {}
|
|
|
|
local track
|
|
|
|
for track in c.tracks:gmatch("%d+") do
|
|
|
|
table.insert(tracks,tonumber(track))
|
2015-12-08 17:46:03 +01:00
|
|
|
end
|
2016-01-24 12:58:42 +01:00
|
|
|
tracks[0] = #tracks
|
|
|
|
|
|
|
|
Channel = { Title=c.title,
|
|
|
|
Request = '?'..src..c.request,
|
|
|
|
Tracks=tracks }
|
2015-12-08 17:46:03 +01:00
|
|
|
|
2016-01-24 12:58:42 +01:00
|
|
|
table.insert(Group.ChannelList,Channel)
|
2015-12-08 17:46:03 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-24 12:58:42 +01:00
|
|
|
for _,Group in ipairs(ChannelList.GroupList) do
|
|
|
|
table.sort(Group.ChannelList,CompareTitle)
|
|
|
|
Group.ChannelList[0] = #Group.ChannelList
|
|
|
|
end
|
|
|
|
table.sort(ChannelList.GroupList,CompareTitle)
|
|
|
|
ChannelList.GroupList[0] = #ChannelList.GroupList
|
|
|
|
|
|
|
|
local encode = newencoder()
|
|
|
|
local data = encode(ChannelList)
|
|
|
|
return data
|
2015-12-08 17:46:03 +01:00
|
|
|
end
|
|
|
|
|
2016-01-25 16:15:59 +01:00
|
|
|
function CreateJSON()
|
2016-01-07 17:50:38 +01:00
|
|
|
local data = nil
|
|
|
|
local file = io.open("/config/ChannelList.json")
|
|
|
|
if file then
|
|
|
|
data = file:read("*a")
|
|
|
|
file:close()
|
|
|
|
else
|
2016-01-24 12:58:42 +01:00
|
|
|
data = Legacy2JSON()
|
2016-01-07 17:50:38 +01:00
|
|
|
end
|
|
|
|
return data
|
2015-12-08 17:46:03 +01:00
|
|
|
end
|
|
|
|
|
2016-01-25 16:15:59 +01:00
|
|
|
function TransponderList(user)
|
|
|
|
local data = nil
|
|
|
|
local file
|
|
|
|
if user then
|
|
|
|
file = io.open("/config/TransponderList.json","r")
|
|
|
|
end
|
|
|
|
if not file then
|
|
|
|
file = io.open("/var/channels/TransponderList.json","r")
|
|
|
|
end
|
|
|
|
if file then
|
|
|
|
data = file:read("*a")
|
|
|
|
file:close()
|
|
|
|
end
|
|
|
|
return data
|
|
|
|
end
|
|
|
|
|
2016-01-07 17:50:38 +01:00
|
|
|
function LoadSourceList()
|
|
|
|
local db = require("DataBase")
|
|
|
|
local SourceList = {}
|
2016-01-25 16:15:59 +01:00
|
|
|
local f,c
|
2015-12-08 17:46:03 +01:00
|
|
|
|
2016-01-07 17:50:38 +01:00
|
|
|
for _,f in ipairs(db.SourceList) do
|
|
|
|
f.ChannelList = {}
|
|
|
|
SourceList[f.refid] = f
|
|
|
|
end
|
2015-12-07 20:29:14 +01:00
|
|
|
|
2016-01-07 17:50:38 +01:00
|
|
|
for _,c in ipairs(db.ChannelList) do
|
|
|
|
local f = SourceList[c.refid]
|
|
|
|
if f then
|
|
|
|
table.insert(f.ChannelList,c)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return SourceList
|
2015-12-07 20:29:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
if method == "GET" then
|
|
|
|
local filename = nil
|
2015-12-08 17:46:03 +01:00
|
|
|
local contenttype = nil
|
2015-12-07 20:29:14 +01:00
|
|
|
local data = nil
|
|
|
|
|
|
|
|
if string.match(query,"select=m3u") then
|
|
|
|
filename = "channellist.m3u"
|
2015-12-08 17:46:03 +01:00
|
|
|
contenttype = "text/m3u; charset=utf-8"
|
2016-01-07 17:50:38 +01:00
|
|
|
data = CreateM3U(host)
|
2015-12-08 17:46:03 +01:00
|
|
|
elseif string.match(query,"select=json") then
|
2016-01-24 12:58:42 +01:00
|
|
|
filename = "ChannelList.json"
|
2015-12-08 17:46:03 +01:00
|
|
|
contenttype = "application/json; charset=utf-8"
|
2016-01-25 16:15:59 +01:00
|
|
|
data = CreateJSON()
|
|
|
|
elseif string.match(query,"select=stl") then
|
|
|
|
filename = "TransponderList.json"
|
|
|
|
contenttype = "application/json; charset=utf-8"
|
|
|
|
data = TransponderList(false)
|
|
|
|
elseif string.match(query,"select=tl") then
|
|
|
|
filename = "TransponderList.json"
|
|
|
|
contenttype = "application/json; charset=utf-8"
|
|
|
|
data = TransponderList(true)
|
2015-12-07 20:29:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
if data then
|
|
|
|
http_print(proto.." 200" )
|
|
|
|
http_print("Pragma: no-cache")
|
|
|
|
http_print("Cache-Control: no-cache")
|
2015-12-08 17:46:03 +01:00
|
|
|
http_print("Content-Type: "..contenttype)
|
2015-12-07 20:29:14 +01:00
|
|
|
if filename then
|
|
|
|
http_print('Content-Disposition: filename="'..filename..'"')
|
|
|
|
end
|
|
|
|
http_print(string.format("Content-Length: %d",#data))
|
|
|
|
http_print()
|
|
|
|
http_print(data)
|
|
|
|
else
|
|
|
|
SendError("404","not found")
|
|
|
|
end
|
|
|
|
|
|
|
|
else
|
|
|
|
SendError("500","What")
|
|
|
|
end
|