-- script for 101.ru (10/11/2015)
 local UpdateID='101RU01'

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

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

 local rc,answer=m_simpleTV.WinInet.Get(session,'http://101.ru/?an=port_allchannels')

 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) 
local m3ustr='#EXTM3U $LogoPath="..\\Channel\\logo\\RadioAddonLogo\\101RU\\"\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 tmp =  findpattern( answer,'<ul class="tabs vertical">(.-)</ul>',1,0,0)
if tmp ~= nil then

local t = {}
local i=1
local link,name,url,title,group
for w in string.gmatch(tmp, "<li(.-)li>") do
	link = findpattern (w,'href="(.-)"',1,6,1)
	name = findpattern (w,'>(.-)/a',1,1,2)
	name = findpattern (name,'%b><',1,1,1)
	
	t[i] =  {}
	t[i].link = link
	t[i].name = name
--	debug_in_file(t[i].name .. '  http://101.ru' .. t[i].link .. '\n')
	i=i+1
end

  for i=1,#t do
	group = '101RU ' .. t[i].name
	inAdr = 'http://101.ru' ..  t[i].link
	inAdr = string.gsub(inAdr,'amp;','')
	rc,answer=m_simpleTV.WinInet.Get(session,inAdr)

   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)

	tmp =  findpattern( answer,'<ul class="list channels%-new(.-)</ul>',1,0,0)
	if tmp ~= nil then
		for w in string.gmatch(tmp, '<h2 class="title">(.-)</h2>') do
                     url = findpattern (w,'href="(.-)"',1,6,1)
                     title = findpattern (w,'%b><',1,1,1)
                     if url == nil or title == nil then break end							
		     url = string.gsub(url,'amp;','')
		     url = 'http://101.ru' .. url
			m3ustr = '#EXTINF:-1 update-code="' .. UpdateID .. group .. title .. '",' .. group .. ':' .. title .. '\n' .. url .. '\n' 

--debug_in_file(m3ustr)
 tfile = io.open(tmpName,'a+')
 if tfile==nil then 
	os.remove(tmpName)
	return
 end
  
 tfile:write(m3ustr)
 tfile:close() 
		end

	end
  end
end
m_simpleTV.WinInet.Close(session)

  --опции  для загрузки плейлиста 
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.ExtFilter = 'Radio'
p.AutoNumber = 0
p.UpdateID=UpdateID
p.ExtFilterLogo =  '..\\Channel\\logo\\icons\\radio.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 = "101RU - радиостанции обновлены (" .. 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

