--script for sockshare.com (20/12/2013)
--open http://www.sockshare.com/file/AB0FB4E78AE5ED00 or http://www.sockshare.com/embed/AB0FB4E78AE5ED00

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://www%.sockshare%.com/' ) then return end

inAdr = string.gsub(inAdr, 'embed', 'file')

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


---------------------------------------------------------------------------
function sockshareTryConnect()

if  m_simpleTV.User == nil then m_simpleTV.User={} end
if  m_simpleTV.User.sockshare == nil then m_simpleTV.User.sockshare={} end 

if  m_simpleTV.User.sockshare.session == nil then 

  m_simpleTV.User.sockshare.session = m_simpleTV.WinInet.New("Opera/9.80 (Windows NT 6.1; U; ru) Presto/2.9.168 Version/11.52")
  if m_simpleTV.User.sockshare.session == nil then return false end
         
  --INTERNET_OPTION_CONNECT_TIMEOUT
  m_simpleTV.WinInet.SetOptionInt(m_simpleTV.User.sockshare.session,2,3000,0)
  --INTERNET_OPTION_RECEIVE_TIMEOUT
  m_simpleTV.WinInet.SetOptionInt(m_simpleTV.User.sockshare.session,6,5000,0)
end

return true

end
---------------------------------------------------------------------------
function sockshareOnError(Mess)
 
 if m_simpleTV.User.sockshare.session~=nil then 
     m_simpleTV.WinInet.Close(m_simpleTV.User.sockshare.session)
	 m_simpleTV.User.sockshare.session=nil
 end	 
 
 m_simpleTV.OSD.ShowMessage('Sockshare error: ' .. Mess,255,10)
 
end
---------------------------------------------------------------------------
---------------------------------------------------------------------------
 if sockshareTryConnect() ~= true then 
   sockshareOnError("Server error")
   return
 end

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

 if rc~=200 then
	   sockshareOnError("Connection error - " .. rc )
	   return
 end

local header =  findpattern( answer,'<title>(.-)</title>',1,7,8)
if header==nil then header=inAdr end

 header = string.gsub(header,'&quot;','"')
 header = string.gsub(header,'&raquo;','')

if m_simpleTV.Control.CurrentTitle ~= nil then m_simpleTV.Control.CurrentTitle=header end
---------------------------------------------------------------------------------
--restart
local fid = findpattern(answer,'playlist(.-),',1,11,2)

if fid~=nil then
	   local host = findpattern(inAdr,'http://(.-)/',1,0,1)
	   local url = host .. fid

rc,answer=m_simpleTV.WinInet.Get(m_simpleTV.User.sockshare.session,url)

 if rc~=200 then
	   sockshareOnError("Connection error - " .. rc)
	   return
 end

local retAdr = findpattern(answer,'url="(.-)"',1,5,1)
retAdr = string.gsub(retAdr,'&amp;','&')
retAdr = string.gsub(retAdr,"'",'')

if retAdr==nil then return end

m_simpleTV.Control.CurrentAdress = retAdr
	   return
 end
--------------------------------------------------------------------------
answer = findpattern(answer,'method="post"(.-)</form>',1,13,7)

 if answer==nil then
	   sockshareOnError("video is not available")
	   return
 end

local body,hash,confirm,host

hash = findpattern(answer,'value="(.-)"',1,7,1)
confirm = findpattern(answer,'submit" value="(.-)"',1,15,1)
body = 'hash=' .. hash .. '&confirm=' .. confirm

header = 
'Accept:	text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/webp, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1\n' ..
'Referer:' .. inAdr .. '\n' ..
'Content-Type:	application/x-www-form-urlencoded\n'

 rc,answer=m_simpleTV.WinInet.Post(m_simpleTV.User.sockshare.session,inAdr,nil,header,body)

 if rc~=200 then
	   sockshareOnError("Connection error - " .. rc )
	   return
 end

host = findpattern(inAdr,'http://(.-)/',1,0,1)
fid = findpattern(answer,'playlist(.-),',1,11,2)

 if fid==nil then
	   sockshareOnError("video is not available")
	   return
 end

local url = host .. fid

rc,answer=m_simpleTV.WinInet.Get(m_simpleTV.User.sockshare.session,url)

 if rc~=200 then
	   sockshareOnError("Connection error - " .. rc)
	   return
 end

local retAdr = findpattern(answer,'url="(.-)"',1,5,1)
retAdr = string.gsub(retAdr,'&amp;','&')
retAdr = string.gsub(retAdr,"'",'')
--debug_in_file(retAdr.. "\n\n")
if retAdr==nil then return end

m_simpleTV.Control.CurrentAdress = retAdr


