--script for streamcloudcore.eu (06/03/2013)
local base = _G
module ('streamcloudcore')

---------------------------------------------------------------------------
function StreamcloudTryConnect()

if  base.m_simpleTV.User == nil then base.m_simpleTV.User={} end
if  base.m_simpleTV.User.Streamcloud == nil then base.m_simpleTV.User.Streamcloud={} end 

if  base.m_simpleTV.User.Streamcloud.session == nil then 

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

return true

end
---------------------------------------------------------------------------
function StreamcloudOnError(Mess)
 
 if base.m_simpleTV.User.Streamcloud.session~=nil then 
     base.m_simpleTV.WinInet.Close(base.m_simpleTV.User.Streamcloud.session)
	 base.m_simpleTV.User.Streamcloud.session=nil
 end	 
 
 base.m_simpleTV.OSD.ShowMessage('Streamcloud error: ' .. Mess,255,10)
 
end
---------------------------------------------------------------------------
function StreamcloudGetAddress(inAdr)

 if StreamcloudTryConnect() ~= true then 
   StreamcloudOnError("Server error")
   return
 end

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

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

local body,host,id,fname
id = base.findpattern( answer,'name="id" value="(.-)"',1,17,1)

--if id==nil then return end

fname = base.findpattern( answer,'name="fname" value="(.-)"',1,20,1)

if fname==nil then
local retAdr = base.findpattern( answer,'file:(.-),',1,7,2)
if retAdr==nil then return end
	   return retAdr
end
host = inAdr
body = 'op=download1&usr_login=&id=' .. id .. '&fname=' .. fname .. '&referer=&hash=&imhuman=Watch+video+now'

header = 
'Host: streamcloud.eu\n' ..
'Accept-Language: ru-RU,ru;q=0.9,en;q=0.8\n' ..
'Referer: ' .. inAdr .. '\n' ..
'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' ..
'Content-Type: application/x-www-form-urlencoded'

rc,answer=base.m_simpleTV.WinInet.Post(base.m_simpleTV.User.Streamcloud.session,host,nil,header,body)

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

local retAdr = base.findpattern( answer,'file:(.-),',1,7,2)
if retAdr==nil then return end

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