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

from plugin import BasePlugin
from common import shell_split
from os.path import basename as base
import tabs
import mpd
import threading
from select import select
from time import sleep

class UpdateThread(threading.Thread):
    """
    Background thread that awaits mpd changes
    """

    def __init__(self, plugin, xmpp):
        threading.Thread.__init__(self)
        self.plugin = plugin
        self.xmpp = xmpp
        self.alive = False
        self.c = mpd.MPDClient()

    def run(self, *args, **kwargs):
        self.alive = True
        current = None
        while self.alive:
            try:
                self.c.connect(host=self.plugin.config.get('host', 'localhost'), port=self.plugin.config.get('port', '6600'))
                password = self.plugin.config.get('password', '')
                if password:
                    self.c.password(password)
                self.c.send_idle()
                select([self.c], [], [], timeout=600)
                self.c.fetch_idle()
                status = self.c.status()
                if status['state'] == 'play' and self.alive:
                    song = self.c.currentsong()
                    if current != song:
                        self.xmpp.plugin['xep_0118'].publish_tune(artist=song.get('artist'),
                                length=song.get('time'), title=song.get('title'),
                                track=song.get('track'), block=False)
                    current = song
                elif status['state'] != 'play':
                    self.xmpp.plugin['xep_0118'].stop(block=False)
                    current = None
                self.c.disconnect()
            except:
                pass
            finally:
                try:
                    self.c.disconnect()
                except:
                    pass
                sleep(8)

class Plugin(BasePlugin):
    def init(self):
        for _class in (tabs.ConversationTab, tabs.MucTab, tabs.PrivateTab):
            self.api.add_tab_command(_class, 'mpd', self.command_mpd,
                    usage='[full]',
                    help='Sends a message showing the current song of an MPD instance. If full is provided, the message is more verbose.',
                    short='Send the MPD status',
                    completion=self.completion_mpd)
        if self.config.get('broadcast', 'true').lower() != 'false':
            self.core.xmpp.register_plugin('xep_0118')
            self.thread = UpdateThread(plugin=self, xmpp=self.core.xmpp)
            self.thread.start()

    def cleanup(self):
        self.thread.alive = False
        self.thread.c.disconnect()
        self.core.xmpp.plugin['xep_0118'].stop(block=False)

    def command_mpd(self, args):
        args = shell_split(args)
        c = mpd.MPDClient()
        c.connect(host=self.config.get('host', 'localhost'), port=self.config.get('port', '6600'))
        password = self.config.get('password', '')
        if password:
            c.password(password)
        current = c.currentsong()
        artist = current.get('artist', 'Unknown artist')
        album = current.get('album', 'Unknown album')
        title = current.get('title', base(current.get('file', 'Unknown title')))

        s = '%s - %s (%s)' % (artist, title, album)
        if 'full' in args:
            if 'elapsed' in current and 'time' in current:
                current_time = float(c.status()['elapsed'])
                percents = int(current_time / float(current['time']) * 10)
                s += ' \x192}[\x191}' + '-'*(percents-1) + '\x193}+' + '\x191}' + '-' * (10-percents-1) + '\x192}]\x19o'
        if not self.api.send_message('%s' % (s,)):
            self.api.information('Cannot send result (%s)' % s, 'Error')

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