--edem.tv 08/03/2015

local edem = m_simpleTV.MainScriptDir .. 'user\\tvplaylists\\edemtv\\edem.txt'
local fhandle,url

fhandle = io.open (edem , "r")

if fhandle == nil then
   local Object = m_simpleTV.Dialog.Show('Обновить плейлист Edem TV',m_simpleTV.MainScriptDir .. 'user\\tvplaylists\\edemtv\\EdemTV.html','user\\tvplaylists\\edemtv\\EdemTV.lua',350,250,1+8+32) 
  return
end

local t ={}
 t[1] = {} t[2] = {} 

  t[1].Id=1
  t[1].Name='Обновить с авторизацией'
  t[1].Adress=''
 
  t[2].Id=2
  t[2].Name='Обновить без авторизации'
  t[2].Adress=''

 local ret,id = m_simpleTV.OSD.ShowSelect('Обновить плейлист Edem TV',0,t,10000,1+4+8)
  
 if id==1 then 
      local Object = m_simpleTV.Dialog.Show('Обновить плейлист Edem TV',m_simpleTV.MainScriptDir .. 'user\\tvplaylists\\edemtv\\EdemTV.html','user\\tvplaylists\\edemtv\\EdemTV.lua',350,250,1+8+32) 
  elseif id==2 then

 url = fhandle:read("*all")
 fhandle: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,3000,0)
 --INTERNET_OPTION_RECEIVE_TIMEOUT
 m_simpleTV.WinInet.SetOptionInt(session,6,5000,0)
--HTTPS
m_simpleTV.WinInet.SetOpenRequestFlags(session, 0x00800000 + 0x00002000 + 0x00001000)
---------------------------------------------------------------
function trim(str)
str = string.match(str,'^%s*(.-)%s*$')
  return str
end
-----------------
if url == nil or url == '' then
  m_simpleTV.OSD.ShowMessage("Edemm.tv - ошибка обновления",255,10)
 return 
end
m_simpleTV.OSD.ShowMessage("Edem.tv - начало обновления" ,0xFF00,10)

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,10)
	   return
 end
-- answer = m_simpleTV.Common.string_fromUTF8(answer)

if not string.match(answer,'EXTM3U') then 
  m_simpleTV.OSD.ShowMessage("Edemm.tv - ошибка обновления",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 $ExtFilter="Edem.tv"\n')
 tfile:close() 


local title = m_simpleTV.Common.string_toUTF8('Плейлист Edem.tv')
local qst = m_simpleTV.Common.string_toUTF8('Конвертировать плейлист в rtmp?')
local rtmp = m_simpleTV.Interface.MessageBox(qst,title,1,0)

local m3ustr,name,grp,link
local svr,key,chid

 if rtmp == 1 then
  for w in string.gmatch(answer, '#EXTINF(.-)m3u8') do
	name = findpattern (w,',(.-)\n',1,1,1)
	grp = findpattern (w,'#EXTGRP(.-)\n',1,8,1)
         svr,key,chid = string.match(w,'http://(.-)/iptv/(.-)/(.-)/')
      if svr == nil or key == nil or chid == nil then link = findpattern (w,'http(.-)$',1,0,0) end
        link = 'rtmp://' .. svr ..'/iptv/' .. chid .. '?key=' .. key
      if name == nil or grp == nil or link == nil then break end
	grp = trim(grp)
	m3ustr = '#EXTINF:-1,' .. grp .. ':' .. name .. '\n' .. link .. '\n'
--debug_in_file(m3ustr)
 tfile = io.open(tmpName,'a+')
 tfile:write(m3ustr)
 tfile:close()
  end

   elseif rtmp ~=1 then

    for w in string.gmatch(answer, '#EXTINF(.-)m3u8') do
    	name = findpattern (w,',(.-)\n',1,1,1)
    	grp = findpattern (w,'#EXTGRP(.-)\n',1,8,1)
    	link = findpattern (w,'http(.-)$',1,0,0)
    	if name == nil or grp == nil or link == nil then break end
    	grp = trim(grp)
    	--grp = grp:gsub("^%l", string.upper)
    --debug_in_file(grp .. '\n')
    	m3ustr = '#EXTINF:-1,' .. grp .. ':' .. name .. '\n' .. link .. 'm3u8\n'
    --debug_in_file(m3ustr)
     tfile = io.open(tmpName,'a+')
     tfile:write(m3ustr)
     tfile:close()
    end
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 = 1  --  -1-auto  0 - plane text  1- UTF8  2- unicode
p.BorpasFileFormat=0
p.AutoNumber = 0
p.ExtFilter = 'Edem.tv'
p.ExtFilterLogo =  '..\\Channel\\logo\\extFiltersLogo\\EdemTV.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 = "Edem.tv - каналы обновлены (" .. 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
end