local base = _G
module("ecocore")
function EcostreamTryConnect()
  if base.m_simpleTV.User == nil then
    base.m_simpleTV.User = {}
  end
  if base.m_simpleTV.User.Ecostream == nil then
    base.m_simpleTV.User.Ecostream = {}
  end
  if base.m_simpleTV.User.Ecostream.session == nil then
    base.m_simpleTV.User.Ecostream.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.Ecostream.session == nil then
      return false
    end
    base.m_simpleTV.WinInet.SetOptionInt(base.m_simpleTV.User.Ecostream.session, 2, 3000, 0)
    base.m_simpleTV.WinInet.SetOptionInt(base.m_simpleTV.User.Ecostream.session, 6, 5000, 0)
  end
  return true
end
function EcostreamOnError(Mess)
  if base.m_simpleTV.User.Ecostream.session ~= nil then
    base.m_simpleTV.WinInet.Close(base.m_simpleTV.User.Ecostream.session)
    base.m_simpleTV.User.Ecostream.session = nil
  end
  base.m_simpleTV.OSD.ShowMessage("Ecostream error: " .. Mess, 255, 10)
end
function EcostreamGetAddress(inAdr)
  if EcostreamTryConnect() ~= true then
    EcostreamOnError("Server error")
    return
  end
  local rc, answer = base.m_simpleTV.WinInet.Get(base.m_simpleTV.User.Ecostream.session, inAdr)
  if rc ~= 200 then
    EcostreamOnError("Connection error - " .. rc)
    return
  end
  if base.string.match(inAdr, "embed") then
    local inAdr = base.findpattern(answer, "<a href=%b\"\"", 1, 9, 1)
    if inAdr == nil then
      EcostreamOnError("video is not available1")
      return
    end
    rc, answer = base.m_simpleTV.WinInet.Get(base.m_simpleTV.User.Ecostream.session, inAdr)
    if rc ~= 200 then
      EcostreamOnError("Connection error - " .. rc)
      return
    end
  end
  answer = base.findpattern(answer, "method=\"post\"(.-)</form>", 1, 13, 7)
  if answer == nil then
    EcostreamOnError("video is not available")
    return
  end
  local body, ss, sss, action
  ss = base.findpattern(answer, "value=\"(.-)\"", 1, 7, 1)
  sss = base.findpattern(answer, "name=\"sss\" value=\"(.-)\"", 1, 18, 1)
  body = "ss=" .. ss .. "&sss=" .. sss
  action = base.findpattern(answer, "action=\"(.-)\"", 1, 8, 1) .. "?ss=" .. ss
  header = "Accept:\ttext/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:\tapplication/x-www-form-urlencoded\n"
  rc, answer = base.m_simpleTV.WinInet.Post(base.m_simpleTV.User.Ecostream.session, action, nil, header, body)
  if rc ~= 200 then
    EcostreamOnError("Connection error - " .. rc)
    return
  end
  local s, k, tt, key, url
  local i = 1
  local t = {}
  answer = base.findpattern(answer, "\"lc(.-)\"", 1, 3, 1)
  for w in base.string.gmatch(answer, "%b''") do
    t[i] = base.string.gsub(w, "'", "")
    i = i + 1
  end
  s = t[1]
  k = t[2]
  tt = t[3]
  key = t[4]
  url = "http://www.ecostream.tv/lc/m.php?s=" .. s .. "&k=" .. k .. "&t=" .. tt .. "&key=" .. key
  header = "X-Requested-With:\tXMLHttpRequest\n" .. "Accept:\t */*\n" .. "Referer:" .. action .. "\n" .. "Content-Type:\tapplication/x-www-form-urlencoded\n"
  rc, answer = base.m_simpleTV.WinInet.Post(base.m_simpleTV.User.Ecostream.session, url, nil, header)
  if rc ~= 200 then
    EcostreamOnError("Connection error - " .. rc)
    return
  end
  local retAdr = base.findpattern(answer, "file=(.-)&", 1, 5, 1)
  return retAdr
end
