--script for movie-tube.co (03/12/2015)
-- http://www.movie-tube.co/981-brand-a-second-coming.html
--required openload.lua / videomegatv.lua

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, 'movie%-tube%.co/%d' ) then return end

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

function unescape_html(str)
  str = string.gsub( str, '&raquo;', '»' )
  str = string.gsub( str, '&lt;', '<' )
  str = string.gsub( str, '&gt;', '>' )
  str = string.gsub( str, '&quot;', '"' )
  str = string.gsub( str, '&apos;', "'" )
  str = string.gsub( str, '&#(%d+);', function(n) return string.char(n) end )
  str = string.gsub( str, '&#x(%d+);', function(n) return string.char(tonumber(n,16)) end )
  str = string.gsub( str, '&amp;', '&' ) -- Be sure to do this after all others
  return str
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,8000,0)
  --INTERNET_OPTION_RECEIVE_TIMEOUT
  m_simpleTV.WinInet.SetOptionInt(session,6,8000,0)
  --INTERNET_FLAG_SECURE
 -- m_simpleTV.WinInet.SetOpenRequestFlags(session, 0x00800000 + 0x00002000 + 0x00001000)
---------------------------------------------------------------------------------------

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

 if rc~=200 then
	   m_simpleTV.WinInet.Close(session)
	   m_simpleTV.OSD.ShowMessage("movie-tube.co connection error - " .. rc ,255,10)
	   return
 end

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

local header =  findpattern( answer,'<title>(.-)</title>',1,7,8)
if header==nil then header=inAdr end
if m_simpleTV.Control.CurrentTitle ~= nil then m_simpleTV.Control.CurrentTitle=unescape_html(header) end

local tmp = findpattern(answer,'fstory%-video(.-)/div',1,0,0)
if tmp == nil then 
	   m_simpleTV.OSD.ShowMessage("movie-tube.co script error"  ,255,5)
   return
end

if string.match(tmp,'openload') then
   local adr = findpattern( tmp,'<iframe src="(.-)"',1,13,1)
   if adr == nil then
   	   m_simpleTV.OSD.ShowMessage("movie-tube.co - openload address not found"  ,255,5)
       return
   end
--debug_in_file(adr .. '\n\n')
   m_simpleTV.Control.ChangeAdress='No'
   m_simpleTV.Control.CurrentAdress = adr
   dofile(m_simpleTV.MainScriptDir .. "user\\video\\openload.lua") 

 elseif string.match(tmp,'videomega') then
   local adr = findpattern( tmp,'src="(.-)"',1,5,1)
   if adr == nil then
   	   m_simpleTV.OSD.ShowMessage("movie-tube.co - videomega address not found"  ,255,5)
       return
   end
--debug_in_file(adr .. '\n\n')
   m_simpleTV.Control.ChangeAdress='No'
   m_simpleTV.Control.CurrentAdress = adr
   dofile(m_simpleTV.MainScriptDir .. "user\\video\\videomegatv.lua") 

end
