--script for VK (20.02.2013)
local body, login, password, host

--set your login and password here
login = ''
password = ''
----------------------------------
--get adress
require 'vkcore'
require('json')

function RuStr(Str, isFrom)
  if Str == nil then return end
  if isFrom == true then
   return m_LinkToPlay.Common.string_fromUTF8(Str, 1251)
  else
    return m_LinkToPlay.Common.string_toUTF8(Str, 1251)
  end
end

if m_LinkToPlay.Control.ChangeAddress ~= 'No' then return end

local inAdr = m_LinkToPlay.Control.CurrentAddress
if inAdr == nil then return end

local retAdr = string.match(inAdr, '<iframe src="(.-)"')
if retAdr == nil then retAdr = inAdr end

retAdr = string.gsub(retAdr, 'https', 'http')
if not string.match(retAdr, 'http://vk%.') and not string.match(retAdr, 'http://vkontakte%.') then return end

m_LinkToPlay.Control.ChangeAddress = 'Yes'
m_LinkToPlay.Control.CurrentAddress = ''

---------------------------------------------------------------------------
local session = m_LinkToPlay.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_LinkToPlay.WinInet.SetOptionInt(session, 2, 3000, 0)
--INTERNET_OPTION_RECEIVE_TIMEOUT
m_LinkToPlay.WinInet.SetOptionInt(session, 6, 5000, 0)
---------------------------------------------------------------------------------------

--[[
host = 'http://login.vk.com'
body = '_origin=http://vk.com&act=login&email=' .. login .. '&pass=' .. password
body = escape(body)
header =
	'Host: login.vk.com\n' ..
	'Accept: text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/webp, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1\n' ..
	'Accept-Language: ru-RU,ru;q=0.9,en;q=0.8\n' ..
	'Referer:' .. host ..'\n' ..
	'Content-Type: application/x-www-form-urlencoded'
local rc,answer = m_simpleTV.WinInet.Post(session, host, nil, header, body)
--debug_in_file(answer .. "\n\n\n", 'C:\\Url.txt', true)
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\n", 'C:\\Url.txt', true)
local rc, answer = m_LinkToPlay.WinInet.Get(session, retAdr)
if rc ~= 200 then
	m_LinkToPlay.WinInet.Close(session)
	--m_simpleTV.OSD.ShowMessage("Connection error - " .. rc, 255, 10)
	return
end
--debug_in_file(answer .. "\n\n\n", nil, true)


local header = findpattern(answer, '<title>(.-)</title>', 1, 7, 8)
if string.match(header, 'ВКонтакте %| Video Ext') then
	header = string.match(answer, "var video_title = .-'(.-)';")
	if header ~= nil then
		header = unescape(header)
		header = RuStr(header, true)
	end
end

if header == nil then header = retAdr end
header = string.gsub(header, '&quot;', '"')
header = string.gsub(header, '&raquo;', '')
header = string.gsub(header, '&#39;', "'")
header = string.gsub(header, '&#33;', "!")
header = string.gsub(header, '&amp;', "&")
header = string.gsub(header, '|', "-")

if m_LinkToPlay.Control.CurrentTitle ~= nil then m_LinkToPlay.Control.CurrentTitle = header end

local content = findpattern(answer, '<param name="flashvars" value="(.-)"', 1, 31, 1)

if content ~= nil then
	--debug_in_file(content .. "\n\n\n", 'C:\\Url.txt', true)
	local t, i = {}, 1
	for name, link in string.gmatch(content, 'url(.-)=http://(.-)&amp;') do
		t[i]        = {}
		t[i].Id     =  i
		t[i].Name   = 'Качество ' .. name
		t[i].Address = 'http://' .. link
		i = i + 1
		--debug_in_file(name .. "   " .. link .. '\n\n', 'C:\\Url.txt')
	end
	if i > 2 then
		local ret = m_LinkToPlay.Taskbar.AddList(header, 0, t, 5000, 1)
		m_LinkToPlay.WinInet.Close(session)
		return
		--local _, id = m_simpleTV.OSD.ShowSelect(header, 0, t, 0, 1)
		--if id == nil then id = 1 end
		--retAdr = t[id].Address
	else
		if t[1] ~= nil then retAdr = t[1].Address end
	end
	m_LinkToPlay.WinInet.Close(session)
	if retAdr == nil then
		--m_simpleTV.OSD.ShowMessage("Ошибка - видео не найдено", 255, 10)
		return
	end
	m_LinkToPlay.Control.CurrentAddress = retAdr
	return
end
content = findpattern(answer,'nvar vars =(.-)};',1,11,1)
--debug_in_file(content .. "\n\n\n", 'C:\\Url.txt', true)
if content == nil then
	--debug_in_file('1)' .. retAdr .. "\n\n\n", nil, true)
	retAdr = vkcore.vk_getadress(retAdr)
	m_LinkToPlay.WinInet.Close(session)
	if retAdr ~= nil then m_LinkToPlay.Control.CurrentAddress = retAdr end
	return
end

--debug_in_file('1)' .. content .. "\n\n\n", nil, true)
content = string.gsub(content, '\\', '')
--debug_in_file(content .. "\n\n\n", 'C:\\Url.txt', true)
local t = json.decode(content)
if t == nil then
	--m_simpleTV.OSD.ShowMessage("Error - video is not available for playback",255,10)
	return
end

local host = 'http://vk.com/video_ext.php?'
local header = t.md_title
header = string.gsub(header, '&quot;', '"')
header = string.gsub(header, '&raquo;', '')
header = string.gsub(header, '&#39;', "'")
header = string.gsub(header, '&#33;', "!")
header = string.gsub(header, '&amp;', "&")
header = string.gsub(header, '|', "-")

if m_LinkToPlay.Control.CurrentTitle ~= nil then m_LinkToPlay.Control.CurrentTitle = header end
local VkAdr = host .. 'oid=' .. t.oid .. '&id=' .. t.vid .. '&hash=' .. t.hash2 .. '&hd=' .. t.hd

--debug_in_file('1)' .. content .. "\n\n\n", nil, true)
retAdr = VkAdr
m_LinkToPlay.WinInet.Close(session)
m_LinkToPlay.Control.PlayAddress(retAdr)