-- script for Tv-On.pl
local body,login,password,host

--set your login and password 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%://tv%-on%.pl' ) 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)

if login =='' or password=='' then
	m_simpleTV.OSD.ShowMessage("Tv-On.pl - Login and Password is required",255,10)
 return 
end
-----------------------------------------------
local function GetAddressTvOn(session,inAdr)

local sid = findpattern(inAdr,'id(.-)htm',1,3,4)

--debug_in_file(sid.."\n\n")

host = 'http://tv-on.pl/player-ajax.htm'
body = 'streamId=' .. sid 


header = 
'Host: tv-on.pl\n' ..
'Accept-Language: ru-RU,ru;q=0.9,en;q=0.8\n' ..
'Referer: ' .. inAdr ..'\n' ..
'Accept: application/json, text/javascript, */*; q=0.01\n' ..
'X-Requested-With: XMLHttpRequest\n' ..
'Content-Type: application/x-www-form-urlencoded; charset=UTF-8'

local 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")
answer = string.gsub(answer, '\\', '')
local retAdr = findpattern(answer,'serverURL=(.-)&',1,10,1)


 if retAdr==nil then
	m_simpleTV.OSD.ShowMessage("Error Tv-On.pl - video is not available",255,10)
 return 
end


retAdr = 'rtmp://$OPT:rtmp-raw=' .. retAdr .. ' swfUrl=http://tv-on.pl/player/videoPlayer.swf live=1 pageUrl=' .. inAdr
--debug_in_file(retAdr.."\n\n")
return retAdr	
 
end
-----------------------------------------------


host = 'http://tv-on.pl/user,login.htm'
body = 'login=' .. login .. '&password=' .. password

header = 
'Host: tv-on.pl\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\n' ..
'Accept-Language: ru-RU,ru;q=0.9,en;q=0.8\n' ..
'Referer: ' .. host ..'\n' ..
'Content-Type: application/x-www-form-urlencoded'

local 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


local ch = 'http://tv-on.pl/channels.htm'

rc,answer=m_simpleTV.WinInet.Get(session,ch)

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 url,name
local i=1
local t={}
local retAdr 
for w in string.gmatch(answer,'class="channel"(.-)/div>') do
	url = findpattern(w,'rel="(.-)"',1,5,1)
	name = findpattern(w,'name(.-)<',1,6,1)
	if url == nil or name == nil then return end
	url = 'http://tv-on.pl/' .. url
	url = GetAddressTvOn(session,url)
	t[i] = {}
	t[i].Id   =  i
	t[i].Name = name
	t[i].Adress  = url
--debug_in_file(t[i].Name .. '  ' .. t[i].Adress.. "\n\n")
	retAdr = t[i].Adress
i=i+1
end
	if i>2 then
   		m_simpleTV.OSD.ShowSelect('Choose a channel',0,t)
	
 	end

if retAdr == nil then retAdr = '' end

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