--script for spbtv.com (27/01/2015)
--http://tv3.spr.spbtv.com/v1/channels/rumusic/stream?protocol=

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, 'spbtv%.com/channels' ) then return end

--if  not string.match( inAdr, 'tv3%.spr%.spbtv%.com' ) then return end



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

---------------------------------------------------------------------------

local inAdr1 = inAdr

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 rc,answer=m_simpleTV.WinInet.Get(session,inAdr1)

 if rc~=200 then
	   m_simpleTV.WinInet.Close(session)
	   m_simpleTV.OSD.ShowMessage("Connection error - " .. rc ,255,10)
	   return
 end

local chanid = findpattern( answer,"channelId%: %'(.-)%'",1,12,1)

local inAdr2 = 'http://tv3.spr.spbtv.com/v1/channels/' .. chanid .. '/stream?protocol=hds'


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 rc,answer=m_simpleTV.WinInet.Get(session,inAdr2)

 if rc~=200 then
	   m_simpleTV.WinInet.Close(session)
	   m_simpleTV.OSD.ShowMessage("Connection error - channel is not available " .. rc ,255,10)
	   return
 end

--answer = m_simpleTV.Common.string_toUTF8(answer)

local channel = findpattern( answer,'http%://(.-)f4m',1,7,4)

local retAdr = 'http://' .. channel .. '.m3u8'

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

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

