def __init__(self, quiet=False): super(SpottyCLI, self).__init__() self._loop = gobject.MainLoop() self._spot = SpotifyControl() self._spot.track_changed.connect(self.cb_track_changed) self._quiet = quiet self._spot.connect()
class SpottyCLI(threading.Thread): """Commandline Spottty operator.""" def __init__(self, quiet=False): super(SpottyCLI, self).__init__() self._loop = gobject.MainLoop() self._spot = SpotifyControl() self._spot.track_changed.connect(self.cb_track_changed) self._quiet = quiet self._spot.connect() def run(self): LOG.debug("Starting mainloop") self._loop.run() LOG.debug("Mainloop ended") def terminate(self): """Terminates the mainloop.""" if self._loop.is_running(): LOG.debug("Terminating mainloop") self._loop.quit() def send_command(self, command): """Send given command to Spotify.""" LOG.debug("Sending command") if self._spot.connected: getattr(self._spot, command)() LOG.debug("Command sent") else: LOG.error("Failed to connect spotify") def cb_track_changed(self, **info): """Spotty track_changed handler.""" LOG.debug("Track changed") if not self._quiet: artist = info.get("artist", u"") title = info.get("title", u"") print("%s - %s" % (artist, title)) self.terminate()