local base = _G

module ('nowrucore')

function nowru_getaddress(inAdr)
--base.debug_in_file(inAdr .. "\n")
if inAdr==nil then return nil end

local session = base.m_simpleTV.WinInet.New("Opera/9.80 (Windows NT 6.0; U; ru) Presto/2.10.229 Version/11.64")

 if session == nil then return nil end

 --INTERNET_OPTION_CONNECT_TIMEOUT
 base.m_simpleTV.WinInet.SetOptionInt(session,2,3000,0)
 --INTERNET_OPTION_RECEIVE_TIMEOUT
 base.m_simpleTV.WinInet.SetOptionInt(session,6,5000,0)
 
inAdr = base.string.gsub(inAdr,'#%d+','')

local rc,answer=base.m_simpleTV.WinInet.Get(session,inAdr)
if rc~=200 then return nil end

local url = base.findpattern( answer,'smil=(.-)"',1,5,1)

if url ~=nil then  
  url = base.url_decode(url)
 else 
    url = base.findpattern (answer,'"smil":"(.-)"',1,8,1)
	if url == nil then return nil end
	url = base.string.gsub(url,'\\','') 
end

local rc,answer=base.m_simpleTV.WinInet.Get(session,url)
if rc~=200 then return nil end
--base.debug_in_file(answer .. "\n")
url = base.findpattern( answer,'src="(.-)"',1,5,1)
if url ==nil then return nil end

local rc,answer=base.m_simpleTV.WinInet.Get(session,url)
if rc~=200 then return nil end

local srv = base.findpattern( answer,'<to>(.-)</to>',1,4,5)
if srv ==nil then return nil end

local str = base.findpattern( url,'/vod/(.-)$',1,5,0)
if str ==nil then return nil end
local retAdr = srv .. str .. '$OPT:codec=avcodec'

--base.debug_in_file(retAdr .. "\n")

base.m_simpleTV.WinInet.Close(session)
return retAdr

end

