--script for tv.vnzee.com (22/03/2015)

require ('jsdecode')

local js = m_simpleTV.MainScriptDir .. 'user\\tvplaylists\\viet\\ztv.js'
local fhandle = io.open (js , "r")
if fhandle == nil then
	   m_simpleTV.OSD.ShowMessage("zTV script error - can't open ztv.js",255,10)
	   return
 end

local scr = fhandle:read("*a")
fhandle:close()

local filepath  = m_simpleTV.Common.GetMainPath (0)

local m3ustr = '#EXTM3U\n'

 local  tmpName = m_simpleTV.Common.GetTmpName()
 if tmpName==nil then 
		return 
 end 

 local tfile = io.open(tmpName,'w+')
 if tfile==nil then 
	os.remove(tmpName)
	return
 end

 tfile:write(m3ustr)
 tfile:close() 

---------------------------------------------------------------------------
 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,8000,0)
  --INTERNET_OPTION_RECEIVE_TIMEOUT
  m_simpleTV.WinInet.SetOptionInt(session,6,8000,0)
---------------------------------------------------------------------------
function trim(str)
str = string.match(str,'^%s*(.-)%s*$')
  return str
end
---------------------------------------------------------------------------

local rc,answer=m_simpleTV.WinInet.Get(session,'http://tv.vnzee.com')

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

answer = findpattern(answer,'<div id="lst">(.-)<h1>',1,0,0)
if answer == nil then
	   m_simpleTV.OSD.ShowMessage("zTV script error",255,10)
	   return
 end

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


m_simpleTV.OSD.ShowMessage("zTV - start loading playlist\nWait until loading complete" ,0xFF00,15)

local i=1
local str,name,adr,e,c,prescr,img,imgname

 for w in string.gmatch(answer,'<li(.-)</li>') do

	c = findpattern(w,'data%-c="(.-)"',1,8,1)
	name = findpattern(w,'name="(.-)"',1,6,1)
	img = findpattern(w,'url%((.-)%)',1,4,1)

        if c == nil or name == nil or img == nil then break end
        
        img = 'http://tv.vnzee.com' .. img
        imgname = string.gsub(img,'http://tv.vnzee.com/img/','')

       e = 'var e="' .. name .. '";'
       prescr = e .. scr
       str = jsdecode.DoDecode('remove_unicode(e)', false, prescr, 0, 0)
       str = string.gsub(str,'%s+','-')
       str = string.gsub(str,'&','and')
       str = string.gsub(str,'[%[%]]','')

       adr =  'http://tv.vnzee.com/livetv~' .. str .. '~' .. c .. '~ztv.html'   
--debug_in_file(adr .. '\n\n')

       rc = m_simpleTV.WinInet.GetFile(session, img ,nil, filepath .. 'zTV' .. imgname) 
       if rc == nil then 
  	   m_simpleTV.OSD.ShowMessage("zTV error - can't save logo " .. name ,255,3)
       end
       m_simpleTV.Common.Wait(100)

       --m3ustr = '#EXTINF:-1,zTV:' .. trim(name) .. '\n' .. trim(adr) .. '\n'
       m3ustr = '#EXTINF:-1 group-title="zTV" tvg-logo="..\\Channel\\logo\\zTV' .. imgname .. '",' .. name .. '\n' .. adr .. '\n'
       tfile = io.open(tmpName,'a+')
       tfile:write(m3ustr)
       tfile:close()       
   i=i+1
 end	
m_simpleTV.WinInet.Close(session)

--do return end

  --опции  для загрузки плейлиста 
local p={}
p.TypeSourse = 1
p.DeleteBeforeLoad = 0
p.TypeSkip   = 1
p.TypeFind =   0
p.AutoSearch = 1
p.NumberM3U =  1
p.Find_Group = 1
p.TypeCoding = 1  --  -1-auto  0 - plane text  1- UTF8  2- unicode
p.BorpasFileFormat=1
p.AutoNumber = 0
p.ExtFilter = 'VietTV'
p.ExtFilterLogo =  '..\\Channel\\logo\\extFiltersLogo\\viet.png'   -- the logo of extended filter (from ver. 0.4.8 b9)
p.ExtFilterLogoForce = 0

local err,add,ref,names = m_simpleTV.Common.LoadPlayList_UTF8 (tmpName,p,0,true,false)
os.remove(tmpName)

if err==true then
     local mess = "zTV - playlist loading complete (New channels: " .. add .. ")"	 
	 if add > 0 and add < 25 then 
	    names = string.gsub(names,'%$end','\n')
		mess = mess .. '\n' .. names
	 end
	 
	 m_simpleTV.OSD.ShowMessage(mess,0x37BEDF,3) 
  end
