--script for my-entertainment.biz (08/03/2013)

require('streamcloudcore')
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://my%-entertainment%.biz' ) then return end

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

---------------------------------------------------------------------------
local function GetFileAddressMyEnter(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
--debug_in_file(answer .. "\n\n")

local retAdr = findpattern(answer,'file=(.-)&&',1,5,2)

if retAdr == nil then return end
retAdr = unescape(retAdr)

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,3000,0)
  --INTERNET_OPTION_RECEIVE_TIMEOUT
  m_simpleTV.WinInet.SetOptionInt(session,6,5000,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,'<title>(.-)</title>',1,7,8)
if header==nil then header=inAdr end

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

local i=1
local t={}
local mUrl,name
local retAdr = ''

 mUrl = findpattern(answer,'<div class="bbcode_quote">(.-)target="Videoframe',1,0,0)

 if mUrl == nil then 

local str = findpattern(answer,'<div class="spoiler"(.-)<div id="comments"',1,0,0)
--debug_in_file(str)

	if str~=nil then

		 local adr,name
		 local i=1
		 local t={}

	 	 for w in string.gmatch(str,'<a href=(.-)/a>') do

			adr = findpattern(w,'%b""',1,1,1)
			name = findpattern(w,'%b><',1,1,1)
			if adr == nil or name == nil then break end

			t[i]={}
			t[i].Id   =  i
			t[i].Name =  i .. '. ' .. name
			t[i].Adress  = adr
--debug_in_file(t[i].Name .. '   ' .. t[i].Adress .. '\n')
			i=i+1
	  	end
  		  if i>2 then
			m_simpleTV.OSD.ShowSelect(header,0,t)
  		  end

 if i >1 then
      retAdr = streamcloudcore.StreamcloudGetAddress(t[1].Adress)
	 if retAdr == nil then
		  m_simpleTV.OSD.ShowMessage("Error - video is not available for playback",255,10)
		  retAdr=''
	  end
  end


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

if mUrl ~= nil then 
 for w in string.gmatch(answer,'bbcode_quote(.-)Videoframe') do

	mUrl = findpattern(w,'href="(.-)"',1,6,1)
	if mUrl == nil then break end
	name = 'Film ' .. i

     t[i] = {}
     t[i].Id   =  i
     t[i].Name = name
     t[i].Adress  = GetFileAddressMyEnter(session,mUrl)
--debug_in_file(t[i].Name .. '\n' .. t[i].Adress .. '\n\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

end
