--script for vtvplus.vn (20/03/2015)

local auth = m_simpleTV.MainScriptDir .. 'user\\tvplaylists\\viet\\auth.txt'
local fhandle = io.open (auth , "r")

if fhandle == nil then
  if m_simpleTV.User.VietTV.VTVPlus == nil then m_simpleTV.User.VietTV.VTVPlus = 'VTVPlus' end
   local Object = m_simpleTV.Dialog.Show('VietTV authorization form',m_simpleTV.MainScriptDir .. 'user\\tvplaylists\\viet\\viettvconfig.html','user\\tvplaylists\\viet\\viettvconfig.lua',350,250,1+8+32) 
  return
end

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

local login,password = string.match(vtvplus,'VTVPlus:login="(.-)",password="(.-)"')
if login == nil or login == '' or password == nil or password == '' then 
	   m_simpleTV.OSD.ShowMessage("VTV Plus script error - can't find login and password",255,10)
	   return
 end
if not string.match(login,'@') then 
	   m_simpleTV.OSD.ShowMessage("VTV Plus - incorrect login data, email required",255,10)
	   return
 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,8000,0)
  --INTERNET_OPTION_RECEIVE_TIMEOUT
  m_simpleTV.WinInet.SetOptionInt(session,6,8000,0)
---------------------------------------------------------------------------
function trim(str)
str = string.match(str,'^%s*(.-)%s*$')
  return str
end
---------------------------------------------------------------------------

login = string.gsub(login,'@','%%40')
local body = 'username=' .. login .. '&password=' .. password --.. '&urlload=.%2F'

local head = 
'Accept-Language: ru-RU,ru;q=0.9,en;q=0.8\n' ..
'Referer: http://vtvplus.vn/user-dangnhap.html\n' ..
'Accept: text/html, */*; q=0.01\n' ..
'Content-Type: application/x-www-form-urlencoded\n' ..
'X-Requested-With: XMLHttpRequest'

 local rc,answer=m_simpleTV.WinInet.Post(session,'http://vtvplus.vn/vtv/ajax.php?act=login',nil,head,body)

 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\n')

if not string.match( answer, 'index' ) then 
	  m_simpleTV.OSD.ShowMessage("Error - Incorrect Login or Password",255,10)
   return 
end

local host = 'http://vtvplus.vn/'

rc,answer=m_simpleTV.WinInet.Get(session,host)
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\n')

answer = findpattern( answer,'<div class="tab%-content"(.-)<div class="tab%-pane fade"',1,0,0)
if answer == nil then
	   m_simpleTV.OSD.ShowMessage("VTV Plus script error" ,255,10)
	   return
 end

m_simpleTV.OSD.ShowMessage("VTV Plus - playlist start loading" ,0xFF00,10)

local m3ustr = '#EXTM3U\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 i=1
local name,adr

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

	name = findpattern(w,'<div class="info_title">(.-)<',1,24,1)
	adr = findpattern(w,'href="(.-)"',1,6,1)
	if name == nil or adr == nil then break end

       m3ustr = '#EXTINF:-1,VTV Plus:' .. trim(name) .. '\n' .. host .. trim(adr) .. '\n'
       tfile = io.open(tmpName,'a+')
       tfile:write(m3ustr)
       tfile:close()
   i=i+1
 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 = 'VietTV'
p.ExtFilterLogo =  '..\\Channel\\logo\\extFiltersLogo\\viet.png'   -- the logo of extended filter (from ver. 0.4.8 b9)
p.ExtFilterLogoForce = 0

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

if err==true then
     local mess = "VTV Plus - playlist loading complete"	 
	 m_simpleTV.OSD.ShowMessage_UTF8(mess,0x37BEDF,5)
end
