--script for onlinemultfilmy.ru (29/01/2014) 

require 'vkcore'
require 'youtubecore'
require('uppod')

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://onlinemultfilmy%.ru' ) 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, 3000, 0)
--INTERNET_OPTION_RECEIVE_TIMEOUT
m_simpleTV.WinInet.SetOptionInt(session, 6, 5000, 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 = m_simpleTV.Common.string_fromUTF8(answer)
debug_in_file(answer, nil, true)

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

local _retAdr
--namba.net-------------------------------------------------------------------------------------------------------
retAdr = findpattern(answer,'<param value="config=(.-)"', 1, 21, 1)
if retAdr ~= nil then
	if string.find(retAdr, 'namba.net') ~= nil then
		local _, id = string.match(retAdr, 'flashvars%-3%.2%.11%.php%?i=(.-)__(.-)_')
		if id then retAdr = 'http://video.mobile.namba.net/view.php?id=' .. id end
		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
		answer = findpattern(answer, '<td class="video%-download%-link"(.-)</td>', 1, 0, 0)
		if answer then retAdr = string.match(answer, 'href="(.-)"') end
		if retAdr then
			m_simpleTV.WinInet.Close(session)
			m_simpleTV.Control.CurrentAdress = retAdr
		end
		--debug_in_file(retAdr .. '\n')
		return
	end
end

--youtube.com, vk.com, enter.az, sibnet.ru------------------------------------------------------------------------
retAdr = findpattern(answer,'<iframe id="ifr"(.-)</iframe>', 1, 0, 0)
if retAdr ~= nil then
	retAdr = findpattern(retAdr, 'src="(.-)"', 1, 5, 1)
	if string.find(retAdr, 'youtube.com') ~= nil then
		_retAdr = string.match(retAdr, 'list=(.+)')
		if _retAdr then
			retAdr = 'http://www.youtube.com/playlist?list=' .. _retAdr
			--debug_in_file(retAdr .. '\n')
		else
			_retAdr = string.match(retAdr, '/embed/(.+)')
			if _retAdr then retAdr = "http://www.youtube.com/watch/" .. _retAdr end
		end
		
	end
	if string.find(retAdr, 'vk.com') ~= nil then
		retAdr = string.gsub(retAdr, '%&%#038;', '%&')
		--retAdr = retAdr .. '&hd=1'
		--debug_in_file('1 - ' .. retAdr .. '\n')
		if retAdr ~= nil then retAdr = vkcore.vk_getadress(retAdr) end
		retAdr = m_simpleTV.Common.string_fromUTF8(retAdr)
		--debug_in_file(retAdr .. '\n')
	end	
	m_simpleTV.WinInet.Close(session)
	m_simpleTV.Control.PlayAddress(retAdr)
	return
end

--vk.com----------------------------------------------------------------------------------------------------------
if retAdr == nil then
	retAdr = findpattern(answer,'<iframe class="ifram"(.-)</iframe>', 1, 0, 0)
	if retAdr ~= nil then
		retAdr = findpattern(retAdr, 'src="(.-)"', 1, 5, 1)
		--debug_in_file('2 - ' .. retAdr .. '\n')
		retAdr = vkcore.vk_getadress(retAdr)
		--debug_in_file('5 - ' .. retAdr .. '\n')
	end
end

--youtube.com è vk.com--------------------------------------------------------------------------------------------
if retAdr == nil then
	local list_1, list_2 = string.match(answer, '"SERIES_LIST": %[\n%[(.-)%],%[(.-)%]	]')
	if (list_1 == nil) and (list_2 == nil) then
		list_1, list_2 = string.match(answer, '"SERIES_LIST": %[\n(.-),%[(.-)%]	]')
		--debug_in_file(list_1 .. '\n\n'  .. list_2 .. '\n')
	end
	if list_1 ~= nil then
		local adr_1 = findpattern (list_1, '%b""', 1, 1, 1)
		local adr_2 = findpattern (list_2, '%b""', 1, 1, 1)
		local name_1, name_2, title_1
		local t, i = {}, 1
		if adr_1 == '|||Ïëååð ¹1' then
			name_1, list_1, title_1 = string.match(list_1, '"|||(.-)",(.-)|(.-)"')
			list_1 = list_1 .. '"'
		else
			_list_1, title_1 = string.match(list_1, '(.-)|(.+)')
			if _list_1 ~= nil then list_1 = _list_1 .. '"' end
			name_1 = string.match(adr_1, '//(.-)/')
		end
		--debug_in_file(list_1 .. '\n\n'  .. name_1 .. '\n')
		t[i] = {}
		t[i].Id     = i
		t[i].Name   = name_1
		t[i].Adress = list_1
		t[i].Title  = title_1
		i = i + 1
		if adr_2 == '|||Ïëååð ¹2' then
			name_2, list_2 = string.match(list_2, '"|||(.-)",(.+)')
		else
			name_2 = string.match(adr_2, '//(.-)/')
		end
		t[i] = {}
		t[i].Id     = i
		t[i].Name   = name_2
		t[i].Adress = list_2
		i = i + 1
		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
			local title = t[id].Title
			t, i = {}, 1
			for w in string.gmatch(retAdr, '%b""') do
				adr = findpattern (w, '%b""', 1, 1, 1)
				if adr == nil then break end
				if string.find(adr, 'youtube.com') ~= nil then
					_adr = string.match(adr, 'list=(.+)')
					if _adr ~= nil then
						adr = 'http://www.youtube.com/playlist?list=' .. _adr
					else
						_adr = string.match(adr, 'www%.youtube%.com/embed/(.-)?feature=player_')
						if _adr ~= nil then adr = 'http://www.youtube.com/watch/' .. _adr end
					end
				end
				if title then 
					name = title
				else
					name = 'Cåðèÿ ' .. i
				end
				t[i] = {}
				t[i].Id     = i
				t[i].Name   = name
				t[i].Adress = adr
				i = i + 1
			end
			if i > 2 then
				_, 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
			end
		end
	else
		list_1 = string.match(answer, '"SERIES_LIST": %[\n%[(.-)%]')
		debug_in_file('\n' .. list_1 .. '\n\n', nil, true)
		if list_1 == nil then return end
		local t, i = {}, 1
		for w in string.gmatch(list_1, '%b""') do
			--debug_in_file(w .. '\n')
			local adr, title = string.match(w, '"//(.-)|(.-)"', 1, 3, 1)
			if adr == nil then break end
			debug_in_file(adr .. ' = ' .. title .. '\n')
			if string.find(adr, 'youtube.com') ~= nil then
				local _adr = string.match(adr, 'list=(.+)')
				if _adr ~= nil then
					adr = 'http://www.youtube.com/playlist?list=' .. _adr
				else
					_adr = string.match(adr, 'www%.youtube%.com/embed/(.-)?feature=player_detailpage')
					if _adr ~= nil then adr = 'http://www.youtube.com/watch/' .. _adr end
				end
			end
			if title then 
				name = title
			else
				name = 'Cåðèÿ ' .. i
			end
			t[i] = {}
			t[i].Id     = i
			t[i].Name   = name
			t[i].Adress = adr
			i = i + 1
		end
		if i > 2 then
			_, 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
		end
	end
	if retAdr == nil then retAdr = '' end
	--debug_in_file('3 - ' .. retAdr .. '\n')
	m_simpleTV.WinInet.Close(session)
	m_simpleTV.Control.PlayAddress(retAdr)
	return
end

--youtube.com, vk.com---------------------------------------------------------------------------------------------
if retAdr == nil then
	answer = findpattern(answer, '<noindex>(.-)</noindex>', 1, 0, 0)
	local t, i = {}, 1
	local adr
	for link, name in string.gmatch(answer, '<option value="(.-)">(.-)</option>') do
		t[i] = {}
		t[i].Id     = i
		t[i].Name   = string.gsub(name, '%&%#8212;', '-')
		t[i].Adress = string.gsub(link, 'embed', 'watch')
		i = i + 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
m_simpleTV.WinInet.Close(session)
m_simpleTV.Control.CurrentTitle = header
m_simpleTV.Control.CurrentAdress = retAdr