Advertisement
Guest User

Untitled

a guest
Feb 6th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.03 KB | None | 0 0
  1. -- cache for alredy resolved titles and
  2. -- titles that could not be resolved (filenames)
  3. -- filename => title
  4. local title_cache = {}
  5.  
  6. -- poor man's mutex
  7. -- filename => true / false
  8. local title_resolving = {}
  9.  
  10. local playlist_startswith = 'https://www.youtube.com/playlist?list='
  11.  
  12. -- resolve title using youtube-dl
  13. -- ... and abusig gnu coreutils for what could be done in lua
  14. function resolve_title(filename)
  15.     print("resolving")
  16.     -- workaround about poor man's mutex
  17.     -- ensures that the actual check won't happen twice
  18.     if title_cache[filename] then
  19.         return title_cache[filename]
  20.     end
  21.  
  22.     -- title cannot be resolved using youtube-dl
  23.     title_resolving[filename] = true
  24.     if not string.match(filename, "^https?://") then
  25.         title_cache[filename] = filename
  26.         title_resolving[filename] = false
  27.     end
  28.    
  29.     local res = io.popen('youtube-dl --flat-playlist -e '.. filename .. ' | head -n 1'):read("*all")
  30.     if res and res ~= '' then
  31.         title_cache[filename] = res
  32.     else
  33.         -- try resolving as playlist
  34.         res = io.popen('youtube-dl --flat-playlist -s '.. filename ..' | tail -n 1 | ' ..
  35.             "awk 'match($0, /playlist: (.+$)/) { print substr($0, RSTART + 10, RLENGTH - 10) }'"):read("*all")
  36.         if res and res ~= '' then
  37.             title_cache[filename] = '[playlist]: ' .. res
  38.         else
  39.             -- finally give up
  40.             title_cache[filename] = filename
  41.         end
  42.     end
  43.    
  44.     title_resolving[filename] = false
  45. end
  46.  
  47. function get_playlist()
  48.     local pos = mp.get_property_number('playlist-pos', 0) + 1
  49.     local count, limlist = limited_list('playlist', pos)
  50.     if count == 0 then
  51.         return 'Empty playlist.'
  52.     end
  53.  
  54.     local message = string.format('Playlist [%d/%d]:\n', pos, count)
  55.     for i, v in ipairs(limlist) do
  56.         local title = v.title
  57.         if not title then
  58.            
  59.             -- try printing title from the cache
  60.             if title_cache[v.filename] then
  61.                 title = title_cache[v.filename]
  62.            
  63.             -- if not, check if it's resolving in the background
  64.             elseif title_resolving[v.filename] then
  65.                 title = v.filename
  66.            
  67.             -- if not, try to resolve it if filename looks like a network stream
  68.             elseif string.match(v.filename, "https?://") then
  69.                 -- doesn't work for whatever reason
  70.                 --mp.add_timeout(0.05, resolve_title(v.filename))
  71.                 resolve_title(v.filename)
  72.            
  73.                 -- temporary title while a proper one is being resolved
  74.                 title = v.filename
  75.            
  76.             -- else assume a local file and use it's filename
  77.             else
  78.                 local _
  79.                 _, title = utils.split_path(v.filename)
  80.                 title_cache[v.filename] = title
  81.             end
  82.         end
  83.         message = string.format('%s %s %s\n', message,
  84.             (v.current and '●' or '○'), title)
  85.     end
  86.     return message
  87. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement