def play(self, track_id): track = self.spotify.getTrack(track_id) if track['thumb'] == '': track['thumb'] = _DEFAULT_ICON if track['title'] == '': track['title'] = _DEFAULT_TITLE if self.kodi: self.listitem.setArt({'thumb': track['thumb']}) self.listitem.setInfo( 'music', { 'album': track['album'], 'artist': track['artist'], 'title': track['title'] } ) if self.isPlaying() and self.getPlayingFile() == _STREAM: log('updating librespot playback') self.updateInfoTag(self.listitem) else: self.librespot.unsuspend() log('starting librespot playback') super(Player, self).play(_STREAM, self.listitem) else: self.dialog.notification( track['title'], track['artist'], icon=track['thumb'], sound=False)
def play(self, track_id): track = self.spotify.getTrack(track_id) if track['thumb'] == '': track['thumb'] = _DEFAULT_ICON if track['title'] == '': track['title'] = _DEFAULT_TITLE if self.kodi: self.listitem.setArt({'thumb': track['thumb']}) self.listitem.setInfo( 'music', { 'album': track['album'], 'artist': track['artist'], 'title': track['title'] }) if self.isPlaying() and self.getPlayingFile() == _STREAM: log('updating librespot playback') self.updateInfoTag(self.listitem) else: self.librespot.unsuspend() log('starting librespot playback') super(Player, self).play(_STREAM, self.listitem) else: self.dialog.notification(track['title'], track['artist'], icon=track['thumb'], sound=False)
def stop(self): if self.isPlaying(): if self.getPlayingFile() == _STREAM: log('stopping librespot playback') super(Player, self).stop() else: self.librespot.stop() else: self.librespot.restart()
def getTrack(self, track_id): try: if time.time() > self.expiration: log('token expired') token = json.loads( urllib2.urlopen(urllib2.Request(*self.request)).read()) log('token {} expires in {} seconds'.format( token['access_token'], token['expires_in'])) self.expiration = time.time() + float(token['expires_in']) - 60 self.headers = { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': 'Bearer {}'.format(token['access_token']) } track = json.loads( urllib2.urlopen( urllib2.Request( 'https://api.spotify.com/v1/tracks/{}'.format( track_id), None, self.headers)).read()) except Exception as e: log('failed to get track {} from Spotify: {}'.format(e)) track = dict() return { 'album': _get('', track, 'album', 'name'), 'artist': _get('', track, 'artists', 0, 'name'), 'duration': _get(0, track, 'duration_ms') / 1000, 'thumb': _get('', track, 'album', 'images', 0, 'url'), 'title': _get('', track, 'name') }
def getTrack(self, track_id): try: if time.time() > self.expiration: log('token expired') token = json.loads(urllib2.urlopen( urllib2.Request(*self.request)).read()) log('token {} expires in {} seconds'.format( token['access_token'], token['expires_in'])) self.expiration = time.time() + float(token['expires_in']) - 60 self.headers = { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': 'Bearer {}'.format(token['access_token']) } track = json.loads(urllib2.urlopen(urllib2.Request( 'https://api.spotify.com/v1/tracks/{}'.format(track_id), None, self.headers)).read()) except Exception as e: log('failed to get track {} from Spotify: {}'.format(e)) track = dict() return { 'album': _get('', track, 'album', 'name'), 'artist': _get('', track, 'artists', 0, 'name'), 'duration': _get(0, track, 'duration_ms') / 1000, 'thumb': _get('', track, 'album', 'images', 0, 'url'), 'title': _get('', track, 'name') }
def run(self): log('named pipe started') try: os.unlink(_FIFO) except OSError: pass os.mkfifo(_FIFO) while (os.path.exists(_FIFO) and stat.S_ISFIFO(os.stat(_FIFO).st_mode)): with open(_FIFO, 'r') as fifo: command = fifo.read().splitlines() log('named pipe received {}'.format(str(command))) if len(command) == 0: break elif command[0] == 'play': self.play(command[1]) elif command[0] == 'stop': self.stop() elif command[0] == 'pause': self.pause() try: os.unlink(_FIFO) except OSError: pass log('named pipe stopped')
def stop(self): if self.state: log('stopping librespot') _systemctl('stop') self.state = False
def pause(self): if self.isPlaying() and self.getPlayingFile() == _STREAM: log('pausing librespot playback') super(Player, self).pause()
def _pactl(bit): log('pactl {}'.format(bit)) subprocess.call(['pactl', 'suspend-sink', _SINK, bit])
def onPlayBackStopped(self): log('a playback stopped') self.librespot.restart()
def onPlayBackEnded(self): log('a playback ended') self.librespot.restart()
def onAbortRequested(self): log('abort requested') with open(_FIFO, 'w') as fifo: fifo.close() self.join()
def __init__(self): log('monitor started') self.player = Player()
def restart(self): log('restarting librespot') _systemctl('restart') self.state = True
def _systemctl(command): log('systemctl {}'.format(command)) subprocess.call(['systemctl', command, _SERVICE]) _pactl('1')
def onPlayBackStarted(self): log('a playback started') if self.getPlayingFile() != _STREAM: self.librespot.stop()
def onSettingsChanged(self): log('settings changed') self.stop() self.updateSettings()
def waitForAbort(self): super(Monitor, self).waitForAbort() self.player.onAbortRequested() log('monitor stopped')