Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.22 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. -- resolve title using youtube-dl
  11. -- ... and abusig gnu coreutils for what could be done in lua
  12. function resolve_title(filename)
  13.     -- workaround about poor man's mutex
  14.     -- ensures that the actual check won't happen twice
  15.     if title_cache[filename] then
  16.         return title_cache[filename]
  17.     end
  18.  
  19.     title_resolving[filename] = true
  20.     local res = io.popen('youtube-dl --flat-playlist -e '.. filename .. ' | head -n 1'):read("*all")
  21.     if res and res ~= '' then
  22.         title_cache[filename] = res
  23.     else
  24.         title_cache[filename] = filename
  25.     end
  26.     title_resolving[filename] = false
  27. end
  28.  
  29. function get_playlist()
  30.     local pos = mp.get_property_number('playlist-pos', 0) + 1
  31.     local count, limlist = limited_list('playlist', pos)
  32.     if count == 0 then
  33.         return 'Empty playlist.'
  34.     end
  35.  
  36.     local message = string.format('Playlist [%d/%d]:\n', pos, count)
  37.     for i, v in ipairs(limlist) do
  38.         local title = v.title
  39.         if not title then
  40.             -- try printing title from the cache
  41.             if title_cache[v.filename] then
  42.                 title = title_cache[v.filename]
  43.             -- if not, check if it's resolving in the background
  44.             elseif title_resolving[v.filename] then
  45.                 title = v.filename
  46.             -- if not, try to resolve it if filename looks like a network stream
  47.             elseif string.match(v.filename, "https?://") then
  48.                 -- doesn't work for whatever reason
  49.                 --mp.add_timeout(0.05, resolve_title(v.filename))
  50.                 resolve_title(v.filename)
  51.                 title = v.filename
  52.             -- else assume a local file and just print the filename instead
  53.             else
  54.                 local _
  55.                 _, title = utils.split_path(v.filename)
  56.                 title_cache[v.filename] = title
  57.             end
  58.         end
  59.         message = string.format('%s %s %s\n', message,
  60.             (v.current and '●' or '○'), title)
  61.     end
  62.     return message
  63. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement