--script for yaske.net (19/12/2013)
-- support 'vk.com', 'putlocker.com', 'sockshare.com', 'nowvideo', 'moevideo.net', 'youtube.com', 'allmyvideos', 'played.to'

require 'vkcoreAlt'

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, 'yaske%.net/es' ) then return end

m_simpleTV.Control.ChangeAdress='Yes'
m_simpleTV.Control.CurrentAdress = ''
--debug_in_file('in ' .. inAdr .. '\n')
---------------------------------------------------------------------------

 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,10000,0)
  --INTERNET_OPTION_RECEIVE_TIMEOUT
  m_simpleTV.WinInet.SetOptionInt(session,6,10000,0)
-----------------------------------------------------------------------------------
local function TestAddress(AdrForTest)
 local tt = {'vk.com', 'putlocker.com', 'sockshare.com', 'nowvideo', 'moevideo.net', 'youtube.com', 'allmyvideos', 'played.to'} 

  for k,v in ipairs( tt) do
     if string.match(AdrForTest , v) then return true end 
  end

 return false
end
-----------------------------------------------------------------------------------
local function GetAddress(inAdr)

 local rc,answer=m_simpleTV.WinInet.Get(session,inAdr)

 if rc~=200 then
	   m_simpleTV.WinInet.Close(session)
	   m_simpleTV.OSD.ShowMessage_UTF8("Connection error - " .. rc ,255,10)
	   return
 end
answer = findpattern(answer,'<div class="nocontent"(.-)</div>',1,0,0)
if answer == nil then return end
local tmp = findpattern(answer,'<iframe src="(.-)iframe>',1,0,0)
if tmp == nil then return end

local adr = findpattern(tmp,'src="(.-)"',1,5,1)
	 if adr == nil then
		  m_simpleTV.OSD.ShowMessage_UTF8("Error - can't find address",255,10)
		  return
	  end
if string.match(adr,'putlocker') then
   rc,answer=m_simpleTV.WinInet.Get(session,adr)

 if rc~=200 then
	   m_simpleTV.WinInet.Close(session)
	   m_simpleTV.OSD.ShowMessage_UTF8("Connection error - " .. rc ,255,10)
	   return
 end
 adr = findpattern(answer,'href="(.-)"',1,6,1)
	 if adr == nil then
		  m_simpleTV.OSD.ShowMessage_UTF8("Error - can't find address",255,10)
		  return
	  end
end

if string.match(adr,'allmyvideos') then
   rc,answer=m_simpleTV.WinInet.Get(session,adr)

 if rc~=200 then
	   m_simpleTV.WinInet.Close(session)
	   m_simpleTV.OSD.ShowMessage_UTF8("Connection error - " .. rc ,255,10)
	   return
 end
 adr = findpattern(answer,'href="(.-)"',1,6,1)
	 if adr == nil then
		  m_simpleTV.OSD.ShowMessage_UTF8("Error - can't find address",255,10)
		  return
	  end
end

if string.match(adr,'played%.to') then
   rc,answer=m_simpleTV.WinInet.Get(session,adr)

 if rc~=200 then
	   m_simpleTV.WinInet.Close(session)
	   m_simpleTV.OSD.ShowMessage_UTF8("Connection error - " .. rc ,255,10)
	   return
 end
 adr = findpattern(answer,'href="(.-)"',1,6,1)
	 if adr == nil then
		  m_simpleTV.OSD.ShowMessage_UTF8("Error - can't find address",255,10)
		  return
	  end
end

if string.match(adr,'nowvideo') then

 	local rc,answer=m_simpleTV.WinInet.Get(session,adr)

 	if rc~=200 then
	   m_simpleTV.WinInet.Close(session)
	   m_simpleTV.OSD.ShowMessage_UTF8("Connection error - " .. rc ,255,10)
	   return
 	end
   adr = findpattern(answer,'flashvars%.advURL="(.-)"',1,18,1)
	if adr == nil then return end
end

if string.match(adr,'sockshare.') then
   adr = string.gsub(adr,'/embed/','/file/')
end
if string.match(adr,'moevideo.net') then
   adr = string.gsub(adr,'frame','')
   adr = string.gsub(adr,'?(.+)','')
end
if string.match(adr,'youtube.com') then
   adr = string.gsub(adr,'/embed/','/watch?v=')
end
		return adr
	
end
-----------------------------------------------------------------------------------
local function GetRealAddress(inAdr)

if string.match(inAdr,'vk%.com') then
    retAdr = vkcoreAlt.vk_getadress(inAdr)
	 if retAdr == nil then
		  m_simpleTV.OSD.ShowMessage_UTF8("Error - video is not available for playback",255,10)
		  return
	  end
end
if string.match(inAdr,'played%.to') then
   m_simpleTV.Control.ChangeAdress='No'
   m_simpleTV.Control.CurrentAdress = inAdr
   dofile(m_simpleTV.MainScriptDir .. "user\\video\\played.lua") 
   m_simpleTV.Control.ChangeAdress='Yes'
   retAdr = m_simpleTV.Control.CurrentAdress
   m_simpleTV.Control.CurrentAdress='' 
