-- script for seasonvar.ru (11/03/2015)

require('json')
require('uppod')
require('jsdecode')

function RuStr(Str, isFrom)
	if Str == nil then return end
	if isFrom == true then
		return m_simpleTV.Common.string_fromUTF8(Str, 1251)
	else
		return m_simpleTV.Common.string_toUTF8(Str, 1251)
	end
end

-------------------------------------------------------------------------------------------------------------------------
function TryDecodeSeasonvar(w, PatternToFind)
local hash ={
------------------
{"7", "Q", "w", "e", "W", "V", "6", "u", "T", "d", "I", "l", "o", "Z", "N", "f", "X", "n", "c", "4", "M", "z", "x", "G", "m", "="},
{"v", "8", "a", "5", "t", "y", "Y", "2", "R", "B", "3", "U", "0", "i", "1", "k", "H", "9", "p", "D", "s", "J", "L", "b", "g", "j"},
-----------------
{"0", "t", "a", "d", "7", "J", "B", "H", "x", "Q", "y", "Z", "5", "I", "1", "v", "g", "b", "M", "z", "N", "L", "R", "c", "w", "="},
{"u", "U", "f", "T", "k", "W", "X", "4", "8", "2", "9", "Y", "o", "D", "e", "n", "l", "s", "3", "G", "V", "p", "i", "m", "6", "F"},
-----------------
{"b", "U", "N", "I", "Z", "o", "H", "V", "T", "w", "W", "D", "a", "f", "t", "Q", "9", "v", "g", "8", "x", "0", "1", "4", "c", "="},
{"p", "z", "m", "X", "R", "J", "2", "Y", "d", "n", "B", "G", "u", "y", "L", "s", "l", "M", "i", "e", "6", "k", "7", "3", "5", "F"},
-----------------
{"Z", "8", "i", "t", "b", "M", "U", "9", "z", "Y", "1", "s", "7", "c", "R", "J", "I", "l", "G", "a", "m", "k", "f", "4", "6", "="},
{"d", "X", "x", "n", "2", "3", "v", "5", "L", "u", "D", "g", "H", "N", "W", "y", "w", "V", "B", "e", "o", "Q", "p", "0", "T", "O"},
-----------------
{"5", "L", "i", "f", "B", "v", "D", "Q", "9", "T", "a", "V", "o", "Y", "w", "J", "7", "M", "Z", "c", "d", "g", "y", "e", "u", "="},
{"s", "p", "6", "3", "N", "G", "8", "k", "0", "U", "t", "z", "H", "l", "x", "W", "m", "n", "R", "I", "2", "b", "X", "1", "4", "E"},
-----------------
{"W", "a", "u", "V", "d", "M", "8", "6", "7", "i", "H", "Y", "9", "5", "N", "s", "w", "D", "p", "l", "T", "Z", "x", "G", "m", "="},
{"2", "B", "g", "v", "Q", "n", "z", "b", "3", "f", "y", "1", "J", "I", "k", "X", "0", "e", "L", "c", "R", "4", "o", "t", "U", "O"},
-----------------
{"g", "N", "8", "z", "a", "o", "3", "W", "x", "7", "2", "L", "t", "Q", "v", "e", "l", "w", "U", "5", "m", "s", "V", "D", "Y", "="},
{"9", "1", "4", "Z", "I", "M", "6", "i", "X", "J", "d", "f", "T", "u", "G", "n", "y", "B", "c", "k", "R", "b", "p", "0", "H", "C"},
-----------------
{"J", "p", "v", "n", "s", "R", "0", "3", "T", "m", "w", "u", "9", "x", "g", "a", "G", "L", "U", "X", "z", "t", "b", "7", "H", "="},
{"f", "N", "W", "5", "e", "l", "V", "D", "y", "Z", "I", "i", "M", "o", "Q", "1", "B", "8", "2", "6", "c", "d", "4", "Y", "k", "C"}}

local ret
local i = 1
if string.match(w, PatternToFind) then return w end
while hash[i] ~= nil do
	ret = uppod.DecodeUppod(w, hash[i], hash[i+1])
	if string.match(ret, PatternToFind) then return ret end
	i = i + 2
