I’m building a custom Python tool for my desktop setup and need to fetch the current song details (artist and track name) from Spotify while it’s playing.
I’ve searched for working libraries but most seem outdated or broken. The official Spotify API doesn’t seem to offer this functionality either.
One approach I considered was extracting the window title from the Spotify desktop client since it shows the current track info. However, I’m having trouble implementing this method.
I’m already using psutil in my project so I have access to Spotify’s process ID if that helps.
Does anyone know how to:
- Extract window title information from a running application using Python
- Any alternative methods to get current playback data from Spotify’s desktop or web player
I’m working on Linux and would prefer a Python solution but I’m open to using shell commands if needed.
Any suggestions would be really helpful!
I’ve encountered a similar issue while working on a desktop integration project. The method of extracting the window title you mentioned can be effective on Linux, although it might seem a bit unconventional. You can indeed use xdotool search --name "Spotify" getwindowname to retrieve the current track information from the Spotify window. Just run that command through Python’s subprocess module, and it should work. However, I’ve found using D-Bus to interface directly with the Spotify client to be more reliable. The desktop application provides media information through MPRIS, allowing you to access track name, artist, and playback status seamlessly using the pydbus library. This approach is advantageous as it remains functional even if the window title changes or Spotify is minimized. Additionally, it offers extra information like album artwork and track duration, which could enhance your desktop tool.