--script for http://imtv.at.ua/new/lPTV.html (Луганск) 25.02.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,3000,0)
 --INTERNET_OPTION_RECEIVE_TIMEOUT
 m_simpleTV.WinInet.SetOptionInt(session,6,5000,0)
---------------------------------------------------------------
local function ChangeName(name)
t={
{"1Музыка","Музыка Первого"},
{"1 Channel Europe","1 Channel Europe"},
{"1kanal","Первый канал"},
{"24 Dock","24 Док"},
{"24 techno","24 Техно"},
{"24 Док","24 Док"},
{"24Док","24 Док"},
{"24ДОК","24 Док"},
{"2х2 (2)","2х2 (+2)"},
{"2x2","2х2"},
{"2х2tv","2х2"},
{"5 Kanal","5 канал"},
{"5 Канал","5 канал"},
{"5Kanal","5 канал"},
{"Animals home","Домашние животные"},
{"Avto 24","Авто 24"},
{"balance","Баланс ТВ"},
{"boetz","Боец"},
{"Bridge TV","BRIDGE TV"},
{"Da Vinci Learning","Da Vinci Learning"},
{"Discovery ID","Investigation Discovery"},
{"Disney","Disney Channel"},
{"Euro Sport","Eurosport"},
{"EuroNews","Euronews"},
{"Fifth","Пятый канал"},
{"firstmuz","1HD"},
{"FOX Life","FOX Life"},
{"Fox Life","FOX Life"},
{"Friday","Пятница!"},
{"history","История"},
{"illusion plus","Иллюзион+"},
{"interesnoe tv","Интересное ТВ"},
{"Investigation Discovery","Investigation Discovery"},
{"Jim Jam","JimJam"},
{"Kino Soyuz","НТВ+ Киносоюз"},
{"Kinohit","НТВ+ Кинохит"},
{"Kinoreis 2","Кинорейс 2"},
{"Kinoreis 3","Кинорейс 3"},
{"Kinoreis 4","Кинорейс 4"},
{"Kinoreis 5","Кинорейс 5"},
{"Kinoreys 1","Кинорейс 1"},
{"Kinoreys 3","Кинорейс 3"},
{"Kinoreys 4","Кинорейс 4"},
{"komediya","Комедия ТВ"},
{"Комсомольская правд","Комсомольская правда"},
{"KomPravda","Комсомольская правда"},
{"kprf","КПРФ"},
{"ldpr","ЛДПР"},
{"Life NEWS","L!FENEWS"},
{"MCM TOP","MCM Top"},
{"MIR 24","Мир 24"},
{"mir","Мир"},
{"MTV Hits","MTV Hits"},
{"MTV rocks","MTV Rocks"},
{"Music Box","Music Box TV"},
{"musboxru","Music Box RU"},
{"musboxtv","Music Box TV"},
{"MUSIC BOX RU","Music Box RU"},
{"Music Box Ru","Music Box RU"},
{"Muz TV","Муз ТВ"},
{"nash football","Наш футбол"},
{"Nashe Kino","НТВ+ Наше кино"},
{"Nauka 2.0","Наука 2.0"},
{"Nickelodeon","Nickelodeon"},
{"novoe kino","НТВ+ Новое кино"},
{"NTV Futbol 2","НТВ+ Футбол 2"},
{"NTV Futbol","НТВ+ Футбол 1"},
{"NTV Mir","НТВ Мир"},
{"NTV Sport","НТВ+ Спорт"},
{"NTV","НТВ"},
{"NTV","НТВ"},
{"ots","ОТС"},
{"Paramount comedy","Paramount Comedy"},
{"perec","Перец"},
{"Perec","Перец"},
{"Peretz","Перец"},
{"Perviy","Первый канал"},
{"Planeta RTR","РТР-Планета"},
{"Primiera","НТВ+ Премьера"},
{"Pyatnitsa!","Пятница!"},
{"pyatnitsa","Пятница!"},
{"pyatyi kanal","Пятый канал"},
{"Ren TV","РЕН ТВ"},
{"REN","РЕН ТВ"},
{"Rossiya 1","Россия 1"},
{"Rossiya 24","Россия 24"},
{"Rossiya 24","Россия 24"},
{"Rossiya K","Россия K"},
{"Rossiya rtr","РТР-Планета"},
{"RT Doc HD","Russia Today Документальный HD"},
{"Ru TV","RU TV"},
{"Rusong","RUSONG TV"},
{"Russia 1","Россия 1"},
{"Russia 2","Россия 2"},
{"Russia 24","Россия 24"},
{"Russia Today HD","Russia Today HD"},
{"Russia Today","Russia Today"},
{"RUSSIA TODAY","Russia Today"},
{"Russkiy Extrim","Русский экстрим"},
{"S TV","STV"},
{"sbc reality","CBS Reality"},
{"smile","Улыбка ребёнка"},
{"Soccer","Футбол"},
{"sporthit","НТВ+ Футбол 3"},
{"STS","СТС"},
{"sts","СТС"},
{"TeenTV","TeenTV"},
{"TNT","ТНТ"},
{"TNT","ТНТ"},
{"TPO","TPO"},
{"TRAVELCHANNEL HD","Travel Channel HD"},
{"TV 1000","TV1000"},
{"tv center","ТВЦ"},
{"TV XXI","TV XXI"},
{"TV-3","ТВ 3"},
{"TV1000 Action East","TV1000 Action"},
{"TV3","ТВ 3"},
{"tvc","ТВЦ"},
{"tvmchannel","TVM Channel"},
{"VH1","VH1"},
{"VH1Classic","VH1 Classic"},
{"World 24","Мир 24"},
{"yumor_tv","Юмор ТВ"},
{"zagorod_zh","Загородная жизнь"},
{"zvezda","Звезда"},
{"Боец ТВ","Боец"},
{"Боец","Боец"},
{"Живи","Живи!"},
{"ЖИВИ","Живи!"},
{"Киносоюз","НТВ+ Киносоюз"},
{"Ля Минор","Ля минор"},
{"Ля-минор ТВ","Ля минор"},
{"МИР","Мир"},
{"Москва доверие","Москва. Доверие"},
{"НАШ","Наш Футбол"},
{"НСТ","Настоящее страшное ТВ"},
{"НТВ + Футбол","НТВ+ Футбол"},
{"НТВ + Футбол","НТВ+ Футбол"},
{"НТВ Баскетбол","НТВ+ Баскетбол"},
{"НТВ Наш Футбол","НТВ+ Наш Футбол"},
{"Охота и Рыбалка","Охота и рыбалка"},
{"1 Первый","Первый канал"},
{"Психология21","Психология 21"},
{"Пятница","Пятница!"},
{"Сетанта Спорт","Setanta Sport"},
{"СОЮЗ","Союз"},
{"СПАС","Спас"},
{"ТБН РОССИЯ","ТБН Россия"},
{"ТВ3+3","ТВ 3 (+3)"},
{"Шансон TV","Шансон ТВ"},
{"Экстрим спорт","Extreme Sport"},
{"Юмор Box","Юмор BOX"},
{"Юмор тв","Юмор ТВ"},
{"ЮМОР ТВ","Юмор ТВ"},
}

  for k,v in ipairs(t) do
     if name == v[1] then name = v[2] end
  end

   return name
