--getaddress 

if m_simpleTV.Control.ChangeAdress ~= 'No' then return end

local inAdr =  m_simpleTV.Control.CurrentAdress

if inAdr==nil then return end

if not string.match( inAdr, '^http://vtvplus%.vn' ) and not string.match( inAdr, '^http://hplus%.com%.vn') and not string.match( inAdr, '^http://tv%.vnzee%.com/livetv~') and not string.match( inAdr, '^http://fptplay%.net/livetv') then return end

m_simpleTV.Control.ChangeAdress='Yes'
m_simpleTV.Control.CurrentAdress = ''
----------------------------------------
 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
---------------------------------------------------------------------------

if string.match( inAdr, '^http://vtvplus%.vn' ) then

local auth = m_simpleTV.MainScriptDir .. 'user\\tvplaylists\\viet\\auth.txt'
local fhandle = io.open (auth , "r")
if fhandle == nil then 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 return end


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

local head = 
'Accept-Language: ru-RU,ru;q=0.9,en;q=0.8\n' ..
'Referer: ' .. inAdr .. '\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 error1 - " .. rc ,255,10)
	   return
 end

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

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

inAdr = findpattern( answer,"href='(.-)'",1,6,1)
if inAdr==nil then return end

rc,answer=m_simpleTV.WinInet.Get(session,inAdr)

 if rc~=200 then
	   m_simpleTV.WinInet.Close(session)
	   m_simpleTV.OSD.ShowMessage("Connection error2 - " .. rc ,255,10)
	   return
 end

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

local tmp  = findpattern(answer,'responseText = "(.-);',1,16,2)

if tmp == nil then 
	   m_simpleTV.OSD.ShowMessage("VTV Plus error - can't find stream",255,10)
	   return
 end

inAdr  = findpattern(tmp,'(.-),',1,0,1)
--debug_in_file(inAdr .. '\n\n')

if inAdr == nil then 
   m_simpleTV.Control.CurrentAdress = tmp
  return
end

if string.match(tmp,'definst_') then
   m_simpleTV.Control.CurrentAdress = tmp
  return
end

host = findpattern(inAdr,'http(.-)live/',1,0,0)
local e = findpattern(inAdr,'&e=(.+)',1,0,0)
if host == nil or e == nil then return end

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

 if rc~=200 then
	   m_simpleTV.WinInet.Close(session)
	   m_simpleTV.OSD.ShowMessage("Connection error3 - " .. rc ,255,10)
	   return
 end

local i=1
local t={}
local retAdr,name,adr

 for w in string.gmatch(answer,'EXT%-X%-STREAM%-INF(.-)&') do

	 name = findpattern (w,'x(.-)\n',1,1,1)
	 adr = findpattern (w,'_(.-)$',1,0,0)
	 if name == nil or adr == nil then break end

     t[i] = {}
     t[i].Id   =  i
     t[i].Name = name .. 'p'
     t[i].Adress  = host .. adr .. e

--debug_in_file(t[i].Name .. '  ' .. t[i].Adress .. '\n\n')
   i=i+1
 end

 if i>2 then
    m_simpleTV.OSD.ShowSelect('Select quality',0,t,10000,32)
 end

 if i>1 then
     retAdr = t[1].Adress
 end

 if retAdr == nil then
     m_simpleTV.OSD.ShowMessage("VTV Plus error  - stream not found",255,10)
 end
--debug_in_file(retAdr .. '\n\n')
  m_simpleTV.Control.CurrentAdress = retAdr
end

if string.match( inAdr, '^http://hplus%.com%.vn') then 

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

 if rc~=200 then
	   m_simpleTV.WinInet.Close(session)
	   m_simpleTV.OSD.ShowMessage("HPlus Connection error - " .. rc ,255,10)
	   return
 end

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

local retAdr = findpattern(answer,'var iosUrl = "(.-)"',1,14,1)
 if retAdr == nil then
     m_simpleTV.OSD.ShowMessage("HPlus error  - stream not found",255,10)
    return
 end

  m_simpleTV.Control.CurrentAdress = retAdr
end


if string.match( inAdr, '^http://tv%.vnzee%.com/livetv~') then 

local 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

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

local ch = findpattern(answer,'load(.-);',1,6,3)
if ch == nil then 
	   m_simpleTV.OSD.ShowMessage("zTV script error",255,10)
	   return
 end

local url = 'http://tv.vnzee.com/file/player?channel=' .. ch 

local head = 
'User-Agent: Opera/9.80 (Windows NT 6.1; U; ru) Presto/2.9.168 Version/11.52\n' ..
'Referer: ' .. inAdr .. '\n' ..
'X-Requested-With: XMLHttpRequest'

--debug_in_file(url .. '  ' .. head .. '\n')

rc,answer=m_simpleTV.WinInet.Get(session,url,nil,0,head)
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')

if  string.match(answer,'setup') then
   local retAdr = findpattern(answer,"'file': '(.-)'",1,9,1)
   if retAdr == nil then
      retAdr = findpattern(answer,'var source = "(.-)"',1,14,1)
       if retAdr == nil then     
          m_simpleTV.OSD.ShowMessage("zTV error  - stream not found",255,10)
             return
        end
    end
  m_simpleTV.Control.CurrentAdress = retAdr
  return
end
   

