--script for oneplay.tv (19/05/2015)

-----------------------------------------------
local function AddItem(t,adr,name)
 
 if (not adr) or (not name) then return end
   
 for i=1,#t do 
   if t[i].address == adr then return end
 end
 
 local i = #t +1
 t[i]={}
 t[i].name= name:gsub("^%s*(.-)%s*$", "%1")
 t[i].address= adr
 
 --debug_in_file('i=' .. i .. ' address=' .. adr .. ',name=' .. name..'\n')   
end
-----------------------------------------------
local function ParcePage(session,adrpage,t,answer)
 
 if answer==nil then
	if not string.match(adrpage,'http') then return end
	local rc
	rc,answer=m_simpleTV.WinInet.Get(session,adrpage)
	if rc~=200 then return end				 
 end
  
 for w in string.gmatch(answer,"<h4(.-)</h4>") do
	local tmp = findpattern( w,'<a(.-)</a>',1,0,0)
	if tmp then
	   local adr = findpattern( tmp,'href=%b""',1,6,1)
	   local name = findpattern( tmp,'>(.-)<',1,1,1)
       AddItem(t,adr,name)
	end
  end
    
 return answer 
end
-----------------------------------------------
local function GetPlstAsTable(t)

 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,5000,0)
  --INTERNET_OPTION_RECEIVE_TIMEOUT
  m_simpleTV.WinInet.SetOptionInt(session,6,8000,0)

  local rc,answer=m_simpleTV.WinInet.Get(session,"http://oneplay.tv/canales-tv/recent/")
  if rc~=200 then
	 m_simpleTV.WinInet.Close(session)			
	 return
  end				
  
  ParcePage(session,'',t,answer)
  
  while true do
    local tmp = findpattern( answer,'class="pagination"(.-)</ul>',1,0,0)
    if tmp==nil then  	
         m_simpleTV.OSD.ShowMessage("oneplay script error ",255,10)
       break 
    end
    
    local stop=false
	local adr=nil
	for w in string.gmatch(tmp,'href=%b""') do    
	  adr = string.gsub(w,'href="','')
	  adr = string.gsub(adr,'"','')
	  if adr==nil or stop then break end
	  if string.match(adr,'#') then stop=true end    
	end
	
	if adr==nil then break end
	answer = ParcePage(session,adr,t)
	if answer==nil then break end    
   end
    
 m_simpleTV.WinInet.Close(session)
 return t
 end
 -----------------------------------------------
 --start
  m_simpleTV.OSD.ShowMessage("OneplayTV - start channels updating" ,0xFF00,10)
 
 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('#EXTM3U\n')
 tfile:close() 

 local t={}
 local m3ustr

 GetPlstAsTable(t)
 
 if #t==0 then return end
 
 for i=1,#t do
   --debug_in_file('name - ' .. t[i].name .. ',adr=' .. t[i].address .. '\n')

  m3ustr = '#EXTINF:-1,OneplayTV:' .. t[i].name ..'\n'.. t[i].address ..'\n'
  tfile = io.open(tmpName,'a+') 
  tfile:write(m3ustr)
  tfile:close()
 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=0
p.AutoNumber = 0
p.ExtFilter = 'Oneplay'
p.ExtFilterLogo =  '..\\Channel\\logo\\extFiltersLogo\\oneplay.png'   -- the logo of extended filter (from ver. 0.4.8 b9)
p.ExtFilterLogoForce = 0

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

if err==true then
     local mess = "OneplayTV - channels updated (" .. add .. ")"
	 if add > 0 and add < 25 then 
	    names = string.gsub(names,'%$end','\n')
		mess = mess .. '\n' .. names
	 end
	 
	 m_simpleTV.OSD.ShowMessage(mess,0xFF00,5)
	 
end

 