vlc-mediaklikk-video/mediaklikk-video.lua

149 lines
3.9 KiB
Lua
Raw Normal View History

local dkjson = require('dkjson')
local log = {}
local openGraph = {}
local streams = {}
local tables = {}
local urlPatterns = {
'hirado%.hu',
'm4sport%.hu',
'mediaklikk%.hu'
2017-09-09 17:22:14 +02:00
}
function probe()
return vlc.access:match('https?') and tables.find(urlPatterns, function(pattern)
return vlc.path:match(pattern)
end)
2017-09-09 17:22:14 +02:00
end
function parse()
local pageSource = streams.readAll(vlc)
local playerOptionsJson = pageSource:match('pl.setup%( (%b{}) %);')
if playerOptionsJson then
log.dbg('Found player options json, finding playlist items of type hls')
local playerOptions = dkjson.decode(playerOptionsJson)
local playlistItems = tables.filter(playerOptions.playlist, function(playlistItem)
return playlistItem.type == 'hls'
end)
log.dbg('Number of playlist items:', #playlistItems)
local params = tables.map(tables.toMap(vlc.path:gmatch('[?&]([^=]+)=([^&]*)')), function(param)
return vlc.strings.decode_uri(param)
end)
return tables.map(playlistItems, function(playlistItem)
return {
path = vlc.access .. ':' .. playlistItem.file,
title = params.title,
arturl = params.bgimage
}
end)
end
log.dbg('Cannot find player options json, finding embedded players');
local playerSetupJsons = tables.toArray(pageSource:gmatch('mtva_player_manager%.player%(document%.getElementById%("player_%d+_%d+"%), (%b{})%);'));
2017-09-09 17:22:14 +02:00
log.dbg('Number of players:', #playerSetupJsons)
2017-09-09 17:22:14 +02:00
return tables.map(playerSetupJsons, function(playerSetupJson)
local playerSetup = dkjson.decode(playerSetupJson)
local video = playerSetup.streamId or playerSetup.token
2017-09-09 17:22:14 +02:00
if not video then
log.warn('Cannot find either streamId or token in player setup json:', playerSetupJson)
return nil
end
local title = playerSetup.title or openGraph.property(pageSource, 'title')
local arturl = (playerSetup.bgImage and vlc.access .. ':' .. playerSetup.bgImage) or openGraph.property(pageSource, 'image')
local playerUrl = vlc.access .. '://player.mediaklikk.hu/playernew/player.php?video=' .. video ..
((title and '&title=' .. vlc.strings.encode_uri_component(title)) or '') ..
((arturl and '&bgimage=' .. vlc.strings.encode_uri_component(arturl)) or '')
2017-09-09 17:22:14 +02:00
log.dbg('Loading player:', playerUrl)
return {
path = playerUrl,
title = title,
arturl = arturl,
options = {
'http-referrer=' .. vlc.access .. '://' .. vlc.path
}
}
end)
2017-09-09 17:22:14 +02:00
end
local function logger(vlcLog)
return function(...)
vlcLog(table.concat({'mediaklikk-video:', ...}, ' '))
2017-09-09 17:22:14 +02:00
end
end
log.dbg = logger(vlc.msg.dbg)
log.warn = logger(vlc.msg.warn)
log.err = logger(vlc.msg.err)
log.info = logger(vlc.msg.info)
2017-09-09 17:22:14 +02:00
function openGraph.property(source, property)
return source:match('<meta property="og:' .. property .. '" content="(.-)"/>')
2017-09-09 17:22:14 +02:00
end
function streams.readAll(s)
local function iterator(size)
if s == vlc then
return s.read(size)
else
return s:read(size)
end
2017-09-09 17:22:14 +02:00
end
return table.concat(tables.toArray(iterator, 1024, nil));
2017-09-09 17:22:14 +02:00
end
function tables.find(values, predicate)
for key, value in pairs(values) do
if predicate(value, key, values) then
return value, key
2017-09-09 17:22:14 +02:00
end
end
end
2017-09-09 17:22:14 +02:00
function tables.toArray(iterator, state, initialValue)
local result = {}
for value in iterator, state, initialValue do
table.insert(result, value)
2017-09-09 17:22:14 +02:00
end
return result
end
2017-09-09 17:22:14 +02:00
function tables.toMap(iterator, state, initialValue)
local result = {}
for key, value in iterator, state, initialValue do
result[key] = value
end
return result
end
function tables.map(values, transform)
local result = {}
for key, value in pairs(values) do
result[key] = transform(value, key, values)
2017-09-09 17:22:14 +02:00
end
return result
2017-09-09 17:22:14 +02:00
end
function tables.filter(values, predicate)
local result = {}
for key, value in pairs(values) do
if predicate(value, key, values) then
result[key] = value
end
end
return result
end