--script for thevideo.me (04/12/2015)
--open link http://thevideo.me/nv4prd5bn834

 --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, 'thevideo%.me' )   and not string.match( inAdr, 'thevideo%.me/embed' )  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,3000,0)
  --INTERNET_OPTION_RECEIVE_TIMEOUT
  m_simpleTV.WinInet.SetOptionInt(session,6,5000,0)
-------------------------------------------
if string.match( inAdr, 'embed' )   then 
 local vid =  findpattern( inAdr,'%-(.-)%-',1,1,1)
 if vid == nil then return end
 inAdr = 'http://thevideo.me/' .. vid
end 

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

 if rc~=200 then
	   m_simpleTV.WinInet.Close(session)
	   m_simpleTV.OSD.ShowMessage("thevideo.me connection error1 - " .. rc ,255,10)
	   return
 end

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=header end

  local tmp = findpattern (answer,'<form id="veriform"(.-)<footer',1,0,0)
	if tmp == nil then 
	  m_simpleTV.OSD.ShowMessage("thevideo.me - script error ",255,10)
	   return 
	end

--debug_in_file(answer .. '\n\n\n')
local _vhash,gfk,id,fname,hash

_vhash = findpattern (tmp,"_vhash', value: '(.-)'",1,17,1)
gfk = findpattern (tmp,"gfk', value: '(.-)'",1,14,1)
id =  findpattern(tmp,'"id" value="(.-)"',1,12,1)
fname =  findpattern(tmp,'"fname" value="(.-)"',1,15,1)
hash =  findpattern(tmp,'"hash" value="(.-)"',1,14,1)
if _vhash == nil then return end
fname = string.gsub(fname,'%s','+')

local body = '_vhash=' .. _vhash .. '&gfk=' .. gfk .. '&op=download1&usr_login=&id=' .. id .. '&fname=' .. fname .. '&referer=&hash=' .. hash .. '&inhu=foff&imhuman=Proceed+to+video'

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)
 m_simpleTV.WinInet.Close(session)
 if rc~=200 then
	   m_simpleTV.WinInet.Close(session)
	  m_simpleTV.OSD.ShowMessage("thevideo.me connection error2 - " .. rc ,255,10)
	   return
 end

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

local tmp = findpattern( answer,'sources:(.-)}],',1,8,1)
--debug_in_file(tmp .. "\n\n")
if tmp == nil then m_simpleTV.OSD.ShowMessage("thevideo.me - video not found "  ,255,10) return end 

tmp = string.gsub(tmp,"'",'"')
tmp = string.gsub(tmp,"label",'"label"')
tmp = string.gsub(tmp,"file",'"file"')

require('json')

local t = json.decode(tmp)
if t == nil then m_simpleTV.OSD.ShowMessage("thevideo.me json decode error "  ,255,10) return end

     local tt = {}
     local i=1
     while true do
              if t[i] == nil then break end
             
              tt[i] = {}
              tt[i].Id   =  i
              tt[i].Name =  t[i].label
              tt[i].Adress = t[i].file
           
           -- debug_in_file(tt[i].Name .. ' ' .. tt[i].Adress  .. "\n\n")
              i=i+1
  
     end
tt = table_reverse(tt)
m_simpleTV.Control.CurrentAdress = tt[1].Adress 
