local base = _G

module ('vkcoreAlt')


function vk_getadress(inAdr)

if inAdr==nil then return nil end


local qualite={'360','480','720'}
local qnum = 3 --1 for 360, 2 for 480, 3 for 720


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)

 local url
 local index

 inAdr = base.string.gsub(inAdr,'&amp;','&')

 if base.string.match( inAdr, 'http://vkontakte%.') then
    url   = 'vkontakte.ru'
    index = base.findpattern(inAdr,'vkontakte%.ru/(.*)',1,13,0)
else
     do
	 url   = 'vk.com'
     index = base.findpattern(inAdr,'vk%.com/(.*)',1,7,0)
	 end
end
 if index == nil then return nil end

local rc,StrFile=base.m_simpleTV.WinInet.Get(session,url,index)
base.m_simpleTV.WinInet.Close(session)

if rc~=200 then return nil end

local qstr= base.findpattern( inAdr,'&hd=(%d+)',1,4,0)

if qstr ==nil then qnum = 1 end

 if qstr~=nil then
    qstr = base.findpattern( StrFile,"video_max_hd = '(.-)'",1,16,1)
--base.debug_in_file(qstr .. "\n")
    qnum = base.tonumber(qstr)
	if qnum == nil or qnum > 3 then qnum = 1 end
 end

--base.debug_in_file(StrFile .. "\n\n\n")
local host= base.findpattern( StrFile,'host=(.-)&amp',1,5,4)
local uid = base.findpattern( StrFile,'uid=(.-)&amp',1,4,4)
local vtag = base.findpattern( StrFile,'vtag=(.-)&amp',1,5,4)
--base.m_simpleTV.OSD.ShowMessage("ff - " ..  host  ,255,5)

if host == nil or uid==nil or vtag==nil then
  return nil
end

local retadr = host .. 'u' .. uid .. '/videos/' .. vtag .. '.' .. qualite[qnum] .. '.mp4'
--base.m_simpleTV.OSD.ShowMessage("ff - " ..  retadr  ,255,5)

return retadr

end

