feat: add function to check if spotify is running
This commit is contained in:
parent
6dc4dca7e0
commit
692acdfa0f
@ -31,30 +31,59 @@ class NoActiveTrackException(RuntimeError):
|
||||
"""
|
||||
|
||||
|
||||
def _get_spotify_dbus_obj():
|
||||
"""Gets the spotify object from the current dbus session bus.
|
||||
_SPOTIFY_BUS_NAME = "org.mpris.MediaPlayer2.spotify"
|
||||
_SPOTIFY_BUS_PLAYER_OBJECT_PATH = "/org/mpris/MediaPlayer2"
|
||||
|
||||
|
||||
def spotify_is_running():
|
||||
"""Checks whether the spotify executable is running.
|
||||
|
||||
This is done by checking whether the spotify bus is advertised as available
|
||||
by dbus.
|
||||
|
||||
Returns
|
||||
-------
|
||||
`spotify_dbus_object` : (dbus.proxies.ProxyObject)[https://dbus.freedesktop.org/doc/dbus-python/dbus.proxies.html#dbus.proxies.ProxyObject]
|
||||
`True`
|
||||
If spotify is running.
|
||||
`False`
|
||||
If spotify is not running.
|
||||
"""
|
||||
|
||||
# Connect to the session bus
|
||||
session_bus = dbus.SessionBus()
|
||||
|
||||
# Check if spotify is in the list of clients
|
||||
return _SPOTIFY_BUS_NAME in session_bus.list_names()
|
||||
|
||||
|
||||
def _get_spotify_dbus_obj():
|
||||
"""Gets the spotify player object from the spotify bus.
|
||||
|
||||
Returns
|
||||
-------
|
||||
`spotify_player_dbus_object` : (dbus.proxies.ProxyObject)[https://dbus.freedesktop.org/doc/dbus-python/dbus.proxies.html#dbus.proxies.ProxyObject]
|
||||
A dbus-python proxy object for the spotify dbus object.
|
||||
|
||||
Raises
|
||||
------
|
||||
`SpotifyNotRunningException`
|
||||
When dbus fails to locate the spotify object.
|
||||
When the spotify executable is not running.
|
||||
"""
|
||||
|
||||
# Connect to the session bus
|
||||
session_bus = dbus.SessionBus()
|
||||
bus_data = ("org.mpris.MediaPlayer2.spotify", "/org/mpris/MediaPlayer2")
|
||||
try:
|
||||
spotify_dbus_object = session_bus.get_object(*bus_data)
|
||||
except dbus.exceptions.DBusException as e:
|
||||
raise SpotifyNotRunningException(
|
||||
"The spotify executable is not running."
|
||||
) from e
|
||||
|
||||
return spotify_dbus_object
|
||||
if not spotify_is_running():
|
||||
raise SpotifyNotRunningException(
|
||||
"Failed to connect to the spotify bus: Spotify not running."
|
||||
)
|
||||
|
||||
# Get and return the proxy object for the spotify player dbus object
|
||||
return session_bus.get_object(
|
||||
_SPOTIFY_BUS_NAME,
|
||||
_SPOTIFY_BUS_PLAYER_OBJECT_PATH
|
||||
)
|
||||
|
||||
|
||||
|
||||
def _get_interface(interface_name):
|
||||
|
Loading…
Reference in New Issue
Block a user