end
return nil
end
-------------------------------------------------------------------------------------------------------------------------
local function Deobfuscating(inStr, Count)
	if inStr == nil then return nil end
	local scr = 'function unPack(code){function indent(code){try{var tabs=0,old=-1,add="";for(var i=0;i<code.length;i++){if(code[i].indexOf("{")!=-1)tabs++;if(code[i].indexOf("}")!=-1)tabs--;if(old!=tabs){old=tabs;add="";while(old>0){add+="\\t";old--;} old=tabs;} code[i]=add+code[i];}}finally{tabs=null;old=null;add=null;} return code;} var env={eval:function(c){code=c;},window:{},document:{}};eval("with(env) {"+code+"}");code=(code+"").replace(/;/g,";\\n").replace(/{/g,"\\n{\\n").replace(/}/g,"\\n}\\n").replace(/\\n;\\n/g,";\\n").replace(/\\n\\n/g,"\\n");code=code.split("\\n");code=indent(code);code=code.join("\\n");return code;}'
	local prescr
	local Str = inStr
	for i = 1, Count, 1 do
		Str = string.gsub(Str, "'", '"')
		Str = string.gsub(Str, '[\t\n]+', '')
		Str = "var code='" .. Str .. "';"
		prescr = Str .. scr
		Str = jsdecode.DoDecode('unPack(code)', false, prescr, 0, 0)
	end
	return Str
end
-------------------------------------------------------------------------------------------------------------------------
function GetPlstSeasonvar(session, inAdr, answer)
	local tmps = 'script type="text/javascript">;eval(.-)</script>'
	local str = findpattern(answer, tmps, 1, 30, 9)
	if str ~= nil then str = Deobfuscating(str, 3) end
	--debug_in_file(answer .. '\n\n', nil, true)
	if str == nil then
		str = findpattern(answer, '<div id="videoplayer(.-)">', 1, 20, 2)
		if str == nil then return nil end
		str = findpattern(answer, '%("#videoplayer' .. str .. '"%)%.load%("player%.php", {(.-)}', 1, 0, 0)
		if str == nil then return nil end
	end
	--debug_in_file('1)\n' .. str .. '\n\n', nil, true)
	local _id = string.match(str, '"id":.-"(.-)"')
	local _type = string.match(str, '"type":.-"(.-)"')
	local _secure = string.match(str, '"secure":.-"(.-)"')
	local _load = findpattern(str, 'load%("(.-)"', 1, 6, 1)
	if _id == nil or _type == nil or _secure == nil or _load == nil then return end
	--debug_in_file('id=' .. _id .. ' type=' .. _type .. ' secure=' .. _secure .. ' load=' .. _load .. '\n') 
	local body = 'id=' .. _id .. '&type=' .. _type .. '&secure=' .. _secure
	local header = 
		'Accept-Language: ru-RU,ru;q=0.9,en;q=0.8\n' ..
		'Referer: ' .. inAdr .. '\n' ..
		'Connection: Keep-Alive\n' .. 
		'Accept: text/html, */*; q=0.01\n' ..
		'X-Requested-With: XMLHttpRequest\n' ..
		'Content-Type: application/x-www-form-urlencoded\n'
	--debug_in_file(body .. '\n')
	--debug_in_file(header .. '\n')
	local rc, answer1 = m_simpleTV.WinInet.Post(session, 'http://seasonvar.ru/'.. _load, nil, header, body)
	if rc ~= 200 then return end
	--debug_in_file(answer1 .. '\n', nil, true)
	tmps = ';eval(.-)\("\.link5play"\)'
	str = findpattern(answer1, tmps, 1, 0, 14)
	if str ~= nil then str = Deobfuscating(str, 3) end
	if str == nil then
		str = findpattern(answer1, 'flashvars(.-);', 1, 0, 0)
		if str == nil then return nil end
	end
	--debug_in_file(str .. '\n\n\n', nil, true) 
	str = findpattern(str, '"pl":"(.-)"', 1, 6, 1)
	local retAdr = TryDecodeSeasonvar(str, 'playls')
	return retAdr
end
-------------------------------------------------------------------------------------------------------------------------
function AppendMassSeasonvar(t,a,i)

