--script for zona.mobi (27/04/2015)
--open URL http://zona.mobi/movies/prizrak-2014 / http://zona.mobi/tvseries/vo-vse-tyazhkie-2008

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, 'zona%.mobi/movies/.+' ) and not string.match( inAdr, 'zona%.mobi/tvseries/.+' ) and not string.match( inAdr, 'http://www%.24video%.com/video/json/' ) then return end

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

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

 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,8000,0)
  --INTERNET_OPTION_RECEIVE_TIMEOUT
  m_simpleTV.WinInet.SetOptionInt(session,6,8000,0)
-------------------------------------------

local function GetAddress(url)

local rc, answer = m_simpleTV.WinInet.Get(session, url)
m_simpleTV.WinInet.Close(session)
 if rc~=200 then
	   m_simpleTV.WinInet.Close(session)
	   m_simpleTV.OSD.ShowMessage("Connection error2 - " .. rc ,255,10)
	   return
 end
answer = m_simpleTV.Common.string_fromUTF8(answer)

--debug_in_file(answer .. '\n\n')

local header =  findpattern( answer,'"title":"(.-)"',1,9,1)
if header==nil then header=inAdr end

local retAdr = findpattern( answer,'"url":"(.-)"',1,7,1)
if retAdr == nil then return nil end

  return retAdr,header
end
---------------------------------------------------
if  string.match( inAdr, 'http://www%.24video%.com/video/json/' ) then
    m_simpleTV.Control.CurrentAdress = GetAddress(inAdr)
   return
end

if string.match( inAdr, 'zona%.mobi/movies/.+' ) then

  local id = string.match(inAdr,'movies/(.+)')
  if id == nil then
    	   m_simpleTV.OSD.ShowMessage("Zona.mobi script error ",255,10)
	   return
  end
  local url = 'http://api2.zona.mobi/solr/movie/select/?start=0&fl=rating_imdb%2Crating_kinopoisk%2Crating_imdb_count%2Crating_kinopoisk_count%2Cname_id%2Cname_rus%2Cname_original%2Cbackdrop_id%2Cdescription%2Cyear%2CgenreId%2Ccountry%2Ccountry_id%2Crelease_date_rus%2Crelease_date_int%2Cruntime%2Cid%2Crelease_date_hq%2Cpersons%2Cmobi_link_id%2Cmobi_link_date%2Crating%2Cserial%2Cabuse&wt=json&rows=1&version=2.2&q=(name_id%3A' .. id .. ')'

local rc, answer = m_simpleTV.WinInet.Get(session, url)
 if rc~=200 then
	   m_simpleTV.WinInet.Close(session)
	   m_simpleTV.OSD.ShowMessage("Connection error1 - " .. rc ,255,10)
	   return
 end
answer = m_simpleTV.Common.string_fromUTF8(answer)

--debug_in_file(answer .. '\n\n')

local tab = json.decode(answer)
if tab == nil then return end

  id = tab.response.docs[1].mobi_link_id
  if id == nil then
    	   m_simpleTV.OSD.ShowMessage("Zona.mobi json error ",255,10)
	   return
  end

  url = 'http://www.24video.com/video/json/' .. id .. '?callback=angular.callbacks._0'

  local retAdr,header = GetAddress(url)

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

  if retAdr == nil then
      	   m_simpleTV.OSD.ShowMessage("Zona.mobi - file not found ",255,10)
  	   return
    end
  m_simpleTV.Control.CurrentAdress =  retAdr

end


-- series
if string.match( inAdr, 'zona%.mobi/tvseries/.+' )  then 

  local id = string.match(inAdr,'tvseries/(.+)')
  if id == nil then
    	   m_simpleTV.OSD.ShowMessage("Zona.mobi script error ",255,10)
	   return
  end
  local url = 'http://api2.zona.mobi/solr/movie/select/?start=0&fl=rating_imdb%2Crating_kinopoisk%2Crating_imdb_count%2Crating_kinopoisk_count%2Cname_id%2Cname_rus%2Cname_original%2Cbackdrop_id%2Cdescription%2Cyear%2CgenreId%2Ccountry%2Ccountry_id%2Crelease_date_rus%2Crelease_date_int%2Cruntime%2Cid%2Crelease_date_hq%2Cpersons%2Cmobi_link_id%2Cmobi_link_date%2Crating%2Cserial%2Cabuse&wt=json&rows=1&version=2.2&q=(name_id%3A' .. id .. ')'

local rc, answer = m_simpleTV.WinInet.Get(session, url)
 if rc~=200 then
	   m_simpleTV.WinInet.Close(session)
	   m_simpleTV.OSD.ShowMessage("Connection error3 - " .. rc ,255,10)
	   return
 end
answer = m_simpleTV.Common.string_fromUTF8(answer)

--debug_in_file(answer .. '\n\n')

local tab = json.decode(answer)
if tab == nil then return end

  id = tab.response.docs[1].id
  if id == nil then
    	   m_simpleTV.OSD.ShowMessage("Zona.mobi json error ",255,10)
	   return
  end

local header =  tab.response.docs[1].name_rus
if header==nil then header=inAdr end
if m_simpleTV.Control.CurrentTitle ~= nil then m_simpleTV.Control.CurrentTitle=header end

  url = 'http://api2.zona.mobi/solr/episode/select/?fl=episode_key%2Ctitle%2Cmobi_link_id%2Crelease_date&version=2.2&start=0&rows=2147483647&wt=json&q=(kinopoisk_id%3A' .. id .. ')'

local rc, answer = m_simpleTV.WinInet.Get(session, url)
 if rc~=200 then
	   m_simpleTV.WinInet.Close(session)
	   m_simpleTV.OSD.ShowMessage("Connection error4 - " .. rc ,255,10)
	   return
 end
answer = m_simpleTV.Common.string_fromUTF8(answer)

--debug_in_file(answer .. '\n\n')

tab = json.decode(answer)
if tab == nil then return end

 local t={}
 local i=1
 local retAdr
 
 while true do
   if tab.response.docs[i] == nil then break end
 
   t[i] = {}
   t[i].Id   =  i
   t[i].Name =  tab.response.docs[i].episode_key .. ' - ' .. tab.response.docs[i].title 
   t[i].Adress  = 'http://www.24video.com/video/json/' .. tab.response.docs[i].mobi_link_id .. '?callback=angular.callbacks._0'
 
 --debug_in_file(t[i].Name .. '  ' .. t[i].Adress .. '\n')
 i=i+1
 end

 if i>2 then
    m_simpleTV.OSD.ShowSelect(header,0,t,10000)
 end

if i>1 then
   retAdr = GetAddress( t[1].Adress)
end

  if retAdr == nil then
      	   m_simpleTV.OSD.ShowMessage("Zona.mobi - file not found ",255,10)
  	   return
    end
  m_simpleTV.Control.CurrentAdress =  retAdr
end


