Пример #1
0
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'
Пример #2
0
class MPDConnection(object):
    def __init__(self):
        self.client = MPDClient()
        self.port = 6600
        self._connect()

    def _connect(self):
        self.client.connect("localhost", self.port)

    def _is_connected(self):
        try:
            self.client.ping()
            return True
        except ConnectionError:
            return False

    def _ensure_connected(self):
        if self._is_connected() is True:
            return True
        self._connect()
        return True

    def get_tracks_on_playlist(self):
        self._ensure_connected()
        return self.client.playlist()

    def add_to_playlist(self, to_add):
        if isinstance(to_add, str):
            to_add = [to_add]
        for eachone in to_add:
            self.client.add(eachone)

    def ensure_playing(self):
        self._ensure_connected()
        if self.client.status()["state"] != "play":
            self.client.play()
        self.client.repeat(1)

    def __getattr__(self, attrname):
        return getattr(self.client, attrname)
Пример #3
0
class OrpheusLibrarySearch(QWidget):
    
    def __init__(self):
        super().__init__()
        self.initUI()
        self.populateList()
        
    def initUI(self):
        #palette = QPalette()
        #palette.setColor(QPalette.Background, QColor('#383C4A'))        
        #palette.setColor(QPalette.WindowText, QColor('#C1C1C1'))        
        #self.setPalette(palette)
        
        self.setMaximumSize(492, 653)
        self.setMinimumSize(492, 653)
        
        le = QLineEdit(self)
        le.textChanged[str].connect(self.onChanged)
        le.returnPressed.connect(self.onActivation)
        le.setClearButtonEnabled(True)
        le.setPlaceholderText('Start typing to search...')
        self.lw = QListWidget()
        self.visibleLw = QListWidget()
        #palette.setColor(QPalette.Base, QColor('#383C4A'))                        
        #self.visibleLw.setPalette(palette)        
        self.visibleLw.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.visibleLw.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)        
        self.visibleLw.itemActivated.connect(self.onActivation)
        self.scrollBar = QScrollBar()
        self.visibleLw.verticalScrollBar().valueChanged.connect(self.scrollBar.setValue)
        self.scrollBar.valueChanged.connect(self.visibleLw.verticalScrollBar().setValue)
        vbox = QVBoxLayout()
        vbox.setSpacing(3)
        #vbox.setContentsMargins(3, 3, 3, 3)
        vbox.setContentsMargins(0, 4, 0, 0)        
        vbox.addWidget(le)       
        hbox = QHBoxLayout()
        hbox.setSpacing(0)
        #hbox.setContentsMargins(0, 0, 0, 0)                
        hbox.addWidget(self.visibleLw)
        hbox.addWidget(self.scrollBar)
        vbox.addLayout(hbox)
        self.setLayout(vbox)
        
        self.setWindowTitle('Music Library')
        
        icon = QIcon.fromTheme('musique')
        self.setWindowIcon(icon)
        
    def populateList(self):
        self.client = MPDClient()
        self.client.connect('localhost', 6600)        
        self.playlistinfo = self.client.playlistinfo()
        self.client.disconnect()
                
        self.playlist = []
        
        #backgroundColor = QColor('#383C4A')
        #foregroundColor = QColor('#C1C1C1')
        
        for i in self.playlistinfo:
            row = ''
            if 'album' in i:
                row = row + i['album'] + ' - '
            if 'title' in i:
                if isinstance(i['title'], str):
                    row = row + i['title']
                else:
                    row = row + i['title'][0]
            if 'artist' in i:
                row = row + ' - ' + i['artist']
            self.playlist.append(row)
            #newRow = QListWidgetItem(row)
            #newRow.setBackground(backgroundColor)
            #newRow.setForeground(foregroundColor)
            #self.lw.addItem(newRow)
        self.visibleLw.addItems(self.playlist)
        self.visibleLw.setCurrentRow(0)
        
    def get_matches(self, pattern):
        self.visibleLw.clear()
        pattern = '.*' + pattern.replace(' ', '.*').lower()
        regexp = re.compile(pattern)
        for i in self.playlist:
            if regexp.match(i.lower()):
                self.visibleLw.addItem(i)                
                
    def formatScrollBar(self):            
        self.scrollBar.setMaximum(self.visibleLw.verticalScrollBar().maximum())                    
        self.scrollBar.setPageStep(self.visibleLw.verticalScrollBar().pageStep())
        
    def onChanged(self, text):
        self.get_matches(text)
        self.visibleLw.setCurrentRow(0)
        self.scrollBar.setMaximum(self.visibleLw.verticalScrollBar().maximum())        
        if self.visibleLw.verticalScrollBar().maximum() == 0:
            self.scrollBar.setVisible(False)
        else:
            self.scrollBar.setVisible(True)

    def onActivation(self):
        selected_song = self.visibleLw.currentItem().text()
        for i in range(0, len(self.playlist)):
            if selected_song == self.playlist[i]:
                self.client.connect('localhost', 6600)
                self.client.play(i)
                self.client.disconnect()       
        
    def keyPressEvent(self, e):
        if e.key() == Qt.Key_Down:
            self.visibleLw.setFocus()
        elif e.key() == Qt.Key_Escape:
            self.close()
Пример #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
Пример #5
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