--kinoman.az 09/04/2015
require('uppod')

if m_simpleTV.Control.ChangeAdress ~= 'No' then return end

local inAdr =  m_simpleTV.Control.CurrentAdress

if inAdr==nil then return end
--http://www.kinoman.az/ru/tv/watch/9/spacetv
if  not string.match( inAdr, 'kinoman%.az/.-/tv/' ) 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; U; ru) Presto/2.9.168 Version/11.52")
if session == nil then return end

 --INTERNET_OPTION_CONNECT_TIMEOUT
 m_simpleTV.WinInet.SetOptionInt(session,2,3000,0)
 --INTERNET_OPTION_RECEIVE_TIMEOUT
 m_simpleTV.WinInet.SetOptionInt(session,6,5000,0)
---------------------------------------------------------------
           local hash11 = {"i", "u", "L", "v", "I", "9", "d", "V", "o", "5", "m", "H", "f", "Q", "t", "p", "J", "4", "y", "D", "e", "a", "Z", "Y", "s", "="}
           local hash22 = {"k", "2", "B", "8", "T", "x", "w", "0", "z", "W", "1", "b", "3", "U", "R", "g", "n", "7", "X", "G", "N", "M", "l", "c", "6", "O"}

local  rc,answer=m_simpleTV.WinInet.Get(session,inAdr)

 if rc~=200 then
	   m_simpleTV.WinInet.Close(session)
	   m_simpleTV.OSD.ShowMessage("Connection error - " .. rc ,255,10)
	   return
 end
-- debug_in_file(answer .. '\n')
local st =  findpattern(answer,'swf%?st=(.-)&',1,7,1)

if st == nil then 
	   m_simpleTV.OSD.ShowMessage("Kinoman error - can't find channel" ,255,10)
	   return
 end

local url = uppod.DecodeUppod(st,hash11,hash22)

url = url ..'?rand=' .. math.random()
--debug_in_file(url .. '\n')

rc,answer=m_simpleTV.WinInet.Get(session,url)
m_simpleTV.WinInet.Close(session)

if rc~=200 then
       m_simpleTV.WinInet.Close(session)
	   return
end

answer = uppod.DecodeUppod(answer,hash11,hash22)
--debug_in_file(answer.. "\n\n")
answer = string.gsub(answer,'\\','')

local retAdr = findpattern( answer,'file":"(.-)"',1,7,1)
if retAdr == nil then return end

m_simpleTV.Control.CurrentAdress = retAdr ..'$OPT:http-user-agent=Opera/9.80 (Windows NT 6.1; U; ru) Presto/2.9.168 Version/11.52'

--debug_in_file(retAdr.. "\n\n")
