--script for ok.ru (04/12/2015)
--open links http://ok.ru/videoembed/35476081345

 require('json')

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://ok%.ru/videoembed/' )  then return end

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

-----------------------------------------------

 local session = m_simpleTV.WinInet.New("Opera/9.80 (Windows NT 6.2; WOW64; U; ru) Presto/2.10.229 Version/11.64")
 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)
-------------------------------------------

 local num = findpattern( inAdr,'videoembed/(.-)$',1,11,0)
 if num == nil then return end

 local url = 'http://ok.ru/dk?cmd=videoPlayerMetadata&mid=' .. num 

            local head = 
            'Referer: ' .. inAdr .. '\n' .. 
            'User-Agent: Opera/9.80 (Windows NT 6.2; WOW64; U; ru) Presto/2.10.229 Version/11.64'
       
             local rc,answer=m_simpleTV.WinInet.Get(session,url,nil,0,head)
             m_simpleTV.WinInet.Close(session)
             if rc~=200 then
            	   m_simpleTV.WinInet.Close(session)
            	   m_simpleTV.OSD.ShowMessage("Connection error - " .. rc ,255,10)
            	   return
             end
           
            answer = unescape3(answer)
            --debug_in_file(answer .. '\n')

            local t = json.decode(answer)
            if t == nil then m_simpleTV.OSD.ShowMessage("ok.ru script error",255,5) return end

            local tt={}
            local i=1
            while true do
              if t.videos[i] == nil then break end
             
              tt[i] = {}
              tt[i].Id   =  i
              tt[i].Name =  t.videos[i].name
              tt[i].Adress = t.videos[i].url
           
           -- debug_in_file(tt[i].Name .. ' ' .. tt[i].Adress  .. "\n\n")
              i=i+1
            end

  local ttt = table_reverse(tt)
  local retAdr

  if i>1 then
    local ret,id = m_simpleTV.OSD.ShowSelect('Select quality',0,ttt,5000,1+4+8)
      if id == nil then return end
      if ret==1 then
	retAdr = tt[id].Adress
      end
      
  end

m_simpleTV.Control.CurrentAdress = retAdr .. '$OPT:http-user-agent=Opera/9.80 (Windows NT 6.2; WOW64; U; ru) Presto/2.10.229 Version/11.64$OPT:http-referrer=http://st.mycdn.me/static/MegaPlayer/10-3-3/vp11.swf'
