Example #1
0
class MPD(BarItem):

    def __init__(self, server="localhost",port=6600):
        self.mpc = MPDClient()
        self.server = server
        self.port = port
        BarItem.__init__(self, "MPD")
        self.output['name'] = "NowPlaying"
        self.update()

    def update(self):
        try:
            self.mpc.connect(self.server, self.port)
            status = self.mpc.status()
            if (status['state'] == "play"):
                song = self.mpc.currentsong()
                self.output['full_text'] = '► ' + song['artist'] + ' - ' + song['album'] + ' - ' + song['title']
            elif (status['state'] == "pause"):
                song = self.mpc.currentsong()
                self.output['full_text'] = '" ' + song['artist'] + ' - ' + song['album'] + ' - ' + song['title']
            else:
                self.output['full_text'] = status['state']
            self.mpc.disconnect()
        except:
            self.output['full_text'] = "MPD disconnected"
Example #2
0
File: mpc.py Project: dyuri/rcfiles
class Client(Notifier):
    """
    The host and port are 127.0.0.1 and 6600 by default but can be set by passing these
    when initiating the client.

    The notification timeout can be changed by setting Client.timeout to milliseconds
    (int) or -1, which then uses the notification server's default timeout.
    """
    defaults = [
        ('summary', 'Music', 'Notification summary.'),
        ('host', '127.0.0.1', 'IP address of MPD server.'),
        ('port', '6600', 'Port of MPD server.'),
    ]

    def __init__(self, **config):
        Notifier.__init__(self, **config)
        self.add_defaults(Client.defaults)

        self.client = MPDClient()
        self.client.host = self.host
        self.client.port = self.port

    @_client_func
    def toggle(self):
        if self.client.status()['state'] == 'play':
            self.client.pause()
        else:
            self.client.play()
        return bodies.get(self.client.status()['state'])

    @_client_func
    def next(self):
        self.client.next()
        current = self.client.currentsong()
        return f"{current['artist']} - {current['title']}"

    @_client_func
    def previous(self):
        self.client.previous()
        current = self.client.currentsong()
        return f"{current['artist']} - {current['title']}"

    @_client_func
    def stop(self):
        self.client.stop()
        return 'Stopped'
Example #3
0
class KissMPD():
    def handle_idle(func):
        def inner(self, *args, **kwargs):
            self.__lock.acquire()
            self.__client.noidle()
            res = func(self, *args, **kwargs)
            self.__client.send_idle()
            self.__lock.release()
            return res

        return inner

    def __init__(self, fp):
        self.__fp = fp
        self.__client = MPDClient()
        self.__lock = threading.Lock()
        self.__idle_client = MPDClient()

    def connect(self):
        self.__client.connect('localhost', 6600)
        self.__idle_client.connect('localhost', 6600)
        status = self.__client.status()
        self.__state = status['state']
        self.__volume = int(status['volume'])
        try:
            self.__songid = status['songid']
        except:
            self.__songid = None
        self.__client.send_idle()
        self.__idle_client.send_idle()

    def disconnect(self):
        self.__client.disconnect()
        self.__idle_client.disconnect()

    def process(self):
        canRead = select([self.__idle_client], [], [], 0)[0]
        if canRead:
            changes = self.__idle_client.fetch_idle()
            self.__idle_client.send_idle()  # continue idling
            change = self.update_status()
            return change
        return None

    @handle_idle
    def refresh(self):
        self.display_current_song()

    def display_current_song(self):
        if (self.__songid):
            song = self.__client.currentsong()
            try:
                text = song['artist'] + ' - ' + song['title']
            except:
                text = song['file'].split('/')[-1]

            self.__fp.set_scrolling_text(3, 1, text + '           ')
            try:
                time = divmod(int(song['time']), 60)
                self.__fp.set_track_clock('0{0[0]:02}{0[1]:02}'.format(time))
            except:
                self.__fp.set_track_clock(None)
        else:
            self.__fp.clear()

    def display_volume(self):
        self.__fp.set_flash_text(8, 'Volume: ' + str(self.__volume))

    def display_state(self):
        if (self.__state != 'play'):
            self.__fp.clear()
            self.__fp.set_text('{0:^12}'.format(self.__state))
        else:
            self.display_current_song()

    @handle_idle
    def update_status(self):
        status = self.__client.status()

        try:
            songid = status['songid']
        except:
            songid = None

        if (songid != self.__songid):
            self.__songid = songid
            self.display_current_song()

        if (int(status['volume']) != self.__volume):
            self.__volume = int(status['volume'])
            self.display_volume()

        state = status['state']
        if (state != self.__state):
            self.__state = state
            self.display_state()
            return state

        return None

    @handle_idle
    def volume_down(self):
        self.__volume = self.__volume - 1
        if (self.__volume < 0):
            self.__volume = 0
        self.__client.setvol(self.__volume)
        self.display_volume()

    @handle_idle
    def volume_up(self):
        self.__volume = self.__volume + 1
        if (self.__volume > 100):
            self.__volume = 100
        self.__client.setvol(self.__volume)
        self.display_volume()

    @handle_idle
    def play(self, toggle=False):
        if (self.__state == 'play'):
            if toggle:
                self.__client.pause()
        else:
            self.__client.play()

    def play_pause(self):
        self.play(True)

    @handle_idle
    def pause(self):
        self.__client.pause(1)

    @handle_idle
    def previous(self):
        self.__client.previous()

    @handle_idle
    def next(self):
        self.__client.next()

    @handle_idle
    def stop(self):
        self.__client.stop()

    def handle_inputevent(self, ie):
        key2function = {
            'volume_down': self.volume_down,
            'volume_up': self.volume_up,
            'stop': self.stop,
            'play': self.play_pause,
            'next_track': self.next,
            'previous_track': self.previous,
            'right': self.next,
            'left': self.previous,
            'down': self.volume_down,
            'up': self.volume_up,
            'ok': self.play_pause,
            'mute': self.play_pause,
        }
        try:
            function = key2function[ie.key]
        except:
            function = None
        if not function:
            return False

        if (ie.type == InputEventType.hold):
            if (function == self.volume_down) or (function == self.volume_up):
                function()
        elif (ie.type == InputEventType.pressed):
            function()
        return True
