four43
four43

Reputation: 1750

Discrepancy between `dbus-send` and Python's `dbus` using Spotify

I am unable to re-create a dbus-send command in Python. dbus-send does the expected, running:

#!/bin/bash
dbus-send \
    --type=method_call \
    --dest=org.mpris.MediaPlayer2.spotify \
    /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.OpenUri \
    string:spotify:playlist:74sUjcvpGfdOvCHvgzNEDO
  1. Changes the Spotify player over to viewing the requested playlist
  2. Starts playing the first song in the playlist

When re-creating in Python, the oddest thing happens - it half works. It changes the Spotify player to the playlist but doesn't play the first song (and sort of breaks the player). This also happens when manually running the call from the QDbusViewer debug application.

In a minimal python example, that looks like:

#!/usr/bin/python3

import dbus

session_bus = dbus.SessionBus()
spotify = session_bus.get_object("org.mpris.MediaPlayer2.spotify", "/org/mpris/MediaPlayer2")
spotify_iface = dbus.Interface(spotify, dbus_interface='org.mpris.MediaPlayer2.Player')
spotify_iface.OpenUri("string:spotify:playlist:74sUjcvpGfdOvCHvgzNEDO")
  1. Changes the Spotify player over to viewing the Playlist
  2. Audio of playing song stops, yet display still says playing
  3. Unable to control current song via play/pause/next buttons in player
  4. No exceptions are thrown, exits 0
  5. Other method calls like PlayPause() seem to work as expected

This is super strange to me because I would have thought the messages sent to DBus would have been identical. Does anyone know where the discrepancy might come from?

Upvotes: 1

Views: 459

Answers (2)

ukBaz
ukBaz

Reputation: 7964

At the bottom of https://www.freedesktop.org/wiki/Software/DBusBindings/ it suggests that dbus-python should not be used for new projects.

With pydbus your example would look like:

import pydbus
bus = pydbus.SessionBus()
spotify = bus.get('org.mpris.MediaPlayer2.spotify', '/org/mpris/MediaPlayer2')
spotify.OpenUri('spotify:playlist:74sUjcvpGfdOvCHvgzNEDO')

pydbus also has some better introspection so you can do print(dir(spotify)) to see all the available methods and properties.

Upvotes: 2

four43
four43

Reputation: 1750

Solved!

The issue came down to a small syntax error:

dbus-send [...] string:spotify:playlist:74sUjcvpGfdOvCHvgzNEDO

That prefix string is required in dbus-send so it knows the data type of the argument.

In Python however, it's adding taking care of that for us using some reflection on the dbus provider:

spotify.OpenUri('spotify:playlist:74sUjcvpGfdOvCHvgzNEDO')

We can drop the leading string:. Darn!

I setup my own listener by using https://github.com/LEW21/pydbus/blob/master/doc/tutorial.rst#exporting-own-objects to inspect what data was getting sent back.

Upvotes: 1

Related Questions