--script for http://spbtv.com (17/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, '^spbfreetv=' ) then return end

m_simpleTV.Control.ChangeAdress='Yes'
m_simpleTV.Control.CurrentAdress = ''
----------------------------------------------------------------
local session = m_simpleTV.WinInet.New("Mozilla/5.0 (Linux; U; Android 4.0.4) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Safari/534.30")
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 id = string.match(inAdr, '^spbfreetv=ch_(.+)')
 if id == nil then return end
-- debug_in_file(id .. '\n')

 local c = '_spbtv_auth=eyJzY2hlbWEiOiJhbm9ueW1vdXMiLCJkZXZpY2VfaWQiOjM3NjU0NywiY3VzdG9tZXJfaWQiOjM3NTk0N30%3D--7461c6b4fe9ed8e511fffb1cb28a48ae0846596a'

 local  url = 'http://tv3.spr.spbtv.com/api/v3/channels/' .. id .. '/streams.xml'
 
 m_simpleTV.WinInet.SetCookies(session, url,'',c)
 
 local rc,answer=m_simpleTV.WinInet.Get(session,url)
 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

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

 if string.match(answer,'error')  then
      m_simpleTV.OSD.ShowMessage("spbtv - Sorry, this channel is not available in your country" ,255,10)
    return
 end

 local tmp =  string.match(answer,'<stream protocol="http_live"(.-)%?')
 if tmp == nil then	
     m_simpleTV.OSD.ShowMessage("spbtv error - can't find stream" ,255,10)
      return
  end

  local retAdr =  findpattern(tmp,'href="(.-)m3u8',1,6,0)
  if retAdr == nil then return end

  retAdr = retAdr:gsub('lq.m3u8','hq.m3u8')

m_simpleTV.Control.CurrentAdress = retAdr ..'$OPT:http-user-agent=Mozilla/5.0 (Linux; U; Android 4.0.4) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Safari/534.30'

