-- script for Wlacz.tv

local body,login,password,host

--set your login and passwor here
login = 'test1234'
password = 'test1234'
----------------------------------

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%://www%.polandlive%.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; 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)

  
  
host = 'http://www.polandlive.tv/user/login'
body = 'email=' .. login .. '&password=' .. password .. '&submit=%D0%9E%D1%82%D0%BF%D1%80%D0%B0%D0%B2%D0%B8%D1%82%D1%8C'

  local rc,answer=m_simpleTV.WinInet.Get(session,host) 

header = 
'Host: www.polandlive.tv\n' ..
'Accept: text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/webp, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1' ..
'Accept-Language: ru-RU,ru;q=0.9,en;q=0.8\n' ..
'Referer: ' .. host ..'\n' ..
'Content-Type: application/x-www-form-urlencoded'

 rc,answer=m_simpleTV.WinInet.Post(session,host,nil,header,body)

 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")

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 = ''
local Token  =  findpattern( answer,'wlacztv_session_token=(.-)&',1,22,1)
local Stream  =  findpattern( answer,'balancer(.-)stream',1,9,7)
	if Token==nil then return end 

	if Stream==nil then 
		Stream  =  findpattern( answer,'flashvars(.-)</param>',1,0,0)
		Stream  =  findpattern( Stream,'redirect(.-)%?',1,9,1)
		retAdr = 'rtmp://eu.001.wlacz.tv:1935/wlacztv?wlacztv_session_token=' .. Token .. '/' .. Stream .. '?wlacztv_session_token=' .. Token

		if Stream==nil then return end		
	else
		retAdr = 'rtmp://eu.001.wlacz.tv:1935/live?wlacztv_session_token=' .. Token .. '/' .. Stream .. '.stream?wlacztv_session_token=' .. Token
	end

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


