--script for youtube 15/07/2015

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

local inAdr =  m_simpleTV.Control.CurrentAdress

if inAdr==nil then return end
inAdr = string.gsub(inAdr, 'https', 'http')

if string.match( inAdr, '^http://www%.youtube(.+)&list=WL' )  then 
	inAdr = findpattern(inAdr,'^(.-)&',1,0,1)
end

if string.match( inAdr, '^http://youtu%.be' )  then 
	inAdr = findpattern(inAdr,'youtu%.be(.+)$',1,8,0)
	inAdr = 'http://www.youtube.com/watch' .. inAdr
end	

if string.match( inAdr, '^http://www%.youtube%.com/embed' )  then 
  local id = findpattern(inAdr,'embed/(.+)',1,6,0)
  if id==nil then return end
  inAdr = 'http://www.youtube.com/watch?feature=player_embedded&v=' .. id
end

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

 if string.match( inAdr, 'youtube%.com/channel/(.-)/playlists') then 
   m_simpleTV.Control.CurrentAdress = inAdr
   dofile(m_simpleTV.MainScriptDir .. "user\\video\\youtube_user_playlists1.lua") 
   return
 end

if  string.match( inAdr, 'list=' ) or string.match( inAdr, 'youtube%.com/channel/' )   then 
   local pls = findpattern(inAdr,'list=(.-)$',1,5,0)
   if pls == nil then 
      pls = findpattern(inAdr,'channel/(.-)/',1,8,1) 
         if pls == nil then return end
      pls = string.gsub(pls,'^UC','UU')
   end

   inAdr = 'http://www.youtube.com/playlist?list=' .. pls
--debug_in_file(inAdr..'\n')
   m_simpleTV.Control.CurrentAdress = inAdr
   dofile(m_simpleTV.MainScriptDir .. "user\\video\\youtube_user_playlists.lua") 
  return
end
--do return end
require ('youtubecore')

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

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

 local rc,answer=m_simpleTV.WinInet.Get(session,inAdr)

 if rc~=200 then
	   m_simpleTV.WinInet.Close(session)
	   m_simpleTV.OSD.ShowMessage("Connection error - " .. rc ,255,10)
	   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)

--livestream
local retAdr
local hlsvp = findpattern(answer,'"hlsvp":(.-)",',1,10,2)
if hlsvp ~=nil then
   hlsvp = unescape (hlsvp)
   hlsvp = string.gsub(hlsvp, 'https', 'http')
   hlsvp = string.gsub(hlsvp,"\\",'')
     rc,answer = m_simpleTV.WinInet.Get(session,hlsvp)
      if rc ~= 200 then return end
--debug_in_file(answer .. '\n')
m_simpleTV.WinInet.Close(session)
local i=1
local t={}
local name,adr
 for w in string.gmatch(answer,'EXT%-X%-STREAM%-INF(.-)m3u8') do
	adr = findpattern (w,"http(.-)index",1,0,0)
	 if adr == nil then break end
	name = findpattern (w,'RESOLUTION=(.-)\n',1,0,0)
	 if name == nil then break end
     t[i] = {}
     t[i].Id   =  i
     t[i].Name = name
     t[i].Adress  = adr .. 'm3u8'

--debug_in_file(t[i].Name .. '  ' .. t[i].Adress .. '\n\n')
i=i+1
 end
if i>1 then
     retAdr = t[1].Adress
	 if retAdr == nil then
		  m_simpleTV.OSD.ShowMessage("Youtube Error - can't find stream",255,10)
		  retAdr=''
	  end
end

if i>2 then
    local ret,id = m_simpleTV.OSD.ShowSelect('Select resolution',0,t,7000,1)
	if ret==1 then
	  retAdr =  t[id].Adress
	end	
 end
m_simpleTV.Control.CurrentAdress =  retAdr	
else

local retAdr,header = youtubecore.GetWatchAddress(inAdr,true)

if retAdr == nil then return end
if m_simpleTV.Control.CurrentTitle_UTF8 ~= nil then m_simpleTV.Control.CurrentTitle_UTF8=header end
--if #retAdr>1 then
--	m_simpleTV.OSD.ShowSelect('Select resolution',0,retAdr,0,32+128)
--end	

m_simpleTV.Control.CurrentAdress =  retAdr[1].Adress
end