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 __call__(self, *args, **kwargs): try: return self._callback(*args, **kwargs) except Exception as exc: LOG.error("Listener %s (%s, %s) failed:%s" % (self, args, kwargs, exc)) if not self._ignore_errors: raise
def notifyservice(self): """Lazy binding to dbus notification interface.""" if self._notifyservice == None: try: proxy = self.spotify.bus.get_object( "org.freedesktop.Notifications", "/org/freedesktop/Notifications") self._notifyservice = dbus.Interface(proxy, "org.freedesktop.Notifications") except Exception, exobj: LOG.error("Notification service connectin failed: %s" % exobj)
def connect(self): """Connect to spotify dbus interface.""" LOG.debug("Connecting") try: self.spotifyservice = self.bus.get_object( "com.spotify.qt", "/org/mpris/MediaPlayer2") self.spotifyservice.connect_to_signal( "PropertiesChanged", self._cb_track_changed) self.connected = True self.state_changed.send(True) except Exception, exobj: LOG.error("Connection to Spotify failed: %s" % exobj)
def fetch(self, url): """Fetch cover image based on URL.""" _, file_name = os.path.split(url) cover_file, exists = self._check_cache(file_name) if not exists: LOG.debug("Downloading %s", url) try: open(cover_file, "w").write(urllib2.urlopen(url).read()) except Exception: LOG.error("Failed to download: %s", url) traceback.print_exc() else: LOG.debug("Cover in cache") return cover_file
def cb_handle_mediakey(self, app, key): """Media key event callback.""" LOG.debug("Media key event %s %s" % (app, key)) if app != "spotty": return if self._interface is None: LOG.error("Disconnected, but got media key?!?!?!") return if key not in self.KEY_MAP: LOG.debug("No method to handle key %s" % key) return action = getattr(self.spotify, self.KEY_MAP[key], None) if action: action() else: LOG.error("%s has no method %s" % (self.spotify, self.KEY_MAP[key]))
def _cb_spotify_spy(self, *args): """DBus listener for spying spotify start/quit.""" try: name, old, new = args except (ValueError, TypeError): LOG.error("Bad values: %s" % args) if name != "com.spotify.qt": return if not old and new: LOG.debug("Spotify appeared!") self.connect() elif old and not new: LOG.debug("Spotify went away") self.spotifyservice = None self.connected = False self.state_changed.send(False) else: LOG.debug("Spotify dbus interface did something weird")
def main(): """Entry point""" # TODO configuration file handling and commandline options options, _ = parse_args() if options.debug: LOG.setLevel(logging.DEBUG) DBusGMainLoop(set_as_default=True) spotify = SpotifyControl() # Load plugins enabled = ["GnomeMediaKeys", "Notify", "CoverFetcher"] plugins = {} LOG.debug("Loading plugins...") for entry in pkg_resources.iter_entry_points("spotty.plugin"): plugin = entry.load() if plugin.__name__ not in enabled: LOG.debug("%s not enabled, skipping...", plugin.__name__) continue LOG.debug("Loading plugin %s", plugin.__name__) try: plugins[plugin.__name__] = plugin.load(spotify) except Exception: LOG.error("Failed to load %s", plugin.__name__) traceback.print_exc() # Start the mainloop spotify.connect() loop = gobject.MainLoop() signal.signal(signal.SIGINT, lambda *args: loop.quit()) loop.run() # Unload plugins for name, plugin in plugins.iteritems(): LOG.debug("Unloading %s", name) try: plugin.unload() except Exception: LOG.error("Failed to unload %s", name) traceback.print_exc()