local base = _G


local string = require"string"

module('ms_kinopoisk_ru')

Parameters = 
            { 
             Charset       = 'windows-1251',
             --SearchUrl     = 'http://www.kinopoisk.ru/search/chrometoolbar.php?query=%s&go=1',
			 SearchUrl  = 'http://www.kinopoisk.ru%s',
             Body       = 'level=7&m_act[what]=content&%s&m_act[find]=%s',
             Method        = 'POST',
             Login         = false,
			 Lang          = 'ru',
             String        = ''
            }

--Информация для фильмов и сериалов на русском-------------------------------------------------------------------------------
function InfoMedia(year, actor, data)
 local a = data
 local session = base.m_simpleTV.WinInet.New("Opera/9.80 (Windows NT 6.1; U; ru) Presto/2.9.168 Version/11.52")
 if not session then return false, 5, ms.lang.message[5] end
 base.m_simpleTV.WinInet.SetOptionInt(session, 2, 8000,0)
 base.m_simpleTV.WinInet.SetOptionInt(session, 6, 8000,0)
 local 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' .. 'Accept-Language: ru-RU,ru;q=0.9,en;q=0.8\n' .. 'Content-Type:application/x-www-form-urlencoded\n'
 local url, body, act
 local d = base.escape(string.gsub(Parameters.String, " ", "+"))
 --[[
 url = string.format(Parameters.SearchUrl, d)
 --base.debug_in_file(url .. '\n')
 local rc, answer = base.m_simpleTV.WinInet.Get(session, url)
 ]]
 if data.serial and actor then
  --Сериал с актёром
  url = string.format(Parameters.SearchUrl, '/s/')
  act = base.escape(string.gsub(actor, " ", "+"))
  body = string.format(Parameters.Body, 'm_act[actor]=' .. act, d)
 else
  if year and (string.len(year) == 4) then
   --Фильм с датой
   url = string.format(Parameters.SearchUrl, '/s/')
   body = string.format(Parameters.Body, 'm_act[year]=' .. year, d)
  else
   --Фильм без даты
   url = string.format(Parameters.SearchUrl, '/index.php')
   body = 'first=yes&what=content&kp_query=' .. d
  end
 end
 local rc, answer = base.m_simpleTV.WinInet.Post(session, url, nil, header, body)
 if (rc ~= 200) then
  base.m_simpleTV.WinInet.Close(session)
  return false, 5, ms.lang.message[5]
 end
 local const = '<p class="header">Скорее всего, вы ищете:</p>'
 --Взять ссылку первого фильма из списка
 local index = string.find(answer, const, 1, true)
 if index then
  const = '<p class="name">(.-)</p>'
  local d = base.findpattern(answer, const, index, 0, 0)
  if d then
   local link = string.match(d, 'href="(.-)"', 1)
   if link then
    url = string.format(Parameters.SearchUrl, link)
    rc, answer = m_simpleTV.WinInet.Get(session, url)
   end
  end
 end
 base.m_simpleTV.WinInet.Close(session)
 if (rc ~= 200) then return false, 5, base.ms.lang.message[5] end
 --base.debug_in_file(answer)
 index = string.find(answer, 'viewFilmInfoWrapper', 1, true)
 if not index then return false, 5, base.ms.lang.message[5] end
 local content = string.sub(answer, index)
 --Проверка, что мы нашли правильный фильм (Пока исключили)
 d = string.match(content, 'itemprop="name">(.-)</h1>', 1)
 d  = string.gsub(d, "%b\<\>", "")
 d  = string.gsub(d, "%b\(\)", "")
 d = base.ms.trim(d)
 if d ~= Parameters.String then  end
 ---------------------------------------------------------
 --title = s .. ' / ' .. string.match(content, '<span itemprop="alternativeHeadline">(.-)</span>', 1)
 d = string.match(content, 'onclick="openImgPopup(.-);', 1)
 if d then a.image = string.match(d, "'(.-)'", 1) end
 d = base.findpattern(content, '<td class="type"   >год</td>(.-)</a>', 1, 23, 0)
 if d then a.year = string.gsub(d, "%b\<\>", "") end
 d = base.findpattern(content, '<td class="type"   >страна</td>(.-)</a>', 1, 26, 0)
 if d then a.country = string.gsub(d, "%b\<\>", "") end
 d = string.match(content, '<td itemprop="director">(.-)</a>', 1)
 if d then a.director = string.gsub(d, "%b\<\>", "") end
 d = base.findpattern(content, '<td class="type">жанр</td>(.-)</td>', 1, 21, 0)
 if d then a.genre = string.gsub(d, "%b\<\>", "") end
 a.duration = string.match(content, '<td class="time" id="runtime">(.-)</td> ', 1)
 d = base.findpattern(content, '<h4>В главных ролях:</h4>(.-)</ul>', 1, 0, 0)
 if d then
  local s = ''
  for row in string.gmatch(d, '<li itemprop="actors">(.-)</li>') do
   row = string.gsub(row, "%b\<\>", "") .. ','
   s = s .. row
  end
  a.actor = s
 end
 d = base.findpattern(content, 'itemprop="description">(.-)</div>', 1, 23, 0)
 if d then 
  d = string.gsub(d, "%b\<\>", "")
  a.descr = string.gsub(d, "&nbsp;", " ")
 end
 a.rating = string.match(content, '<meta itemprop="ratingValue" content="(.-)"', 1)
 
 --d = base.findpattern(content, '<span class="rating_ball">(.-)</span>', 1, 0, 0)
 --if d then a.rating = string.gsub(d, "%b\<\>", "") end
 return true, a, ''
end