--script for divan.tv (30/10/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, '^http://divan%.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; Win64; x64) Presto/2.12.388 Version/12.17")
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,5000,0)
---------------------------------------------------------------

local function GetRetAdr(tmp)
 local retAdr

 if string.match(tmp,'trailer') then
    retAdr = string.gsub(tmp,'trailer','index')
   else
    retAdr = tmp
 end

  return retAdr
end
----------------------------------------------------------------
 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 tmp = findpattern( answer,'file: "(.-)"',1,7,1)
if tmp == nil then 

   local msg = string.match(answer,'<div class="msg"(.-)</div>')
   if msg == nil then
	   m_simpleTV.OSD.ShowMessage("Divan error1" ,255,10)
	   return
    end
     local url = findpattern( msg,'href="(.-)"',1,6,1)
     if url == nil then
	   m_simpleTV.OSD.ShowMessage("Divan error2" ,255,10)
	   return
    end
     url = 'http://divan.tv' .. url
--debug_in_file(url .. '\n')

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

 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 tmp = findpattern( answer,'file: "(.-)"',1,7,1)
if tmp == nil then 
	   m_simpleTV.OSD.ShowMessage("Divan - Sorry, this channel is not available in your country" ,255,3)
	   return
 end

local retAdr = GetRetAdr(tmp)
retAdr = retAdr ..'$OPT:http-user-agent=Opera/9.80 (Windows NT 6.1; Win64; x64) Presto/2.12.388 Version/12.17'
m_simpleTV.Control.CurrentAdress = retAdr
  return
end

local retAdr = GetRetAdr(tmp)
retAdr = retAdr ..'$OPT:http-user-agent=Opera/9.80 (Windows NT 6.1; Win64; x64) Presto/2.12.388 Version/12.17'
m_simpleTV.Control.CurrentAdress = retAdr
