Blocking rap on Spotify
I never liked rap or hip-hop, but I keep getting it in my radios and automatic playlists on Spotify. There’s no way to blacklist a genre in Spotify. But there’s a way to do it with an “add-on”.
Spotify exposes the track info and player control API via MPRIS. That is, on Linux. This probably won’t work on Windows or Android. Anyway, what you can do is: listen for new track events, figure out what songs probably suck, and send the Next event to the player. Incidentally, this works with any MPRIS-compatible player, not just Spotify.
A “fun” twist is that Spotify does not provide genre information at all. I solved this using the MusicBrainz API. The API is based on XML, so it’s kind of a mess. Luckily, they also have an experimental JSON version of it. It doesn’t know every rapper out there, but the coverage is quite good in my experience.
Without further ado, here’s the script: MightyPork/rapblock. You can submit issues (after registration), and download a pre-built x86_64 binary from the releases tab.
I wrote it in rust, so you might object to calling it a script. I’m sure you can do this with python or anything else. The reason I chose rust is mostly just that I’m learning it right now and it seemed like a cool exercise. Plus the crates ecosystem has a library for almost everything by now. There’s no reason to reinvent the wheel when someone figured out how to do MPRIS before, for example.
The way I implemented it, it only checks for the artist, not a particular song. This is because (a) the API does not include tags in song results (unless I overlooked that), and (b) it holds up reasonably well for rap and hip-hop. A rapper probably won’t sing country, for example.
Here’s some screens of the script doing it’s thing:
The script comes with a configuration file that lets you customize it: change the list of blocked genres, blacklist or whitelist artists, etc.
I now have it in my KDE Autostart and it runs silently in the background, you don’t even know about it. The idle CPU usage is near zero, it’s blocked waiting for a player or an MPRIS event. You can also change the config on-the-fly, it’ll automatically re-read the file when it detects a change (with inotify). I’m quite happy with it