summaryrefslogtreecommitdiff
path: root/plugins/mpd_client.py
blob: 5207a0d680e23f8925d114c608d56ce0e57ed762 (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
# a plugin adding a command to manipulate an MPD instance

from plugin import BasePlugin
from common import shell_split
import mpd

class Plugin(BasePlugin):
    def init(self):
        self.add_command('mpd', self.command_mpd, "Usage: /mpd [full]\nMpd: sends a message showing the current song of an MPD instance. If full is provided, teh message is more verbose.", self.completion_mpd)

    def command_mpd(self, args):
        args = shell_split(args)
            c = mpd.MPDClient()
        try:
        c.connect(host=self.config.get('host', 'localhost'), port=self.config.get('host', '6600'))
        except Exception as e:
            self.core.information('%s' % (e,), 'Error')
            return
        password = self.config.get('password', '')
        if password:
            try:
                c.password(password)
            except Exception as e:
                self.core.information('%s' % (e,), 'Error')
                return
        current = c.currentsong()
        current_time = float(c.status()['elapsed'])

        s = '%(artist)s - %(title)s (%(album)s)' % current
        if 'full' in args:
            pourcentage = int(current_time / float(current['time']) * 10)
            s += ' \x192[\x191' + '-'*(pourcentage-1) + '\x193+' + '\x191' + '-' * (10-pourcentage-1) + '\x192]\x19o'
        if not self.core.send_message('%s' % (s,)):
            self.core.information('Cannot send result (%s), this is not a conversation tab' % result)

    def completion_mpd(self, the_input):
        return the_input.auto_completion(['full'])