--script for youtube user playlists (12/03/2014)
--open link http://www.youtube.com/playlist?list=PL425xO8HuUCfTlPf9-Oj_VOu7UQC0Uj6e

require('json')

if m_LinkToPlay.Control.ChangeAddress ~= 'No' then return end

local inAdr = m_LinkToPlay.Control.CurrentAddress
if inAdr == nil then return end

if not string.match(inAdr, '^http://www%.youtube%.com/playlist') then return end

m_LinkToPlay.Control.ChangeAddress = 'Yes'
m_LinkToPlay.Control.CurrentAddress = ''

--------------
function trim(str)
	str = string.match(str,'^%s*(.-)%s*$')
	return str
end

local function unescape_html(str)
  str = string.gsub( str, '&lt;', '<' )
  str = string.gsub( str, '&gt;', '>' )
  str = string.gsub( str, '&quot;', '"' )
  str = string.gsub( str, '&apos;', "'" )
  str = string.gsub( str, '&#(%d+);', function(n) return string.char(n) end )
  str = string.gsub( str, '&#x(%d+);', function(n) return string.char(tonumber(n,16)) end )
  str = string.gsub( str, '&amp;', '&' ) -- Be sure to do this after all others
  return str
end

---------------
local session = m_LinkToPlay.WinInet.New("Opera/9.80 (Windows NT 6.1; U; ru) Presto/2.9.168 Version/11.52")
if session == nil then return end

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

answer = m_LinkToPlay.Common.string_fromUTF8(answer)
answer = string.gsub(answer, '\n', '')
answer = string.gsub(answer, '\r', '')

--debug_in_file(answer .. '\n', 'C:\\debug_1.txt', true)
--do return end
local header = findpattern(answer, '<title>(.-)</title>', 1, 7, 8)
if header == nil then header = inAdr end
header = string.gsub(header, '&quot;', '"')
header = string.gsub(header, '&raquo;', '')
header = string.gsub(header, '&#39;', "'")

if m_LinkToPlay.Control.CurrentTitle ~= nil then m_LinkToPlay.Control.CurrentTitle = unescape_html(header) end
--debug_in_file('1) ' .. m_LinkToPlay.Control.CurrentTitle .. '\n\n')

rc, answer = m_LinkToPlay.WinInet.Get(session, inAdr)
m_LinkToPlay.WinInet.Close(session)
if rc ~= 200 then return end
answer = string.gsub(answer, '&quot;', '"')
answer = string.gsub(answer, '&raquo;', '')
answer = string.gsub(answer, '&#39;', "'")
answer = string.gsub(answer, '&amp;', "&")
answer = m_LinkToPlay.Common.string_fromUTF8(answer)

local host = findpattern(inAdr, 'http://(.-)/', 1, 0, 0)
local i, t = 1, {}
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].Address = host .. adr
    --debug_in_file(t[i].Name .. '\n' .. t[i].Address .. '\n\n')
    if retAdr == '' then retAdr = t[i].Address end
	i = i + 1
end

if i > 2 then
    local ret = m_LinkToPlay.Taskbar.AddList(header, 0, t, 15000)
    return
    --m_simpleTV.OSD.ShowSelect(header, 0, t, 15000)
end

require ('youtubecore')

retAdr, header = youtubecore.GetWatchAddress(retAdr, true)
if retAdr == nil then return end
if type(retAdr) == 'table' then
	local ret = m_LinkToPlay.Taskbar.AddList(header, 0, retAdr, 7000, -1)
else
	m_LinkToPlay.Control.CurrentAddress = retAdr
end