--get adress

require('json')
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%://filmix%.net') then return end
m_simpleTV.Control.ChangeAdress = 'Yes'
m_simpleTV.Control.CurrentAdress = ''
local hash11 = {"l","u","T","D","Q","H","0","3","G","1","f","M","p","U","a","I","6","k","d","s","b","W","5","e","y","="}
local hash22 = {"w","g","i","Z","c","R","z","v","x","n","N","2","8","J","X","t","9","V","7","4","B","m","Y","o","L","h"}
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)
--debug_in_file(answer)
if rc ~= 200 then
	m_simpleTV.WinInet.Close(session)
	return
end
local header = findpattern( answer, '<title>(.-)</title>', 1, 7, 8)
--header = m_simpleTV.Common.string_fromUTF8(header)
header = string.gsub(header, '&quot;', '"')
header = string.gsub(header, '&raquo;', '')
header = string.gsub(header, '&#39;', "'")
header = string.gsub(header, '&amp;', "&")
header = string.gsub(header, '&#8230;', "")
if header == nil then header = inAdr end
header = string.gsub(header, '&quot;', '"')
if m_simpleTV.Control.CurrentTitle ~= nil then m_simpleTV.Control.CurrentTitle = header end
local file = false
local pls = false
debug_in_file(answer .. '\n\n', 'C:\\_TEMP\\filmix.txt', true)
local retAdr = findpattern(answer, 'HTML5 PLAYER(.-)HTML5 PLAYER', 1, 15, 15)
if string.match(retAdr, "plArray = cleanArray%(%['','','','',''%]%),") then
	--岳伤
	retAdr = findpattern(retAdr, "'(.-)'", 1, 1, 1)
	retAdr = uppod.DecodeUppod(retAdr, hash11, hash22)
	--debug_in_file(retAdr .. '\n\n', 'C:\\_TEMP\\filmix.txt', true)
	local quality = findpattern(retAdr, "%[(.-)%]", 1, 1, 1) .. ','
	local t, i = {}, 1
	for q in string.gmatch(quality, '(.-),') do
		--debug_in_file(q .. '\n', 'C:\\_TEMP\\filmix.txt')
		if q ~= '' then 
			t[i]        = {}
			t[i].Id     = i
			t[i].Name   = q
			t[i].Adress = string.gsub(retAdr, '%b\[\]', q)
			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
	end
	m_simpleTV.Control.CurrentAdress = retAdr
	--debug_in_file(retAdr, 'C:\\_TEMP\\filmix.txt', true)
	return
elseif string.match(retAdr, "fileArray = cleanArray%(%['','','','',''%]%),") then
	--纤派巳岩
	pls = true
end
if pls == false then retAdr = findpattern(answer, 'file=(.-)&amp;', 1, 5, 5) end
if (retAdr ~= nil) and (pls == false) then
	--岳伤
	file = true
else
	--纤派巳岩
	retAdr = findpattern(retAdr, "plname = '(.-)'", 1, 10, 1)
	if retAdr == nil then retAdr = findpattern(answer, 'pl=(.-)&amp;', 1, 3, 5) end
	debug_in_file(retAdr, 'C:\\_TEMP\\filmix.txt', true)
end
if retAdr == nil then
	--debug_in_file(answer, 'C:\\_TEMP\\filmix.txt', true)
	m_simpleTV.OSD.ShowMessage("Error filmix.net - can't find address", 255, 10)
	return
end
retAdr = uppod.DecodeUppod(retAdr, hash11, hash22)
--debug_in_file(retAdr .. "\n\n\n", 'C:\\_TEMP\\filmix.txt', true)
if file == false then
	rc, answer = m_simpleTV.WinInet.Get(session, retAdr)
	if rc ~= 200 then
		m_simpleTV.WinInet.Close(session)
		return
	end
	if pls == true then answer = uppod.DecodeUppod(answer, hash11, hash22) end
	answer = m_simpleTV.Common.string_fromUTF8(answer)
	answer = string.gsub(answer, '\n', ' ')
	answer = string.gsub(answer, '\r', '')
	m_simpleTV.WinInet.Close(session)
	local tab = json.decode(answer)
	debug_in_file(answer .. "\n\n\n", 'C:\\_TEMP\\filmix.txt', true)
	if tab == nil or tab.playlist == nil then
		m_simpleTV.OSD.ShowMessage("Error filmix.net - can't find address", 255, 10)
		return
	end
	local a, n, k, l = {}, 1
	retAdr = ''
	for i = 1, #tab.playlist, 1 do
		local t = tab.playlist
		local isfile
		if t[i].file ~= nil then
			k = 1
			isfile = true
		else
			if t[i].playlist == nil then break end
			t = t[i].playlist
			k = #t
			isfile = false
		end
		--debug_in_file(#t .. '\n', 'C:\\_TEMP\\filmix.txt', true)
		for j = 1, k, 1 do
			a[n]      = {}
			a[n].Id   = n
			if isfile == true then
				l = i
			elseif isfile == false then l = j end
			a[n].Name = t[l].comment
			a[n].Adress = t[l].file
			--debug_in_file(a[n].Name .. '\n' .. a[n].Adress .. '\n\n', 'C:\\_TEMP\\debug.txt')
			n = n + 1
		end
		--if isBreak == true then break end
	end
	if #a > 1 then
		local _, id = m_simpleTV.OSD.ShowSelect(header, 0, a, 0, 1)
		if id == nil then id = 1 end
		retAdr = a[id].Adress
	else
		retAdr = a[1].Adress
	end
end
if retAdr == nil then return end
--世着岩挛
local s = findpattern(retAdr, '_%[(.-)%]', 1, 2, 1)
if s ~= nil then
	s = s .. ','
	local a, i = {}, 1
	for q in string.gmatch(s, '(.-),') do
		if q ~= '' then
			a[i]      = {}
			a[i].Id   = i
			a[i].Name = q
			a[i].Adress = string.gsub(retAdr, '%b\[\]', q)
			--debug_in_file(a[i].Name .. '\n' .. a[i].Adress .. '\n\n', 'C:\\_TEMP\\debug.txt')
			i = i + 1
		end
		--debug_in_file(q .. '\n\n', 'C:\\_TEMP\\debug.txt')
	end
	if #a > 1 then
		local _, id = m_simpleTV.OSD.ShowSelect(header, 0, a, 0, 1)
		if id == nil then id = 1 end
		retAdr = a[id].Adress
	else
		retAdr = a[1].Adress
	end
end
m_simpleTV.Control.CurrentAdress = retAdr