--script for tuthd.tv (03/11/2015)

   local UpdateID='TutHD_TV01'
   local UseLocalLogo = nil

if UseLocalLogo == nil then
   local fhandle = io.open (m_simpleTV.MainScriptDir ..  "user/tvplaylists/tvmenu.ini")   
   if fhandle == nil then 
      UseLocalLogo = '1'
      return
   end
   local str = fhandle:read('*a')
   fhandle:close()

   UseLocalLogo = string.match(str,'UseLocalLogo=(%d);')
   if UseLocalLogo == nil then UseLocalLogo = 0 end
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,5000,0)
  --INTERNET_OPTION_RECEIVE_TIMEOUT
  m_simpleTV.WinInet.SetOptionInt(session,6,5000,0)

---------------------------------------------------------------------------------------
 m_simpleTV.OSD.ShowMessage("tuthd.tv - 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  rc,answer=m_simpleTV.WinInet.Get(session,'http://tuthd.tv.')
m_simpleTV.WinInet.Close(session)

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


answer = findpattern(answer,'<div class="channels">(.-)<div class="footer">',1,0,0)
--debug_in_file (answer .. '\n')

if answer == nil then 
	   m_simpleTV.OSD.ShowMessage("tuthd.tv script error - can't find channels",255,10)
	   return
 end

answer = string.gsub(answer, '<div class="category%-name">', '<end><div class="category%-name">')
answer = string.gsub(answer, '<div class="footer">', '<end><div class="footer">')


local m3ustr
local name,adr,grp,img

local i=1,j
j=1

if UseLocalLogo == '1' then
   local sqlstr = 'Select main.id,main.pict from main where main.updateid like "%TutHD_%";'
   local dTable = m_simpleTV.Database.GetTable(sqlstr)
   if dTable ~= nil and dTable[1] ~= nil then
     for k, v in ipairs(dTable) do 
        --debug_in_file(v.id .. '  ' .. v.pict .. '\n')
        m_simpleTV.Database.ExecuteSql('DELETE FROM Main WHERE (Main.Id=' .. v.id .. ');',true) 
     end
   end
end

for ww in string.gmatch(answer,'<div class="category%-name">(.-)<end>') do

    grp = findpattern(ww,'(.-)<',1,0,1)
    if grp == nil then grp = 'tuthd' end
	
      for w in string.gmatch(ww,'<a(.-)/a>') do
        
          name = findpattern(w,'<div class="name">(.-)<',1,18,1)
          adr = findpattern(w,'href="(.-)"',1,6,1)
          if name == nil or adr == nil then break end
          adr = 'http://tuthd.tv.'..adr
          img = findpattern(w,'src="(.-)"',1,5,1)
          if img == nil then img = '' end

          if UseLocalLogo == '1' then    

              m3ustr = '#EXTINF:-1 update-code="' .. UpdateID .. grp .. name .. '" group-title="' .. grp .. '",' .. name .. '\n' .. adr .. '\n'

           elseif UseLocalLogo ~= '1' then

             m3ustr = '#EXTINF:-1 update-code="' .. UpdateID .. grp .. name .. '" group-title="' .. grp .. '" tvg-logo="http://tuthd.tv' .. img .. '",' .. name .. '\n' .. adr .. '\n'

         end

         tfile = io.open(tmpName,'a+')
         tfile:write(m3ustr)
         tfile:close()
      --debug_in_file(m3ustr)
        j=j+1  
      end

      i=i+1
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 = 'TutHD'
p.ExtFilterLogo =  '..\\Channel\\logo\\extFiltersLogo\\tuthd.png'   -- the logo of extended filter (from ver. 0.4.8 b9)
p.ExtFilterLogoForce = 0
p.UpdateID=UpdateID

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

if err==true then
     local mess = "tuthd.tv - 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



