-- script for PCradio (10/11/2015)
 local UpdateID='PCRADIO01'

 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)

 local url =  "http://pcradio.biz/player/listradio/pcradio_ru.xml"  

 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,10)
	   return
 end

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

 m_simpleTV.OSD.ShowMessage("PCradio - start updating\nThe update takes extremely long time\nPlease wait .." ,0xFF00,10)
 
local m3ustr='#EXTM3U $LogoPath="..\\Channel\\logo\\RadioAddonLogo\\PCradio\\"\n'
local name,adr,grp,id

for w in string.gmatch(answer, '<channel>(.-)</channel>') do

       id = string.match(w,'<genre_id>(.-)</genre_id>')   
       name = string.match(w,'<title>(.-)</title>')
       adr = string.match(w,'<url_hi>(.-)</url_hi>')
       grp = string.match(w,'<genre_sait>(.-)</genre_sait>')
       if id == nil or name == nil or adr == nil or grp == nil then break end
       name = string.gsub(name,',',' ')
       grp = string.gsub(grp,',',' ')
    if id ~= '29' then
      m3ustr = m3ustr .. '#EXTINF:-1 update-code="' .. UpdateID .. grp .. name .. '",PCradio ' .. grp .. ':' .. name .. '\n' .. adr ..'$OPT:http-user-agent=pcradio' ..'\n' 
    end
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(m3ustr)
 tfile:close() 

  --опции  для загрузки плейлиста 
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.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 = "PCradio - радиостанции обновлены (" .. 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