Example #4
0
class KissMPD():
    def handle_idle(func):
        def inner(self, *args, **kwargs):
            self.__lock.acquire()
            self.__client.noidle()
            res = func(self, *args, **kwargs)
            self.__client.send_idle()
            self.__lock.release()
            return res
        return inner

    def __init__(self, fp):
        self.__fp = fp
        self.__client = MPDClient()
        self.__lock = threading.Lock()
        self.__idle_client = MPDClient()

    def connect(self):
        self.__client.connect('localhost', 6600)
        self.__idle_client.connect('localhost', 6600)
        status = self.__client.status()
        self.__state = status['state']
        self.__volume = int(status['volume'])
        try:
            self.__songid = status['songid']
        except:
            self.__songid = None
        self.__client.send_idle()
        self.__idle_client.send_idle()

    def disconnect(self):
        self.__client.disconnect()
        self.__idle_client.disconnect()

    def process(self):
        canRead = select([self.__idle_client], [], [], 0)[0]
        if canRead:
            changes = self.__idle_client.fetch_idle()
            self.__idle_client.send_idle() # continue idling
            change = self.update_status()
            return change
        return None

    @handle_idle
    def refresh(self):
        self.display_current_song()

    def display_current_song(self):
        if(self.__songid):
            song = self.__client.currentsong()
            try:
                text = song['artist'] + ' - ' + song['title']
            except:
                text = song['file'].split('/')[-1]

            self.__fp.set_scrolling_text(3, 1, text)
            try:
                time = divmod(int(song['time']), 60)
                self.__fp.set_track_clock('0{0[0]:02}{0[1]:02}'.format(time))
            except:
                self.__fp.set_track_clock(None)
        else:
            self.__fp.clear()

    def display_volume(self):
        self.__fp.set_flash_text(8, 'Volume: ' + str(self.__volume))

    def display_state(self):
        if(self.__state != 'play'):
            self.__fp.clear()
            self.__fp.set_text('{0:^12}'.format(self.__state))
        else:
            self.display_current_song()

    @handle_idle
    def update_status(self):
        status = self.__client.status()

        try:
            songid = status['songid']
        except:
            songid = None

        if(songid != self.__songid):
            self.__songid = songid
            self.display_current_song()

        if(int(status['volume']) != self.__volume):
            self.__volume = int(status['volume'])
            self.display_volume()

        state = status['state']
        if(state != self.__state):
            self.__state = state
            self.display_state()
            return state

        return None

    @handle_idle
    def volume_down(self):
        self.__volume = self.__volume - 1
        if(self.__volume < 0):
            self.__volume = 0
        self.__client.setvol(self.__volume)
        self.display_volume()

    @handle_idle
    def volume_up(self):
        self.__volume = self.__volume + 1
        if(self.__volume > 100):
            self.__volume = 100
        self.__client.setvol(self.__volume)
        self.display_volume()

    @handle_idle
    def play(self, toggle=False):
        if(self.__state == 'play'):
            if toggle:
                self.__client.pause()
        else:
            self.__client.play()

    def play_pause(self):
        self.play(True)

    @handle_idle
    def pause(self):
        self.__client.pause(1)

    @handle_idle
    def previous(self):
        self.__client.previous()

    @handle_idle
    def next(self):
        self.__client.next()

    @handle_idle
    def stop(self):
        self.__client.stop()

    def handle_inputevent(self, ie):
        key2function = {
            'volume_down'    : self.volume_down,
            'volume_up'      : self.volume_up,
            'stop'           : self.stop,
            'play'           : self.play_pause,
            'next_track'     : self.next,
            'previous_track' : self.previous,
            'right'          : self.next,
            'left'           : self.previous,
            'down'           : self.volume_down,
            'up'             : self.volume_up,
            'ok'             : self.play_pause,
            'mute'           : self.play_pause,
        }
        try:
            function = key2function[ie.key]
        except:
            function = None
        if not function:
            return False

        if(ie.type == InputEventType.hold):
            if(function == self.volume_down) or (function == self.volume_up):
                function()
        elif(ie.type == InputEventType.pressed):
            function()
        return True
Example #5
0
#!/usr/bin/env python2

from musicpd import MPDClient
import os

fifo_name = os.getenv("XDG_RUNTIME_DIR", "/tmp") + "/powerstatus10k/fifos/mpd"
mpd_host = os.getenv("MPD_HOST", "127.0.0.1")
mpd_port = os.getenv("MPD_PORT", "6600")

client = MPDClient()
client.connect(mpd_host, mpd_port)

while True:
    client.send_idle()
    client.fetch_idle()

    status = client.status()
    state = status["state"] if "state" in status else "stop"

    song = client.currentsong()
    artist = song["artist"] if "artist" in song else "Unknown Artist"
    title = song["title"] if "title" in song else "Unknown Title"

    # Do this here to avoid problems on a deleted FIFO during runtime.
    if not os.path.exists(fifo_name):
        os.mkfifo(fifo_name)

    with open(fifo_name, "w") as fifo:
        fifo.write(f"{state}:{artist}:{title}")