def _grab(self): """Grab the media key control.""" LOG.debug("Grabing media keys") self._interface.GrabMediaPlayerKeys( "spotty", 0, dbus_interface='org.gnome.SettingsDaemon.MediaKeys') self._grabbed = True
def _call_spotify(self, method): """Helper to call spotify method.""" if not self.connected: LOG.debug("Not connected") return self.spotifyservice.get_dbus_method(method, "org.mpris.MediaPlayer2.Player")()
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()
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 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_track_changed(self, *args): """Metadata change listener.""" clear_data = {} try: data = args[1]["Metadata"] except (IndexError, KeyError): return if not data: return for key, name in META_MAP.items(): clear_data[key] = data.get(name, None) # Parse year if clear_data["date"]: clear_data["year"] = clear_data["date"].split("-")[0] else: clear_data["year"] = None if isinstance(clear_data["artist"], dbus.Array): clear_data["artist"] = " - ".join(clear_data["artist"]) # TODO: Convert all the dbus data types LOG.debug(str(clear_data)) self._current_track = clear_data self.track_changed.send(**clear_data)
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()
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 terminate(self): """Terminates the mainloop.""" if self._loop.is_running(): LOG.debug("Terminating mainloop") self._loop.quit()
def run(self): LOG.debug("Starting mainloop") self._loop.run() LOG.debug("Mainloop ended")
def _release(self): """Release the media key control.""" LOG.debug("Releasing media keys") self._interface.ReleaseMediaPlayerKeys("spotty", dbus_interface='org.gnome.SettingsDaemon.MediaKeys') self._grabbed = False
def cb_track_changed(self, *_, **info): """Track change callback listener.""" if info.has_key("art_url"): LOG.debug("fetching art url %s" % info["art_url"]) return {"cover": self.fetch(info["art_url"])}