Fandom

Music Player Daemon Community Wiki

Hack:mpdnp

525pages on
this wiki
Add New Page
Talk0 Share

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

  • Configuration Variables:
   ============= ==========================================
   Variable name Meaning
   ============= ==========================================
   host          The host where your mpd runs
   port          The port to connect to mpd (usually 6600)
   format        How this script should display
                 whats going on.
                 You may use the following variables here:
                 $artist, $title_or_file,
                 $length_min, $length_sec, $pct,
                 $pos_min, $pos_sec
  • License: Released under GPL licence.
import weechat as wc
import mpdclient as mpd
import re
from os.path import basename, splitext

default_fmt = "/me 's MPD plays: $artist - $title_or_file ($length_min:$length_sec)"

wc.register("mpdnp", "0.3", "", "np for mpd")

def subst(text, values):
 out = ""
 n = 0
 for match in re.finditer(findvar, text):
   if match is None: continue 
   else:
     l, r = match.span()
     nam = match.group(1)
     out += text[n:l+1] + values.get(nam, "") #"$" + nam)
     n = r
 return out + text[n:]

def np(server, args):
 """
   Send information about the currently
   played song to the channel.
 """
 host = wc.get_plugin_config("host")
 port = int(wc.get_plugin_config("port"))
 cont = mpd.MpdController(host=host, port=port)
 song = cont.getCurrentSong()
 pos, length, pct = cont.getSongPosition()

 # insert artist, title, album, track, path
 d = song.__dict__
 d.update({
     "title_or_file": song.title or splitext(basename(song.path))[0],
     "pos_sec": "%02d" % (pos / 60),
     "pos_min": str(pos / 60),
     "length_sec": "%02d" % (length % 60),
     "length_min": str(length / 60),
     "pct": "%2.0f" % pct,
 })
 
 wc.command(subst(wc.get_plugin_config("format"), d))
 return 0
 
def dbgnp(server, args):
 try:
   return np(server, args)
 except Exception, e:
   print e
 
wc.add_command_handler("mpdnp", "np", "", "", np.__doc__)

findvar = re.compile(r'[^\\]\$([a-z_]+)(\b|[^a-z_])')

default = {
 "host": "localhost",
 "port": "6600",
 "format": default_fmt,
}

for k, v in default.items():
 if not wc.get_plugin_config(k):
   wc.set_plugin_config(k, v)

Also on Fandom

Random Wiki