end
------------------------------
 m_simpleTV.OSD.ShowMessage("Луганск - начало обновления" ,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 url = 'http://imtv.at.ua/new/lPTV.html?rand=' .. math.random()

local rc,answer=m_simpleTV.WinInet.Get(session,url)
m_simpleTV.WinInet.Close(session)

if rc~=200 then
       m_simpleTV.WinInet.Close(session)
	   return
end
answer = m_simpleTV.Common.string_fromUTF8(answer)
answer = findpattern( answer,'<optgroup>(.+)</optgroup>',1,0,0)
if answer == nil then
    m_simpleTV.OSD.ShowMessage("Луганск - ошибка обновления",255,10)
	return
end

answer = string.gsub(answer,'\n','')
answer = string.gsub(answer,'<optgroup>','<optgroup label="Разные каналы">')
answer = string.gsub(answer,'<optgroup ','</optgroup><optgroup ')
--debug_in_file(answer .. '\n')

local i=1
local m3ustr, name, adr, grp
for w in string.gmatch(answer,'<optgroup(.-)optgroup>') do
	grp = findpattern( w,'label="(.-)"',1,7,1)
	if grp == nil then break end

   for ww in string.gmatch(w,'<option(.-)option>') do

	adr = findpattern( ww,'value="(.-)"',1,7,1)
	name = findpattern( ww,'%b><',1,1,1)
	if adr == nil or name == nil then break end
--debug_in_file(name .. '\n')
--	name = ChangeName(name)
--debug_in_file('#EXTINF:-1,'..grp..':'.. name..'\n'..adr..'\n')
	
 m3ustr = '#EXTINF:%-1,'..grp..':'.. name..'\n'..adr..'\n'
 tfile = io.open(tmpName,'a+')
 tfile:write(m3ustr)
 tfile:close()
   end
 i=i+1
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 = 'Lugansk'
p.ExtFilterLogo =  '..\\Channel\\logo\\extFiltersLogo\\IMTV.at.ua.jpg'   -- 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 = "Луганск - каналы обновлены (" .. 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
-----------------------------------

