dotfiles

My collection of dotfiles
git clone git://git.noxz.tech/dotfiles
Log | Files | Refs

ztatus.lua (3014B)


      1 function string.htmlescape(str)
      2     local str = string.gsub(str, "<", "&lt;")
      3     str = string.gsub(str, ">", "&gt;")
      4     str = string.gsub(str, "&", "&amp;")
      5     str = string.gsub(str, "\"", "&quot;")
      6     str = string.gsub(str, "'", "&apos;")
      7     return str
      8 end
      9 
     10 function string.shellescape(str)
     11     return "'"..string.gsub(str, "'", "'\"'\"'").."'"
     12 end
     13 
     14 function string.starts(str,start)
     15     return string.sub(str, 1, string.len(start)) == start
     16 end
     17 
     18 function os.capture(cmd, raw)
     19     local f = assert(io.popen(cmd, 'r'))
     20     local s = assert(f:read('*a'))
     21     f:close()
     22     if raw then return s end
     23     s = string.gsub(s, '^%s+', '')
     24     s = string.gsub(s, '%s+$', '')
     25     s = string.gsub(s, '[\n\r]+', ' ')
     26     return s
     27 end
     28 
     29 function notify_current_track()
     30     local data = mp.get_property_native("metadata")
     31     local state = "playing"
     32     local artist = ""
     33     local title = ""
     34     local media_title = ""
     35     local body = ""
     36     local command = ""
     37 
     38     if not data then
     39         return
     40     end
     41 
     42     if mp.get_property_native("pause") then
     43         state = "paused"
     44     end
     45 
     46     function get_metadata(data, keys)
     47         for _,v in pairs(keys) do
     48             if data[v] and string.len(data[v]) > 0 then
     49                 return data[v]
     50             end
     51         end
     52         return ""
     53     end
     54 
     55     artist = get_metadata(data, {"artist", "ARTIST"})
     56     title = get_metadata(data, {"title", "TITLE", "icy-title"})
     57     media_title = mp.get_property_native("media-title")
     58 
     59     -- soundcloud user and title
     60     if string.starts(mp.get_property_native("path"), "https://api.soundcloud.com/tracks/") then
     61         body = string.shellescape(("%s: %s"):format(
     62             state,
     63             os.capture(("sscw-track %s"):format(
     64                 string.shellescape(mp.get_property_native("filename"))
     65             ))
     66         ))
     67     -- filename only
     68     elseif title ~= "" then
     69         -- title only
     70         if artist == "" then
     71             body = string.shellescape(("%s: %s"):format(
     72                 state,
     73                 string.htmlescape(title)
     74             ))
     75         -- artist and title
     76         else
     77             body = string.shellescape(("%s: %s - %s"):format(
     78                 state,
     79                 string.htmlescape(artist),
     80                 string.htmlescape(title)
     81             ))
     82         end
     83     -- media_title fallback
     84     elseif media_title ~= "" then
     85         body = string.shellescape(("%s: %s"):format(
     86             state,
     87 	    media_title
     88         ))
     89     -- filename fallback
     90     else
     91         body = string.shellescape(("%s: %s"):format(
     92             state,
     93             mp.get_property_native("filename")
     94         ))
     95     end
     96 
     97 	os.execute(("printf %s > /tmp/ztatus.fifo"):format(body))
     98 end
     99 
    100 function notify_metadata_updated(name, value)
    101     notify_current_track()
    102 end
    103 
    104 function notify_pause_change(name, value)
    105     notify_current_track()
    106 end
    107 
    108 
    109 mp.register_event("file-loaded", notify_current_track)
    110 mp.observe_property("metadata", nil, notify_metadata_updated)
    111 mp.observe_property("pause", "bool", notify_pause_change)