--script for cxz.to (23/04/2015)
--open link http://cxz.to/films/i49zuQ8lBr5E3hLHZgXCM8w-oblivion.html

--Use SergeyVS script cxzto.lua 1-yes, 0-no
local use_SergeyVS_script=0
-----------------------------

if use_SergeyVS_script~=0 then dofile(m_simpleTV.MainScriptDir .. "user\\video\\cxzto.lua") return end

if m_simpleTV.Control.ChangeAdress ~= 'No' then return end

local inAdr =  m_simpleTV.Control.CurrentAdress

if inAdr==nil then return end

if  string.match( inAdr, '^http://cxz%.to/flist/' ) or string.match( inAdr, '^http://cxz%.to%./flist/' ) then 
        dofile(m_simpleTV.MainScriptDir .. "user\\video\\cxztoAlt.lua") 
    return
end

if  not string.match( inAdr, '^http://cxz%.to/' ) and not string.match( inAdr, '^http://fs%.to/' ) and not string.match( inAdr, '^http://brb%.to/' ) and not string.match( inAdr, '^http://cxz%.to%.' )then return end
if  string.match( inAdr, 'o/get/' ) or  string.match( inAdr, '/get/' ) then return end

local hostname = string.match( inAdr, 'http://(.-)/' )
if hostname == nil then hostname = 'cxz.to' end

inAdr = string.gsub( inAdr, '%#.+','')
inAdr = string.gsub( inAdr, '%#','')
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)

 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,'&raquo;',"»")

if m_simpleTV.Control.CurrentTitle ~= nil then m_simpleTV.Control.CurrentTitle=header end

local id = findpattern( inAdr,'%.to/(.-)-',1,4,1)

if id == nil then 
   id = findpattern( inAdr,'%.to%./(.-)-',1,5,1)
   if id == nil then return end
end
---------------------------------------
function trim(s)
  return (s:gsub("^%s*(.-)%s*$", "%1"))
end

local folder=0
local link = inAdr .. '?ajax&r=0.20429846600290447&id=' .. id .. '&download=1&view=1&view_embed=0&blocked=0&folder_quality=null&folder_lang=null&folder_translate=null&folder=' .. folder ..'&_=1411929432406'

  rc,answer=m_simpleTV.WinInet.Get(session,link)

 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)
--debug_in_file(answer .. '\n\n\n')

 if string.match(answer, 'заблокированы') then m_simpleTV.OSD.ShowMessage("Ошибка - файлы временно заблокированы",255,10) return end

-- serials
 if  string.match( inAdr, '%.to/serials/' ) or string.match( inAdr, '%.to/cartoonserials/' ) or string.match( inAdr, '%.to/tvshow/' )   or string.match( inAdr, '%.to/video/serials/' ) or string.match( inAdr, '%.to/video/cartoonserials/' ) or string.match( inAdr, '%.to/video/tvshow/' ) or string.match( inAdr, '%.to%./serials/' ) or string.match( inAdr, '%.to%./cartoonserials/' ) or string.match( inAdr, '%.to%./tvshow/' )   or string.match( inAdr, '%.to%./video/serials/' ) or string.match( inAdr, '%.to%./video/cartoonserials/' ) or string.match( inAdr, '%.to%./video/tvshow/' ) then 
 local i=1
 local t={}
 local retAdr,name
 for w in string.gmatch(answer,'<div class="header">(.-)</li>') do
	folder = findpattern (w,'parent_id:(.-)"',1,11,2)
	name = findpattern (w,'<b>(.-)<',1,3,1)
	if folder == nil or name == nil then break end
	link = inAdr .. '?ajax&r=0.20429846600290447&id=' .. id .. '&download=1&view=1&view_embed=0&blocked=0&folder_quality=null&folder_lang=null&folder_translate=null&folder=' .. folder ..'&_=1411929432406'
     t[i] = {}
     t[i].Id   =  i
     t[i].Name =  name 
     t[i].Adress  = link

--debug_in_file(t[i].Name .. '  ' .. t[i].Adress .. '\n\n')
i=i+1
 end
if i>1 then
    local ret,id = m_simpleTV.OSD.ShowSelect('Выбор сезона',0,t,10000,1+4+8)
	    if ret==1 then
                   rc,answer=m_simpleTV.WinInet.Get(session,t[id].Adress)

                       if rc~=200 then
                      	   m_simpleTV.WinInet.Close(session)
                      	   m_simpleTV.OSD.ShowMessage("Connection error - " .. rc ,255,10)
                      	   return
                       end
            end
 answer = m_simpleTV.Common.string_fromUTF8(answer)
--debug_in_file(answer .. '\n\n\n')
 end

 local i=1
 local tt={}
 for w in string.gmatch(answer,'<li class="folder folder%-language ">(.-)<span class="material%-details">') do
	folder = findpattern (w,'parent_id:(.-)"',1,12,3)
	name = findpattern (w,'%}">(.-)<',1,3,1)
	if folder == nil or name == nil then break end
	link = inAdr .. '?ajax&r=0.20429846600290447&id=' .. id .. '&download=1&view=1&view_embed=0&blocked=0&folder_quality=null&folder_lang=null&folder_translate=null&folder=' .. folder ..'&_=1411929432406'
     tt[i] = {}
     tt[i].Id   =  i
     tt[i].Name =  name 
     tt[i].Adress  = link

