--script for openload.co (03/12/2015)
--open links https://openload.co/f/pKjloo_sVy4/ / https://openload.co/embed/pKjloo_sVy4/

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, 'openload%.co/f' ) and not string.match( inAdr, 'openload%.co/embed/' ) then return end

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

if string.match( inAdr, 'openload%.co/embed/' ) then 
   inAdr = string.gsub( inAdr, 'embed', 'f')
end

local num = findpattern( inAdr,'f/(.-)/',1,2,1)
if num == nil then 
   num = findpattern( inAdr,'f/(.-)$',1,2,0)
   if num == nil then return end
end

local url = 'https://openload.co/getdllink/' .. num .. '?_=' ..  os.time() 

 local session = m_simpleTV.WinInet.New("Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0;")
 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)
  --INTERNET_FLAG_SECURE
  m_simpleTV.WinInet.SetOpenRequestFlags(session, 0x00800000 + 0x00002000 + 0x00001000)
------------------------------------------

 local rc,answer=m_simpleTV.WinInet.Get(session,url)
 m_simpleTV.WinInet.Close(session)
 if rc~=200 then
	   m_simpleTV.OSD.ShowMessage("Openload connection error - " .. rc ,255,10)
	   return
 end

--debug_in_file(answer .. '\n')
  local a = 'var decode0;var decode1; var decode2; function $(param0){decode0 = param0;var obb = {attr: function(param1,param2){decode1 = param1;decode2 = param2;}};return obb;};' .. answer
  
  require('jsdecode')	
  a = jsdecode.DoDecode('decode2',false,a,128)
  
  if a~=nil and string.match(a,'http') then
	m_simpleTV.Control.CurrentAdress=a
  end	