end
if string.match(inAdr,'allmyvideos%.net') then
   m_simpleTV.Control.ChangeAdress='No'
   m_simpleTV.Control.CurrentAdress = inAdr
   dofile(m_simpleTV.MainScriptDir .. "user\\video\\allmyvideos.lua") 
   m_simpleTV.Control.ChangeAdress='Yes'
   retAdr = m_simpleTV.Control.CurrentAdress
   m_simpleTV.Control.CurrentAdress='' 
end
if string.match(inAdr,'putlocker%.com') then
   m_simpleTV.Control.ChangeAdress='No'
   m_simpleTV.Control.CurrentAdress = inAdr
   dofile(m_simpleTV.MainScriptDir .. "user\\video\\putlocker.lua") 
   m_simpleTV.Control.ChangeAdress='Yes'
   retAdr = m_simpleTV.Control.CurrentAdress
   m_simpleTV.Control.CurrentAdress='' 
end
if string.match(inAdr,'sockshare%.com') then
   m_simpleTV.Control.ChangeAdress='No'
   m_simpleTV.Control.CurrentAdress = inAdr
   dofile(m_simpleTV.MainScriptDir .. "user\\video\\sockshare.lua") 
   m_simpleTV.Control.ChangeAdress='Yes'
   retAdr = m_simpleTV.Control.CurrentAdress
   m_simpleTV.Control.CurrentAdress=''  
end
if string.match(inAdr,'moevideo%.net') then
   m_simpleTV.Control.ChangeAdress='No'
   m_simpleTV.Control.CurrentAdress = inAdr
   dofile(m_simpleTV.MainScriptDir .. "user\\video\\moevideo.lua") 
   m_simpleTV.Control.ChangeAdress='Yes'
   retAdr = m_simpleTV.Control.CurrentAdress
   m_simpleTV.Control.CurrentAdress=''
end
if string.match(inAdr,'nowvideo') then
   m_simpleTV.Control.ChangeAdress='No'
   m_simpleTV.Control.CurrentAdress = inAdr
   dofile(m_simpleTV.MainScriptDir .. "user\\video\\nowvideo.lua") 
   m_simpleTV.Control.ChangeAdress='Yes'
   retAdr = m_simpleTV.Control.CurrentAdress
   m_simpleTV.Control.CurrentAdress='' 
end
if string.match(inAdr,'youtube%.com') then
   m_simpleTV.Control.ChangeAdress='No'
   m_simpleTV.Control.CurrentAdress = inAdr
   dofile(m_simpleTV.MainScriptDir .. "user\\video\\youtube.lua") 
   m_simpleTV.Control.ChangeAdress='Yes'
   retAdr = m_simpleTV.Control.CurrentAdress
   m_simpleTV.Control.CurrentAdress=''
end


	return retAdr
end
-----------------------------------------------------------------------------------


 local rc,answer=m_simpleTV.WinInet.Get(session,inAdr)

 if rc~=200 then
	   m_simpleTV.WinInet.Close(session)
	   m_simpleTV.OSD.ShowMessage_UTF8("Connection error - " .. rc ,255,10)
	   return
 end

local Mheader =  findpattern( answer,'<title>(.-)</title>',1,7,8)
Mheader = m_simpleTV.Common.string_toUTF8(Mheader,65001)
if Mheader==nil then Mheader=inAdr end
if m_simpleTV.Control.CurrentTitle ~= nil then m_simpleTV.Control.CurrentTitle=Mheader end

answer = findpattern(answer,'<a class="btn btn%-mini enlace_link"(.-)</tbody>',1,0,0)
--debug_in_file(answer .. '\n')

if answer~=nil then
local i=1
local t={}
local name,adr,wa,wb,wc,wd
local retAdr = ''
for w in string.gmatch(answer,'enlace_link(.-)star_rating') do

	wa = findpattern (w,'<b>(.-)</b>',1,3,4)
	if wa == nil then wa ='' end
	wb = findpattern (w,'google(.-)/td>',1,0,0)
	if wb ~=nil then
	  wb = findpattern (wb,'%b><',1,1,1)
	end
	wc = findpattern (w,'images/flags(.-)/td>',1,0,0)
	if wc ~= nil then 
	  wc = findpattern (wc,'%b><',1,1,1)
	end
	wd = findpattern (w,'style="text%-transform:capitalize;">(.-)<',1,35,1)
	if wd == nil then wd ='' end
	name = wa .. ', Servidor: ' .. wb .. ', Audio: ' .. wc .. ', Formato: ' .. wd
	name = string.gsub (name, '&nbsp;','')
	adr = findpattern (w,'href="(.-)"',1,6,1)
	if adr == nil then break end

local CheckAdr = GetAddress(adr)
--debug_in_file(CheckAdr .. ' \n')
	if TestAddress(CheckAdr) then

  		   t[i] = {}
  		   t[i].Id   =  i
  		   t[i].Name = m_simpleTV.Common.string_toUTF8(name,65001)
  		   t[i].Adress  = CheckAdr

--	debug_in_file(t[i].Name .. '\n' .. t[i].Adress .. '\n')

		i=i+1
	end
end

 if i>2 then
    m_simpleTV.OSD.ShowSelect_UTF8(Mheader,0,t,5000)
 end

 if i>1 then
   retAdr = GetRealAddress(t[1].Adress)
 end
  else 
     retAdr = GetAddress(inAdr)
     retAdr = GetRealAddress(retAdr)
 end

m_simpleTV.WinInet.Close(session)
m_simpleTV.Control.CurrentAdress = retAdr




