--script for cxz.to(12/10/2015)
-- open link 'Список файлов' http://cxz.to/flist/if6cfuGPnP8RLfI9J8p5HW?folder=224176 

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://cxz%.to/flist/' ) and not string.match( inAdr, '^http://cxz%.to%./flist/' ) then return end

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

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

---------------------------------------------------------------------------
--[[
function url_decode(str)
  str = string.gsub (str, "+", " ")
  str = string.gsub (str, "%%(%x%x)",
      function(h) return string.char(tonumber(h,16)) end)
  str = string.gsub (str, "\r\n", "\n")
  return str
end
]]

local function CheckHostname(adr)

if string.match(adr, 'http://(.-)/' ) ~= hostname then adr = string.gsub(adr,'http://(.-)/','http://' .. hostname .. '/') end

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

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

 for w in string.gmatch(answer,'http://(.-)\n') do
	name = string.match (w,'.+/(.-)$')
	if name == nil then name ='Серия ' .. i end
	name = url_decode(name)
	name = m_simpleTV.Common.string_fromUTF8(name)
     t[i] = {}
     t[i].Id   =  i
     t[i].Name = name
     t[i].Adress  = CheckHostname('http://' .. w)


--debug_in_file(t[i].Name .. '  ' .. t[i].Adress .. '\n\n')
i=i+1
 end	

 if i>2 then
    m_simpleTV.OSD.ShowSelect('Выбор серии',0,t,5000)
 end	

if i>1 then
     retAdr = t[1].Adress
	 if retAdr == nil then
		  m_simpleTV.OSD.ShowMessage("Ошибка - видео не доступно",255,10)
		  retAdr=''
	  end
end

m_simpleTV.WinInet.Close(session)
m_simpleTV.Control.CurrentAdress = retAdr


