--script for http://fptplay.net (17/10/2015)

require ('jsdecode')

local js = m_simpleTV.MainScriptDir .. 'user\\tvplaylists\\viet\\fptplay.js'
local fhandle = io.open (js , "r")
if fhandle == nil then
	   m_simpleTV.OSD.ShowMessage("FPT Play: script error - can't open fptplay.js",255,10)
	   return
 end

local scr = fhandle:read("*a")
fhandle:close()

local filepath  = m_simpleTV.Common.GetMainPath (0)

   local UpdateID='FPTPlay_TV01'
   local UseLocalLogo = nil

if UseLocalLogo == nil then
   local fhandle = io.open (m_simpleTV.MainScriptDir ..  "user/tvplaylists/tvmenu.ini")   
   if fhandle == nil then 
      UseLocalLogo = '1'
      return
   end
   local str = fhandle:read('*a')
   fhandle:close()

   UseLocalLogo = string.match(str,'UseLocalLogo=(%d);')
   if UseLocalLogo == nil then UseLocalLogo = 0 end
end

if UseLocalLogo == '1' then
   local sqlstr = 'Select main.id,main.pict from main where main.updateid like "%FPTPlay_%";'
   local dTable = m_simpleTV.Database.GetTable(sqlstr)
   if dTable ~= nil and dTable[1] ~= nil then
     for k, v in ipairs(dTable) do 
        --debug_in_file(v.id .. '  ' .. v.pict .. '\n')
        m_simpleTV.Database.ExecuteSql('DELETE FROM Main WHERE (Main.Id=' .. v.id .. ');',true) 
     end
   end
end

-----------------------------------------------

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

 m_simpleTV.OSD.ShowMessage("FPT Play: start loading playlist" ,0xFF00,15)

  local rc,answer=m_simpleTV.WinInet.Get(session,'http://fptplay.net/livetv')
 -- 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

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

 local tmp = findpattern(answer,'<div class="wrap_content_title">(.-)<div class="ads_bot_banner">',1,0,0)
 if tmp == nil then
  	   m_simpleTV.OSD.ShowMessage("FPT Play script error - channels not found "  ,255,3)
    return
 end
--debug_in_file(tmp .. '\n')
--do return end
  local adr,name,img,grp
  local m3ustr = '#EXTM3U\n' 
  local i=1

for ww in string.gmatch(tmp,'<div class="row fix_padding">(.-)<script') do

  grp = findpattern(ww,'<span class="pull%-left">(.-)<',1,24,1)
  if grp == nil then break end
  local g = 'var g="' .. grp .. '";'
  local  prescr = g .. scr
  grp = jsdecode.DoDecode('decodeHTML(g)', false, prescr, 0, 0)

  for w in string.gmatch(ww,'<a class="tv_channel(.-)/a>') do

       adr = findpattern(w,'href=%b""',1,6,1)
       name = string.match(w,'alt="(.-)"')
       img = string.match(w,'original="(.-)"')

       if adr == nil or name == nil or img == nil then break end

       local e = 'var e="' .. name .. '";'
       local  prescr = e .. scr
       name = jsdecode.DoDecode('decodeHTML(e)', false, prescr, 0, 0)

--[[
       rc = m_simpleTV.WinInet.GetFile(session, img ,nil, filepath .. 'fptplay' .. i .. '.png') 
       if rc == nil then 
  	   m_simpleTV.OSD.ShowMessage("FPT Play error - can't save logo " .. name ,255,3)
       end
       m_simpleTV.Common.Wait(100)
       m3ustr = m3ustr .. '#EXTINF:-1 group-title="FPT Play" tvg-logo="..\\Channel\\logo\\fptplay' .. i .. '.png",' .. name .. '\n' .. adr .. '\n'
]]
          if UseLocalLogo == '1' then    

              m3ustr = m3ustr .. '#EXTINF:-1 update-code="' .. UpdateID .. grp .. name .. '" group-title="' .. grp .. '",' .. name .. '\n' .. adr .. '\n'

           elseif UseLocalLogo ~= '1' then

             m3ustr = m3ustr .. '#EXTINF:-1 update-code="' .. UpdateID .. grp .. name .. '" group-title="' .. grp .. '" tvg-logo="' .. img .. '",' .. name .. '\n' .. adr .. '\n'

         end
  i=i+1
--debug_in_file(m3ustr)
 end
end
m_simpleTV.WinInet.Close(session)

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=1
p.AutoNumber = 0
p.ExtFilter = 'FPTPlay'
p.ExtFilterLogo =  '..\\Channel\\logo\\extFiltersLogo\\logo_FptPlay.png'   -- the logo of extended filter (from ver. 0.4.8 b9)
p.ExtFilterLogoForce = 0
p.UpdateID=UpdateID

local err,add,ref,names = m_simpleTV.Common.LoadPlayList (tmpName,p,0,true,false)
os.remove(tmpName)


  if err==true then
     local mess = "FPT Play: playlist loading complete (New channels: " .. add .. ")"
	 if add > 0 and add < 25 then 
	    names = string.gsub(names,'%$end','\n')
		mess = mess .. '\n' .. names
	 end
	 
	 m_simpleTV.OSD.ShowMessage(mess,0x37BEDF,3) 
  end
