--script for filmsenzalimiti.net (ITA) (12/12/2013)
--open link like http://www.filmsenzalimiti.net/wolverine-limmortale.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://www%.filmsenzalimiti.net' ) 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,5000,0)
  --INTERNET_OPTION_RECEIVE_TIMEOUT
  m_simpleTV.WinInet.SetOptionInt(session,6,8000,0)

 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

 answer = m_simpleTV.Common.string_fromUTF8(answer)

local header =  findpattern( answer,'<title>(.-)</title>',1,7,8)
if header==nil then header=inAdr end

 header = string.gsub(header,'&quot;','"')
if m_simpleTV.Control.CurrentTitle ~= nil then m_simpleTV.Control.CurrentTitle=header end

answer = string.lower(answer)
local adr = findpattern(answer,'<iframe src="(.-)"',1,13,1)

if answer ~= nil then
	  rc,answer=m_simpleTV.WinInet.Get(session,adr)
 
 if rc~=200 then
	   m_simpleTV.WinInet.Close(session)
	   m_simpleTV.OSD.ShowMessage("Connection error - " .. rc ,255,10)
	   return
 end
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','')
    
 local 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("Error - can't find stream" .. rc ,255,10) 
  return 
end

m_simpleTV.WinInet.Close(session)
m_simpleTV.Control.CurrentAdress = retAdr

end
