summaryrefslogtreecommitdiff
path: root/scripts/python/mpdnp.py
blob: 37e9da6c3a0accd175850505a13cac4384209d81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""
  :Author: Henning Hasemann <hhasemann [at] web [dot] de>

  :What it does:
    This plugin lets you inform all users in the current
    channel about the song which music-player-daemon (MPD)
    is currently playing.

  :Usage:
    /mpdnp - Display file mpd is playing to current channel.

  :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
  
  Released under GPL licence.
"""

todo = """
  - maybe support sending commands to mpd.
  - maybe make condional display
    (displaying some characters only
     if preceding or trailing variables are set)
"""

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)