-- плейлист torent-tv http://super-pomoyka.us.to/trash/ttv-list/ 12/03/2015

 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)
-----------------------------------------------

 m_simpleTV.OSD.ShowMessage("TTV - начало обновления" ,0xFF00,5)

local url = 'http://super-pomoyka.us.to/trash/ttv-list/ttv.m3u'

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

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


if not string.match(answer,'EXTM3U') then 
  m_simpleTV.OSD.ShowMessage("TTV - ошибка обновления",255,10)
 return 
end

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 function GetGrp(grp)
  if string.match(grp,'%(') then
   grp = string.match(grp,'%((.+)')
  end
  return grp
end

local name,grp,m3ustr,adr
for w in string.gmatch(answer, '#EXTINF(.-//.-)\n') do
--debug_in_file(w .. '\n')
   grp = string.match(w,'%((.+)%)')
   if grp == nil then grp = 'Другое' end
   grp = GetGrp(grp)
   --name = string.match(w,',(.-)$')
   name = string.match(w,',(.-%()')
    if name == nil then name = string.match(w,',(.-%))') end
    if name == nil then name = string.match(w,',(.-)\n') end
    name = string.gsub(name,'%(','')
--debug_in_file(name .. '\n')
   adr = string.match(w,'\n(.-)$')
  if grp == nil or name == nil  then break end
  m3ustr = '#EXTINF:-1,' .. grp .. ':' .. name .. '\n' .. adr .. '\n'
--debug_in_file(m3ustr)
 tfile = io.open(tmpName,'a+')
 tfile:write(m3ustr)
 tfile:close()
end

--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 = 0  --  -1-auto  0 - plane text  1- UTF8  2- unicode
p.BorpasFileFormat=0
p.AutoNumber = 0
p.ExtFilter = 'TTV'
p.ExtFilterLogo =  '..\\Channel\\logo\\extFiltersLogo\\TTV.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 = "TTV - каналы обновлены (" .. 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