local index=1
		while true do
		if t.playlist[index] == nil then break end
		
			if t.playlist[index].playlist~=nil then
						i= AppendMassSeasonvar(t.playlist[index],a,i)
			else 
							a[i] = {}
							a[i].Id   =  i
							a[i].Name =  string.gsub(t.playlist[index].comment,'<br>','')
							a[i].Adress  = t.playlist[index].file	   
							a[i].Adress = string.gsub(a[i].Adress,'%[','%%5B')
							a[i].Adress = string.gsub(a[i].Adress,'%]','%%5D')
							--debug_in_file( a[i].Id .. '  ' .. a[i].Name .. '   ' ..  a[i].Adress .. '\n')
							i = i+1
			end	   
			index=index+1
end

	return i
end
-------------------------------------------------------------------------------------------------------------------------

if m_simpleTV.Control.ChangeAdress ~= 'No' then return end
local inAdr = m_simpleTV.Control.CurrentAdress
if inAdr == nil then return end
if not string.match(inAdr, 'http://seasonvar%.ru') then return end

m_simpleTV.Control.ChangeAdress = 'Yes'
m_simpleTV.Control.CurrentAdress = ''

local session = m_simpleTV.WinInet.New("Opera/9.80 (Windows NT 6.1; WOW64) Presto/2.12.388 Version/12.12")
if session == nil then return end

--INTERNET_OPTION_CONNECT_TIMEOUT
m_simpleTV.WinInet.SetOptionInt(session, 2, 6000, 0)
--INTERNET_OPTION_RECEIVE_TIMEOUT
m_simpleTV.WinInet.SetOptionInt(session, 6, 10000, 0)

local rc, answer = m_simpleTV.WinInet.Get(session, inAdr)
if rc ~= 200 then
	m_simpleTV.WinInet.Close(session)
	return
end
answer = RuStr(answer, true)

local header = findpattern(answer, '<title>(.-)</title>', 1, 7, 8)
if header == nil then header = inAdr end
header = string.gsub(header, '&quot;', '"')
header = string.gsub(header, '&#238;', string.char(238))
if m_simpleTV.Control.CurrentTitle ~= nil then m_simpleTV.Control.CurrentTitle = header end

--debug_in_file(answer, nil, true)

local retAdr = GetPlstSeasonvar(session, inAdr, answer)

--debug_in_file(retAdr .. '\n\n')
if retAdr == nil then
	m_simpleTV.WinInet.Close(session)
	m_simpleTV.OSD.ShowMessage("Error seasonvar.ru - can't find plst address", 255, 10)
	return
end

local url='http://seasonvar.ru'

retAdr = url .. retAdr .. '?rand=' .. math.random()

--debug_in_file('plst='.. retAdr .. '\n\n', nil, true)

local ccc = 'l' .. 'V' .. 'e' .. '3' .. '2' .. '4' .. 'P' .. 'q' .. 's' .. 'I' .. '2' .. '4'
m_simpleTV.WinInet.SetCookies(session, retAdr, 's' .. 'v' .. 'a', ccc)
rc, answer = m_simpleTV.WinInet.Get(session,retAdr)
m_simpleTV.WinInet.Close(session)

if rc ~= 200 then return end
--debug_in_file(answer .. '\n\n', nil, true)

answer = TryDecodeSeasonvar(answer, 'playlist')
if answer == nil then return end
answer = m_simpleTV.Common.string_fromUTF8(answer)
answer = string.gsub(answer,'\n',' ')
answer = string.gsub(answer,'\r','')
--debug_in_file(answer .. '\n\n', nil, true)

local t = json.decode(answer)
if t == nil or t.playlist == nil then
	m_simpleTV.OSD.ShowMessage("Error seasonvar.ru - can't find address", 255, 10)
	return
end

local a, i = {}, 1
retAdr = ''
i = AppendMassSeasonvar(t, a, i)
if i > 1 then retAdr = a[1].Adress end
if i > 2 then
	m_simpleTV.OSD.ShowSelect(header, 0, a, 5000)
end
m_simpleTV.Control.CurrentAdress = retAdr