--script for movdivx.com (04/12/2015)
--open link http://movdivx.com/fm3vf5vpkb0t/Rawhide_s_1_e_1_p1-1.flv.html

 require('jsdecode')

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://movdivx%.com' ) and  not string.match( inAdr, '^http://www%.movdivx%.com' )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,10000,0)
  --INTERNET_OPTION_RECEIVE_TIMEOUT
  m_simpleTV.WinInet.SetOptionInt(session,6,10000,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
--debug_in_file(answer .. "\n\n")

  local tmp = findpattern (answer,'<Form method="POST" (.-)</div>',1,0,0)
	if tmp == nil then 
	  m_simpleTV.OSD.ShowMessage("movdivx script error - file not found",255,10)
	   return 
	end
local id,fname

id =  findpattern(tmp,'"id" value="(.-)"',1,12,1)
fname =  findpattern(tmp,'"fname" value="(.-)"',1,15,1)
if id == nil then return end
fname = string.gsub(fname,'%s','+')
	
local body = 'op=download1&id=' .. id ..'&rand=&referer=&usr_login=&fname=' ..fname ..'&method_free=Continue+to+Stream'
local head = 
'Accept-Language: ru-RU,ru;q=0.9,en;q=0.8\n' ..
'Referer: ' ..  inAdr .. '\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' ..
'Content-Type: application/x-www-form-urlencoded'

--m_simpleTV.Common.Wait(5000)
 rc,answer=m_simpleTV.WinInet.Post(session,inAdr,nil,head,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")
local header =  findpattern( answer,'<span class="dfilename"(.-)<',1,24,1)
if header==nil then header=inAdr end
if m_simpleTV.Control.CurrentTitle ~= nil then m_simpleTV.Control.CurrentTitle=header end

tmp = findpattern (answer,'<div class="player"(.-)</div>',1,4,1)
if tmp == nil then m_simpleTV.OSD.ShowMessage("movdivx - script error ",255,10) return end

local tmps = "<script type='text/javascript'>eval(.-)</script>"
  
 answer = findpattern(tmp,tmps,1,35,9)
 if answer==nil then return nil end
 answer = string.gsub(answer,'eval','')
  
 local answer = jsdecode.DoDecode(answer)
 if answer==nil then return nil end
--debug_in_file(answer .. '\n\n')

local retAdr=findpattern(answer,'file:"(.-)"',1,6,1)

if retAdr==nil then 
   retAdr=findpattern(answer,'divx"src="(.-)"',1,10,1)
    if retAdr==nil then 
       m_simpleTV.OSD.ShowMessage("movdivx - can't find stream",255,10) 
       return 
    end
end

m_simpleTV.Control.CurrentAdress = retAdr

