--script for youtube user uploads (15/07/2015)
-- open link http://www.youtube.com/user/Fletcher2008/videos
require('json')

if m_simpleTV.Control.ChangeAdress ~= 'No' then return end

local inAdr =  m_simpleTV.Control.CurrentAdress

if inAdr==nil then return end
--https://www.youtube.com/user/yalksne07/videos
if not string.match( inAdr, 'www%.youtube%.com/user/(.+)/videos$' )  then return end

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

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

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
local rc,answer = m_simpleTV.WinInet.Get(session,inAdr)
m_simpleTV.WinInet.Close(session)
if rc ~= 200 then return end

local header =  findpattern( answer,'<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 = m_simpleTV.Common.string_fromUTF8(answer)

 local i=1
 local t={}
 local retAdr,name,adr
 for w in string.gmatch(answer,'<div class="yt%-lockup%-content">(.-)</h3>') do
	name = findpattern (w,'title="(.-)"',1,7,1)
	adr = findpattern (w,'href="(.-)"',1,6,1)
	if adr == nil or name == nil then break end

     t[i] = {}
     t[i].Id   =  i
     t[i].Name =  i .. '. ' .. youtubecore.unescape_html(name )
     t[i].Adress  = 'http://www.youtube.com' .. adr

--debug_in_file(t[i].Name .. '  ' .. t[i].Adress .. '\n\n')
i=i+1
 end
  if i>2 then
    m_simpleTV.OSD.ShowSelect_UTF8(header,0,t,5000)
 end

  if i>1 then
    retAdr = youtubecore.GetWatchAddress(t[1].Adress)
    if retAdr==nil then return end
 end
m_simpleTV.Control.CurrentAdress =  retAdr
