--script for shurweb.es (11/04/2013)
--get adress
require 'vkcoreAlt'

local retAdr

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

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

---------------------------------------------------------------------------
 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)
   --INTERNET_OPTION_END_BROWSER_SESSION
  m_simpleTV.WinInet.SetOptionInt(session,42,0,0)
-----------------------------------------------------------------------------------
local function GetAddressVK(inAdr)

 --m_simpleTV.Common.Wait(2000)
 debug_in_file('2' .. inAdr .. '\n\n')
 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 = findpattern(answer,'<div class="single_post">(.-)iframe>',1,0,0)

retAdr = findpattern(answer,'src="(.-)"',1,5,1)
retAdr = vkcoreAlt.vk_getadress(retAdr)

	return retAdr
end
-----------------------------------------------------------------------------------
 debug_in_file('1' .. inAdr .. '\n')
while true do 
 local sss = m_simpleTV.WinInet.GetCookies(session,'http://www.shurweb.es/','')
 if sss~=nil then
	--debug_in_file(sss .. '\n\n')
	
	debug_in_file(sss .. '\n')
	local aa = findpattern(sss,"(.-)=",1,0,1)
	m_simpleTV.WinInet.SetCookies(session,'http://www.shurweb.es/','',aa .. '=1')
	
	m_simpleTV.WinInet.Close(session)
	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)
    --INTERNET_OPTION_END_BROWSER_SESSION
    m_simpleTV.WinInet.SetOptionInt(session,42,0,0)
  else break 	
 end	
end 
 local rc,answer=m_simpleTV.WinInet.Get(session,inAdr) -- .. '?p=' .. math.random())

 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')
 answer = m_simpleTV.Common.string_fromUTF8(answer)
 --debug_in_file(answer .. '\n')
 --do return end
local Mheader =  findpattern( answer,'<title>(.-)</title>',1,7,8)
if Mheader==nil then Mheader=inAdr end

if m_simpleTV.Control.CurrentTitle ~= nil then m_simpleTV.Control.CurrentTitle=Mheader end

answer = findpattern(answer,'<div class="season_title"(.-)<div id="sidebar">',1,0,0)

if answer==nil then 
	retAdr = GetAddressVK(inAdr)
	--answer = findpattern(answer,'<div class="single_post">(.-)iframe>',1,0,0)
	--retAdr = findpattern(answer,'src="(.-)"',1,5,1)
	--retAdr = vkcoreAlt.vk_getadress(retAdr)
	m_simpleTV.Control.CurrentAdress = retAdr
  return 
end

local i=1
local t={}
local name,adr
for w in string.gmatch(answer,'<div class="video">(.-)</div>') do

	name = findpattern (w,'%b><',1,1,1)
	adr = findpattern (w,'href="(.-)"',1,6,1)
    	if adr == nil then break end

     t[i] = {}
     t[i].Id   =  i
     t[i].Name = name
     t[i].Adress  = adr
--debug_in_file(t[i].Adress .. '\n')
     i=i+1
 end

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

 if i >1 then
      retAdr = GetAddressVK(t[1].Adress)
	 if retAdr == nil then
		  m_simpleTV.OSD.ShowMessage("Error - video is not available for playback",255,10)
		  retAdr=''
	  end
  end

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


