--script for tuthd.tv (07/09/2015)

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, 'tuthd%.tv' ) then return end

m_simpleTV.Control.ChangeAdress='Yes'
m_simpleTV.Control.CurrentAdress = ''
m_simpleTV.Control.EventPlayingInterval=1000
m_simpleTV.Control.EventTimeOutInterval=10000  
----------------------------------------------------------------
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,8000,0)
 --INTERNET_OPTION_RECEIVE_TIMEOUT
 m_simpleTV.WinInet.SetOptionInt(session,6,8000,0)
---------------------------------------------------------------

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

 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')


answer = findpattern(answer,'source = (.-)swfobject',1,9,20)
--debug_in_file(answer)
--do return end
if answer == nil then 
	   m_simpleTV.OSD.ShowMessage("tuthd.tv script error - can't find stream",255,10)
	   return
 end

 require('jsdecode')
 local retAdr = jsdecode.DoDecode(answer)

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

m_simpleTV.Control.CurrentAdress = retAdr

