--script for vidzi.tv (04/12/2015)
--open http://vidzi.tv/acqgtm8u3bwf.html
--http://vidzi.tv/embed-acqgtm8u3bwf-640x360.html


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://vidzi%.tv/' ) and not string.match( inAdr, '^http://vidzi%.tv/embed' ) then return end

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

---------------------------------------------------------------------------
if string.match( inAdr, 'embed' )   then 
 local vid =  findpattern( inAdr,'%-(.-)%-',1,1,1)
 if vid == nil then return end
 inAdr = 'http://vidzi.tv/' .. vid
end 

 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
 answer = m_simpleTV.Common.string_fromUTF8(answer)

local header =  findpattern( answer,'<Title>(.-)</Title>',1,7,8)
if header==nil then header=inAdr end
if m_simpleTV.Control.CurrentTitle ~= nil then m_simpleTV.Control.CurrentTitle=header end

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

local tmps = "<script type='text/javascript'>eval(.-)</script>"
  
 answer = findpattern(answer,tmps,1,35,9)
 if answer==nil then return nil end
 answer = string.gsub(answer,'eval','')

 require('jsdecode')

 answer = jsdecode.DoDecode(answer)
 if answer==nil then return nil end
 --debug_in_file(answer .. '\n\n')

local retAdr = findpattern (answer,'file:"(.-)"',1,6,1)
    if retAdr==nil then 
       m_simpleTV.OSD.ShowMessage("vidzi - can't find stream",255,10) 
       return 
    end


m_simpleTV.Control.CurrentAdress = retAdr