local retAdr = findpattern(answer,'var source = "(.-)"',1,14,1)
--debug_in_file(url_decode(retAdr) .. '\n\n')

 if retAdr == nil then
   retAdr = findpattern(answer,'iframe src="(.-)"',1,12,1)
      if retAdr == nil then
          m_simpleTV.OSD.ShowMessage("zTV error  - stream not found",255,10)
         return
      end

--[[
    local stream = findpattern(retAdr,'src=(.+)',1,4,0)
    if stream == nil then
	   m_simpleTV.OSD.ShowMessage(url_decode(retAdr) .. "\nnot supported by SimpleTV",255,10)
	   return
    end
]]
    if string.match(retAdr,'fptplay') then
      retAdr = stream .. '$OPT:http-user-agent=Opera/9.80 (Windows NT 6.1; Win64; x64) Presto/2.12.388 Version/12.17$OPT:http-referrer=' .. retAdr .. '$OPT:hds-max-queue=1$OPT:hds-live-leadtime=10'
    end

  if retAdr == nil then
      m_simpleTV.OSD.ShowMessage("zTV error  - stream not found",255,10)
     return
  end
end
retAdr = url_decode(retAdr)

--debug_in_file(retAdr .. '\n\n')
  m_simpleTV.Control.CurrentAdress = retAdr

end

if string.match( inAdr, '^http://fptplay%.net/livetv') then

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

 if rc~=200 then
	   m_simpleTV.WinInet.Close(session)
	   m_simpleTV.OSD.ShowMessage("FPT Play Connection error - " .. rc ,255,10)
	   return
 end

--debug_in_file(answer .. '\n\n')
 local id = findpattern(answer,"var id = '(.-)'",1,10,1)
 if id == nil then
	   m_simpleTV.OSD.ShowMessage("FPT Play script error" ,255,10)
	   return
 end

---id=vtv1-hd&type=newchannel&quality=3&mobile=web
local body = 'id=' .. id .. '&type=newchannel&quality=3&mobile=web'

local head = 
'Accept-Language: ru-RU,ru;q=0.9,en;q=0.8\n' ..
'Referer: ' .. inAdr .. '\n' ..
'Content-Type: application/x-www-form-urlencoded; charset=UTF-8\n' ..
'Accept: application/json, text/javascript, */*; q=0.01\n' ..
'X-Requested-With: XMLHttpRequest'

 local rc,answer=m_simpleTV.WinInet.Post(session,'http://fptplay.net/show/getlinklivetv',nil,head,body)
m_simpleTV.WinInet.Close(session)

 if rc~=200 then
	   m_simpleTV.WinInet.Close(session)
	   m_simpleTV.OSD.ShowMessage("Connection error1 - " .. rc ,255,10)
	   return
 end

--debug_in_file(answer .. '\n\n')
 local retAdr = findpattern(answer,'stream":"(.-)"',1,9,1)
 if retAdr == nil then
	   m_simpleTV.OSD.ShowMessage("FPT Play error - can't find stream" ,255,10)
	   return
 end

 retAdr = string.gsub(retAdr,'\\','')
  m_simpleTV.Control.CurrentAdress = retAdr

end

--http://fptplay.net/xem-video/6-anh-em-sieu-nhan-1994-mighty-morphin-power-rangers-ss2-5551667f17dc132a09ee17c0.html#tap-3
--[[
if string.match( inAdr, '^http://fptplay%.net/xem%-video') then

  local url = findpattern(inAdr,'^http(.-)#',1,0,1)
  if url == nil then url=inAdr end

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

 if rc~=200 then
	   m_simpleTV.WinInet.Close(session)
	   m_simpleTV.OSD.ShowMessage("FPT Play Connection error - " .. rc ,255,10)
	   return
 end

--debug_in_file(answer .. '\n\n')
 local id = findpattern(answer,"var id = '(.-)'",1,10,1)
 if id == nil then
	   m_simpleTV.OSD.ShowMessage("FPT Play script error" ,255,10)
	   return
 end

 local ep = string.match(inAdr,'#tap%-(%d+)')
 if ep == nil then ep = 1 end

local body = 'id=' .. id .. '&episode=' .. ep .. '&mobile=web'

local head = 
'Accept-Language: ru-RU,ru;q=0.9,en;q=0.8\n' ..
'Referer: ' .. inAdr .. '\n' ..
'Content-Type: application/x-www-form-urlencoded; charset=UTF-8\n' ..
'Accept: application/json, text/javascript, */*; q=0.01\n' ..
'X-Requested-With: XMLHttpRequest'

 local rc,answer=m_simpleTV.WinInet.Post(session,'http://fptplay.net/show/getlink',nil,head,body)
m_simpleTV.WinInet.Close(session)

 if rc~=200 then
	   m_simpleTV.WinInet.Close(session)
	   m_simpleTV.OSD.ShowMessage("Connection error1 - " .. rc ,255,10)
	   return
 end

debug_in_file(answer .. '\n\n')
 local retAdr = findpattern(answer,'stream":"(.-)"',1,9,1)
 if retAdr == nil then
	   m_simpleTV.OSD.ShowMessage("FPT Play error - can't find stream" ,255,10)
	   return
 end

 retAdr = string.gsub(retAdr,'\\','')
  m_simpleTV.Control.CurrentAdress = retAdr

end
]]