Guest User

Untitled

a guest
Sep 9th, 2019
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.20 KB | None | 0 0
  1. #!/usr/bin/lua
  2.  
  3. local lgi = require('lgi')
  4. local player = lgi.Playerctl.Player{}
  5.  
  6. -- penlight func, temporary.
  7. function assert_arg (n,val,tp,verify,msg,lev)
  8.     if type(val) ~= tp then
  9.         error(("argument %d expected a '%s', got a '%s'"):format(n,tp,type(val)),lev or 2)
  10.     end
  11.     if verify and not verify(val) then
  12.         error(("argument %d: '%s' %s"):format(n,val,msg),lev or 2)
  13.     end
  14.     return val
  15. end
  16.  
  17. -- penlight func
  18. local function assert_string (n,s)
  19.     assert_arg(n,s,'string')
  20. end
  21.  
  22. -- decode uri to standard unix path
  23. local decodeURI
  24. do
  25.     local char, gsub, tonumber = string.char, string.gsub, tonumber
  26.     local function _(hex) return char(tonumber(hex, 16)) end
  27.  
  28.     function decodeURI(s)
  29.         s = gsub(s, '%%(%x%x)', _)
  30.         s = gsub(s, 'file://', '')
  31.         return s
  32.     end
  33. end
  34.  
  35. -- check if file is available, if not return
  36. local raw_path = player:print_metadata_prop('xesam:url')
  37. if raw_path == nil then
  38.     return
  39. else
  40.     path = decodeURI(raw_path)
  41. end
  42.  
  43. -- capture system cmd
  44. function os.capture(cmd)
  45.     local f = io.popen(cmd, 'r')
  46.     local s = f:read()
  47.     f:close()
  48.     return s
  49. end
  50.  
  51. -- get bitrate
  52. function string_to_decode(str)
  53.     local cmd = ('mediainfo --inform="Audio;%BitRate/String%" "' ..path.. '" ')
  54.     local buf = os.capture(cmd)
  55.     return buf
  56. end
  57.  
  58. -- when albumarArtist is a list, filter all but first artist
  59. local raw_albumArtist = player:print_metadata_prop('xesam:albumArtist')
  60. function format_artist()
  61.     for word in string.gmatch(raw_albumArtist, '([^,;]+)') do
  62.        return word
  63.     end
  64. end
  65.  
  66. -- fetch metadata via playerctl lib
  67. local title = player:get_title()
  68. local albumartist = format_artist()
  69. local album = player:get_album()
  70. -- get bitrate from mediainfo
  71. local bitrate = string_to_decode(str)
  72.  
  73. -- concat metadata values into a single string
  74. function listvalues(s)
  75.     local t = { }
  76.     for k,v in ipairs(s) do
  77.         t[#t+1] = tostring(v)
  78.     end
  79.     return table.concat(t,"  /  ")
  80. end
  81.  
  82.  
  83. -- calculate at what length to truncate
  84. local total_length = utf8.len(title) +utf8.len(albumartist) +utf8.len(album) +utf8.len(bitrate)
  85. local chars = total_length - 75
  86.  
  87. local max_title = math.floor( utf8.len(title) - chars )
  88. local max_albumartist = math.floor( utf8.len(albumartist) - chars )
  89. local max_album = math.floor( utf8.len(album) - chars )
  90.  
  91. function shorten(s,w)
  92.     local ellipsis = "…"
  93.     local n_ellipsis = utf8.len(ellipsis)
  94.     assert_string(1,s)
  95.     if utf8.len(s) > w then
  96.         return s:sub(1,w-n_ellipsis) .. ellipsis
  97.     end
  98.     return s
  99. end
  100.  
  101. -- only truncate the longest string...
  102. -- figure out a better way to determine the longset variable.
  103. if utf8.len(title) >= utf8.len(albumartist) and utf8.len(title) >= utf8.len(album) then
  104.     metadata = { shorten(title, max_title), albumartist, album, bitrate }
  105. elseif utf8.len(albumartist) >= utf8.len(title) and utf8.len(albumartist) >= utf8.len(album) then
  106.     metadata = { title, shorten(albumartist, max_albumartist), album, bitrate }
  107. elseif utf8.len(album) >= utf8.len(title) and utf8.len(album) >= utf8.len(albumartist) then
  108.     metadata = { title, albumartist, shorten(album, max_album), bitrate }
  109. end
  110.  
  111. print(listvalues(metadata))
Advertisement
Add Comment
Please, Sign In to add comment