--script for stream-oase.tv (12/03/2013)

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, 'oase%.tv' ) then return end

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

local function GetAddress(session,inAdr)
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 = findpattern( answer,"file: '(.-)'",1,7,1)

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

return retAdr

end

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

 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,5000,0)
  --INTERNET_OPTION_RECEIVE_TIMEOUT
  m_simpleTV.WinInet.SetOptionInt(session,6,8000,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

 answer = m_simpleTV.Common.string_fromUTF8(answer)

local header =  findpattern( answer,'<h2>(.-)</h2>',1,4,5)
if header==nil then header=inAdr end

 header = string.gsub(header,'&quot;','"')
if m_simpleTV.Control.CurrentTitle ~= nil then m_simpleTV.Control.CurrentTitle=header end

local str = findpattern( answer,'avs_player(.-)</p>',1,0,0)
if str == nil then return end
str = string.lower(str)
--local adr = findpattern( str,'"><IFRAME SRC="(.-)"',1,15,1)

local i=1
local t={}
local name,adr,stream
local retAdr=''
for w in string.gmatch(str,'iframe(.-)/iframe') do

	name = 'Stream ' .. i 
	stream = findpattern (w,'src="(.-)"',1,5,1)

    	if stream ~= nil then

		if string.match(stream,'mightyupload') then adr = GetAddress(session,stream) end

	end

     t[i] = {}
     t[i].Id   =  i
     t[i].Name = name
     t[i].Adress  = adr
--debug_in_file(t[i].Adress .. '\n')
if retAdr == '' then retAdr = t[i].Adress end
     i=i+1

 end

 if i>2 then
    m_simpleTV.OSD.ShowSelect(header,0,t)
 end


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


