--script for youtube user playlists (15/07/2015)
--open link http://www.youtube.com/playlist?list=PL425xO8HuUCfTlPf9-Oj_VOu7UQC0Uj6e
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, 'www%.youtube%.com/playlist' )  then return end

require ('youtubecore')
m_simpleTV.Control.ChangeAdress='Yes'
m_simpleTV.Control.CurrentAdress =  'error'

local isPlst = false 
 if m_simpleTV.User~=nil and m_simpleTV.User.Youtube~=nil and m_simpleTV.User.Youtube.Plst~=nil and m_simpleTV.User.Youtube.Plst.Active==true then 
  isPlst = true
  m_simpleTV.User.Youtube.Plst.Active = false
 end

--------------
function trim(str)
str = string.match(str,'^%s*(.-)%s*$')
  return str
end
------------------------------------------------------------------
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
if string.match(inAdr, '^https://') then
	m_simpleTV.WinInet.SetOpenRequestFlags(session, 0x00800000 + 0x00002000 + 0x00001000)
end

--m_simpleTV.OSD.ShowMessage(inAdr,255,5)
local rc,answer = m_simpleTV.WinInet.Get(session,inAdr)
m_simpleTV.WinInet.Close(session)
if rc ~= 200 then return end

local header = string.gsub(answer,'\n','')
header = string.gsub(header,'\r','')
header =  findpattern( header,'<title>(.-)</title>',1,7,8)
if header==nil then header=inAdr end
if m_simpleTV.Control.CurrentTitle_UTF8 ~= nil then m_simpleTV.Control.CurrentTitle_UTF8=youtubecore.unescape_html(header) end

answer = string.gsub(answer,'&quot;','"')
answer = string.gsub(answer,'&raquo;','')
answer = string.gsub(answer,'&#39;',"'")
answer = string.gsub(answer,'&amp;',"&")
--answer = m_simpleTV.Common.string_fromUTF8(answer)

local host = findpattern(inAdr,'http://(.-)/',1,0,0) or findpattern(inAdr,'https://(.-)/',1,0,0)
local i=1
local t={}
local name,adr
local retAdr ='' 

 for w in string.gmatch(answer,'<a class="pl%-video%-title%-link(.-)/a>') do

	name = findpattern (w,'%b><',1,1,1)
	if name==nil then name = 'Noname' end
	
	adr = findpattern (w,'href="(.-)&',1,7,1)
        if adr == nil then break end

     t[i] = {}
     t[i].Id  =  i
     t[i].Name = i .. '. ' .. trim(name)
     t[i].Adress = host .. adr

     --debug_in_file(t[i].Name .. '\n' .. t[i].Adress .. '\n\n')
     if retAdr == '' then retAdr = t[i].Adress end
     i=i+1
 end
 
if i==1 then return end 
 
 if i>2 or isPlst then
    
	local Scr=nil
	if isPlst == true then	 	  
	  t.ExtButton1 = {}
	  t.ExtButton1.ButtonEnable = true
	  t.ExtButton1.ButtonName = "Playlists"
	  t.ExtButton1.ButtonScript = "ChoiseYoutubePlst()"
	  
	  if m_simpleTV.User.Youtube.Plst.Level0 == true then		
		m_simpleTV.User.Youtube.Plst.Level0 = false
		Scr = 'user\\video\\youtube_user_script.lua'
		m_simpleTV.User.Youtube.Plst.Level0_Enabled = true
		m_simpleTV.User.Youtube.Plst.Table_Level1 =  t
		m_simpleTV.User.Youtube.Plst.Header_Level1 = header
	   else 
	    m_simpleTV.User.Youtube.Plst.Table_Level0 =  t
		m_simpleTV.User.Youtube.Plst.Header_Level0 = header
	  end
	end
	
	local fl = 0
	if isPlst == true then
	      fl = 8 
	 else 
       if i==2 then fl=fl + 32 end
	end

	m_simpleTV.OSD.ShowSelect_UTF8(header,0,t,5000,fl,Scr)
 end

retAdr = youtubecore.GetWatchAddress(retAdr)
if retAdr==nil then return end
m_simpleTV.Control.CurrentAdress =  retAdr
