--get address
m_simpleTV.Control.EventPlayingInterval=1000 

require('json')

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%://101%.ru/%?an=port_channel' ) and not string.match( inAdr, '^http%://101%.ru/%?an=personal' ) and not string.match( inAdr, '^http://listen%.di%.fm/' ) and not string.match( inAdr, '^http://listen%.radiotunes%.com/' ) and not string.match( inAdr, '^http://listen%.jazzradio%.com/' ) and not string.match( inAdr, '^http://listen%.rockradio%.com/webplayer' ) 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,3000,0)
 --INTERNET_OPTION_RECEIVE_TIMEOUT
 m_simpleTV.WinInet.SetOptionInt(session,6,6000,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
local retAdr = ''

--101 radio
if  string.match( inAdr, '^http%://101%.ru/%?an=port_channel' ) or string.match( inAdr, '^http%://101%.ru/%?an=personal' ) then

local param  = findpattern( answer,'playerAPI.Uppod.AddParamPlayer(.-)uppod',1,0,0)
--debug_in_file(param .. '\n\n')

if param~=nil then 
	local pl = findpattern( param,"'pl':'(.-),",1,6,2)
		if pl ~= nil then 
			pl = string.gsub(pl,'|','&')
			pl = 'http://101.ru' .. pl .. '&rand=' .. math.random()
			--debug_in_file(pl .. '\n\n')
		  else
			pl = findpattern( param,"file':'(.-),",1,7,2)
			--debug_in_file(pl .. '\n\n')
				if string.match(pl, 'main') then
					pl = findpattern( pl,"(.-)main",1,0,0)
					pl = string.gsub(pl,'///','/')
					--debug_in_file(pl .. '\n\n')
				end
			m_simpleTV.Control.CurrentAdress = pl
			m_simpleTV.WinInet.Close(session)
			return
		end
 rc,answer=m_simpleTV.WinInet.Get(session,pl)

 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\n')
 local t = json.decode(answer) 

 if t == nil or t.playlist==nil then
    m_simpleTV.OSD.ShowMessage("Error - can't find address",255,5)
   return
 end 

local a={}
local i=1

while true do
  if t.playlist[i] == nil then break end
  local adr = t.playlist[i].file
  adr = string.gsub(adr,'setst=(%d+)&','setst=-1&')
  a[i] = {}
  a[i].Id   =  i
  a[i].Name =  t.playlist[i].comment
  a[i].Adress  = adr
  if retAdr == '' then retAdr = a[i].Adress end
--debug_in_file(retAdr .. '\n\n')
  i = i+1

end

 if i>2 then
     m_simpleTV.OSD.ShowSelect('Сервер',0,a,5000,32)
 end 

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

end
--http://101.ru/play.m3u?uid=1&bit=2&serv=332&tok=06310483lrwChN8DGbCV5E+L7P1RgA==2&setst=045910500138656707620131210&rand=0.6893949499353766

--DI radio
if string.match( inAdr, '^http://listen%.di%.fm/' ) or string.match( inAdr, '^http://listen%.radiotunes%.com/' ) or string.match( inAdr, '^http://listen%.jazzradio%.com/' ) or string.match( inAdr, '^http://listen%.rockradio%.com/webplayer' ) then

answer = string.gsub(answer, '\\','')
answer = string.gsub(answer, '_aac%.flv','')
answer = string.gsub(answer, '_aacplus%.flv','')
local t = json.decode(answer)

if t == nil then
   m_simpleTV.OSD.ShowMessage("Error - stream not found"  ,255,10)
   return
end

local tab={}
local i=1
local retAdr 

while true do
  if t[i] == nil then break end
  
  tab[i] = {}
  tab[i].Id   =  i
  tab[i].Name =  'Server ' .. i
  tab[i].Adress  = t[i]
--debug_in_file(tab[i].Name .. '  ' .. tab[i].Adress .. '\n')
  i = i+1

end

 if i>2 then
     m_simpleTV.OSD.ShowSelect('Available Servers',0,tab,5000,32)
 end 
if i>1 then
	retAdr = tab[1].Adress
end
if retAdr == nil then return end
--debug_in_file(retAdr)
m_simpleTV.WinInet.Close(session)
m_simpleTV.Control.CurrentAdress = retAdr

end 