--debug_in_file(tt[i].Name .. '  ' .. tt[i].Adress .. '\n\n')
     i=i+1
 end
if i>1 then
    local ret,id = m_simpleTV.OSD.ShowSelect('Язык',0,tt,10000,1+4+8)
	    if ret==1 then
                   rc,answer=m_simpleTV.WinInet.Get(session,tt[id].Adress)

                       if rc~=200 then
                      	   m_simpleTV.WinInet.Close(session)
                      	   m_simpleTV.OSD.ShowMessage("Connection error - " .. rc ,255,10)
                      	   return
                       end
            end
 answer = m_simpleTV.Common.string_fromUTF8(answer)
--debug_in_file(answer .. '\n\n\n')
 end

local i=1
local adr,n,q
local ttt={}
 for w in string.gmatch(answer,'<div>(.-)</li>') do
	adr = findpattern (w,'/flist(.-)"',1,0,1)
	--name =findpattern (w,'">(.-)<',1,2,1)
	n,name =string.match (w,'">(.-)<.->(.-)<')
	if adr == nil or n == nil or name == nil then break end
	q = string.match(adr,'quality=(.+)')
	if q == nil then q = '' end
	if string.match(q, '%d+') then q = q ..'p' end

     ttt[i] = {}
     ttt[i].Id   =  i
     ttt[i].Name = n .. trim(name) .. ' ' .. trim(q)
--     ttt[i].Adress  = 'http://cxz.to' .. adr
     ttt[i].Adress  = 'http://' .. hostname .. adr


--debug_in_file(ttt[i].Name .. '  ' .. ttt[i].Adress .. '\n\n')
     i=i+1
 end	
if i>1 then
    local ret,id = m_simpleTV.OSD.ShowSelect('Выбор перевода',0,ttt,10000,1+4+8)
	if ret==1 then
	  retAdr =  ttt[id].Adress
		if retAdr == nil then retAdr='' end
	end	
 end

		if  retAdr~=nil then 
 			 m_simpleTV.Control.ChangeAdress='No'
  			 m_simpleTV.Control.CurrentAdress = retAdr
        		dofile(m_simpleTV.MainScriptDir .. "user\\video\\cxztoAlt.lua") 
		   return
		end

    return
end
--------------------------------------

--movie
local i=1
local t={}
local retAdr,name

 for w in string.gmatch(answer,'<div>(.-)/a>') do
	folder = findpattern (w,'parent_id:(.-)"',1,12,3)
	name = findpattern (w,'">(.-)<',1,2,1)
	if folder == nil or name == nil then break end
	link = inAdr .. '?ajax&r=0.20429846600290447&id=' .. id .. '&download=1&view=1&view_embed=0&blocked=0&folder_quality=null&folder_lang=null&folder_translate=null&folder=' .. folder ..'&_=1411929432406'
     t[i] = {}
     t[i].Id   =  i
     t[i].Name =  name 
     t[i].Adress  = link

--debug_in_file(t[i].Name .. '  ' .. t[i].Adress .. '\n\n')
     i=i+1
 end
if i>1 then
    local ret,id = m_simpleTV.OSD.ShowSelect('Язык',0,t,10000,1+4+8)
	    if ret==1 then
                   rc,answer=m_simpleTV.WinInet.Get(session,t[id].Adress)
                   m_simpleTV.WinInet.Close(session)

                       if rc~=200 then
                      	   m_simpleTV.WinInet.Close(session)
                      	   m_simpleTV.OSD.ShowMessage("Connection error - " .. rc ,255,10)
                      	   return
                       end
            end
 answer = m_simpleTV.Common.string_fromUTF8(answer)
--debug_in_file(answer)

local i=1
local adr,n,q
local tt={}
 for w in string.gmatch(answer,'<div>(.-)</li>') do
	adr = findpattern (w,'/flist(.-)"',1,0,1)
	--name =findpattern (w,'">(.-)<',1,2,1)
	n,name =string.match (w,'">(.-)<.->(.-)<')
	if adr == nil or n == nil or name == nil then break end
	q = string.match(adr,'quality=(.+)')
	if q == nil then q = '' end
	if string.match(q, '%d+') then q = q ..'p' end

     tt[i] = {}
     tt[i].Id   =  i
     tt[i].Name = n .. trim(name) .. ' ' .. trim(q)
--     tt[i].Adress  = 'http://cxz.to' .. adr
     tt[i].Adress  = 'http://' .. hostname .. adr

--debug_in_file(tt[i].Name .. '  ' .. tt[i].Adress .. '\n\n')
     i=i+1
 end	
if i>1 then
    local ret,id = m_simpleTV.OSD.ShowSelect('Выбор перевода',0,tt,10000,1+4+8)
	if ret==1 then
	  retAdr =  tt[id].Adress
		if retAdr == nil then retAdr='' end
	end	
 end
end
		if  retAdr~=nil then 
 			 m_simpleTV.Control.ChangeAdress='No'
  			 m_simpleTV.Control.CurrentAdress = retAdr
        		dofile(m_simpleTV.MainScriptDir .. "user\\video\\cxztoAlt.lua") 
		   return
		end





