--script for http://promodj.com/tv (20/01/2015)
-- open promodjtv

if not string.match(m_simpleTV.Control.CurrentAdress, 'promodjtv') then return end

m_simpleTV.Control.ChangeAdress='No'
---------------------------------------------------------------------------

 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 function GetPDJAddress(retAdr,header)
  
local str = 'http://promodj.com/tv/config.html?rnd=' .. math.random()

local  rc,answer=m_simpleTV.WinInet.Get(session,str)
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
if string.match (answer,'error') then
  local retAdr = 'rtmp://rtmp.cdn.promodj.com/live/pdjlive'
    return retAdr
end
answer = m_simpleTV.Common.string_fromUTF8(answer)

answer = string.gsub( answer, '\\', '')
--debug_in_file(answer .. '\n\n')

local header =  findpattern( answer,'"title":(.-)"@url',1,0,0)
if header==nil then header=inAdr end
header =  findpattern( header,'"%$":"(.-)"',1,5,1)
if header==nil then header=inAdr end
--debug_in_file(header .. '\n\n')
local retAdr  = findpattern(answer,'"@url":"(.-)%?',1,8,1)
--debug_in_file(retAdr .. '\n\n')
if retAdr == nil then return end
  return retAdr, header
end


   m_simpleTV.Common.Wait(5000)
  local retAdr,header = GetPDJAddress(retAdr,header)
  if retAdr == nil then return end
  --if m_simpleTV.Control.CurrentTitle ~= nil then m_simpleTV.Control.CurrentTitle=header end
  m_simpleTV.Control.ChangeAdress='Yes'
  m_simpleTV.Control.CurrentAdress = retAdr


