-- script for kinomaxpro.com (29/10/2013)

require('vkcore')
require('json')

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, 'kinomaxpro%.com') then return end

m_simpleTV.Control.ChangeAdress = 'Yes'
m_simpleTV.Control.CurrentAdress = ''

function RuStr(Str, isFrom)
	if Str == nil then return end
	if isFrom == true then
		return m_simpleTV.Common.string_fromUTF8(Str, 1251)
	else
		return m_simpleTV.Common.string_toUTF8(Str, 1251)
	end
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, 3000, 0)
--INTERNET_OPTION_RECEIVE_TIMEOUT
m_simpleTV.WinInet.SetOptionInt(session, 6, 8000, 0)
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

answer = RuStr(answer, true)
--debug_in_file(answer .. '\n', 'C:\\tmp.txt', true)

header = findpattern(answer, '<title>(.-)</title>', 1, 7, 8)
header = string.gsub(header, '&raquo;', '')
if header == nil then header = inAdr end
if m_simpleTV.Control.CurrentTitle ~= nil then m_simpleTV.Control.CurrentTitle = header end

------------------------------------------------------------------------------------------------------------------
local function GetAddress(tmp)
	local tmp = string.gsub(tmp, 'iframe', 'manifest.f4m')
	local rc,answer=m_simpleTV.WinInet.Get(session,tmp)
	if rc~=200 then
		m_simpleTV.WinInet.Close(session)
		m_simpleTV.OSD.ShowMessage("Connection error - " .. rc ,255,10)
		return
	end
	tmp = findpattern(answer,'url="(.-)"',1,5,1)
	tmp = string.gsub(tmp, 'hds/', '')
	retAdr = tmp .. 'index.m3u8'
	return retAdr
end
------------------------------------------------------------------------------------------------------------------

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

--<iframe src="https://player.vimeo.com/video/128551871"
for w in string.gmatch(answer, '<iframe(.-)</iframe>') do
	adr = findpattern (w, '%b""', 1, 1, 1)
	--debug_in_file(adr .. '\n', 'C:\\tmp.txt')
	if (adr ~= nil) and string.match(adr, 'player%.vimeo%.com') then
		t[i]        =  {}
		t[i].Id     =  i
		t[i].Name   = 'Вариант ' .. i
		t[i].Adress = adr
		i = i + 1
	end
end
if i > 2 then
	local _, id = m_simpleTV.OSD.ShowSelect(header, 0, t, 0, 1)
	if id == nil then id = 1 end
	retAdr = t[id].Adress
else
	retAdr = t[1].Adress
	--debug_in_file(retAdr .. '\n', 'C:\\tmp.txt', true)
	if string.match(retAdr, 'player%.vimeo%.com') then
		if string.match(retAdr, '^https://') then
			m_simpleTV.WinInet.SetOpenRequestFlags(session, 0x00800000 + 0x00002000 + 0x00001000)
			header = 'Referer: ' .. inAdr .. '\n'
			rc, answer = m_simpleTV.WinInet.Get(session, retAdr, nil, 0, header)
			if rc ~= 200 then
				m_simpleTV.WinInet.Close(session)
				m_simpleTV.OSD.ShowMessage("Connection error - " .. rc, 255, 10)
				return
			end
			tmp = findpattern(answer, '<script>(.-)</script>', 1, 8, 9)
			if tmp == nil then return end
			--Эта строчка меняется
			tmp = findpattern(tmp, 'function%(e,a%)%{var t=(.-);if%(!t%.request%)', 1, 20, 15)
			--debug_in_file(tmp .. '\n', 'C:\\tmp.txt', true)
			local tab = json.decode(tmp)
			if (tab == nil) or (tab.request == nil) or (tab.request.files == nil) then
				m_simpleTV.OSD.ShowMessage("Error - video is not available for playback",255,10)
				return
			end
			--debug_in_file(tmp .. '\n', 'C:\\kinomaxpro.txt', true)
			local t, i, n = {}, 1, 0
			retAdr = ''
			tab = tab.request.files
			local f = { [0] = 'mobile', [1] = 'hd', [2] = 'sd' }
			while true do
				local s = f[n]
				if tab.h264[s] == nil then break end
				t[i] = {}
				t[i].Id = i
				t[i].Name = s
				t[i].Adress = tab.h264[s].url
				--debug_in_file(t[i].Adress .. '\n')
				if retAdr == '' then retAdr = t[i].Adress end
				i = i + 1
				n = n + 1
			end
			if i > 2 then
				local _, id = m_simpleTV.OSD.ShowSelect(header, 0, t, 0, 1)
				if id == nil then id = 1 end
				retAdr = t[id].Adress
			end
		end
	end
	if string.match(retAdr, 'vk.com' ) then
		retAdr = vkcore.vk_getadress(retAdr)
		if retAdr == nil then
			m_simpleTV.OSD.ShowMessage("Ошибка - Видео c VK не найдено",255,2)
			return
		end
	end
	if string.match(retAdr, 'moonwalk.cc' ) then
		retAdr = GetAddress(retAdr)
		if retAdr == nil then
			m_simpleTV.OSD.ShowMessage("Ошибка - Видео c moonwalk не найдено",255,2)
			return
		end
	end
	if string.match(retAdr, 'gidtv.cc' ) then 
		rc, answer = m_simpleTV.WinInet.Get(session, retAdr)
		if rc~=200 then
			m_simpleTV.WinInet.Close(session)
			m_simpleTV.OSD.ShowMessage("Connection error - " .. rc ,255,10)
			return
		end
		tmp = findpattern (answer,'function setPlayer(.-)function set_pause_ad',1,0,0)
		retAdr = findpattern (tmp,'setFlash(.-),',1,10,2)
		retAdr = string.gsub(retAdr,'manifest.f4m','playlist.m3u8')
		if retAdr == nil then
			m_simpleTV.OSD.ShowMessage("Ошибка - Видео c gidtv не найдено",255,2)
			return
		end
	end
end
if retAdr == nil then
	m_simpleTV.OSD.ShowMessage("Ошибка - видео не найдено",255,10)
	return
end
m_simpleTV.WinInet.Close(session)
m_simpleTV.Control.PlayAddress(retAdr)