Genrify: Python app to filter Spotify library based on genre

Taking a temporary departure from my usual Infrastructure/DevOps articles, this article is about “Genrify”, a Python app I’ve written to select tracks from Saved Albums or Playlists on Spotify based on Artist Genre and add them to Queue or to a new Playlist.

I got frustrated at having a Spotify library of added albums and created playlists, but not being able to query my library by genre. Basically, I wanted functionality like Smart Playlists in Apple’s Music app (previously iTunes), where it’s possible to say something like “select all songs from recently added albums where the genre is darkwave”. Therefore, with the goal in mind of being able to do something similar with my Spotify library, I created this Python 3 app called Genrify which uses Spotify’s API via the Spotipy Python library to achieve this functionality.

My Genrify app allows choosing a genre from recently added albums (Saved Albums) or from a Playlist, then it gives the choice of adding all the tracks associated with that genre (obtained from the genre of the artist for each track) to the Queue or to a new Playlist.

The interface is currently rather rudimentary, especially given that it uses a basic CLI in a terminal, but it works well to achieve the goal I was aiming for.

My code is here on GitHub. Please feel free to fork this and improve on it.

Requirements

Setup

  1. Log in to the Spotify Developer Dashboard with your Spotify account.
  2. Choose CREATE AN APP, give it a name and description, tick the “I understand” box, and choose CREATE.
  3. Make a note of the Client ID and Client Secret.
  4. Choose EDIT SETTINGS and add http://127.0.0.1:9090 under Redirect URIs, then choose SAVE.
  5. Copy setup_template to setup, then edit setup and add the Client ID and Client Secret for your newly created Spotify app.
  6. (Assuming you’re using Bash or similar) import the setup environment variables with:
. setup

Usage

Assuming python is a symlink to python3:

python genrify.py

Example

Run the app:

python genrify.py

These options appear:

1. Select from Saved Albums
2. Select from a Playlist

Choose 1 or 2:

Typing “1” then Enter to choose from Saved Albums will show a list of the top 20 available genres generated from the 50 most recently added albums:

Top 20 Genres from 50 most recent Saved Albums:

1. witch house (7 albums)
2. big beat (6 albums)
3. industrial (6 albums)
4. neo-psychedelic (5 albums)
5. cyberpunk (5 albums)
6. industrial rock (5 albums)
7. ebm (4 albums)
8. electro-industrial (4 albums)
9. industrial metal (4 albums)
10. neue deutsche harte (4 albums)
11. grave wave (4 albums)
12. space rock (3 albums)
13. chamber psych (3 albums)
14. electronica (3 albums)
15. dance rock (3 albums)
16. hardcore techno (3 albums)
17. wave (3 albums)
18. oakland indie (2 albums)
19. classic progressive house (2 albums)
20. downtempo (2 albums)

Select Genre:

Let’s say we type “1” then Enter to choose “witch house” as the genre:

94 tracks to add for selected genre "witch house":

1. Add to Queue
2. Create Playlist

Choose 1 or 2:

Typing “2” then Enter to create a Playlist will show the following:

Playlist "[genrify] witch house" created and 94 tracks added

That’s it! And voilà, the desired playlist has appeared in our Spotify library, as if by magic:

Genrify playlist in Spotify app

We could also have chosen to use an existing Playlist as the source, and we could have added the tracks to the Queue rather than creating a new Playlist.

Desirable improvements

If I had the time, which I don’t currently, then these are some improvements I’d want to add:

  • Add breakpoints to UI so that CTRL-C is not necessary to quit.
  • Fail with nicer error when no active device found when adding to Queue.
  • Code could be made more modular by splitting parts off into more repeatable functions.
  • Select from albums other than the 50 most recently added.
  • Be able to handle more than 50 playlists, and format playlist list better.
  • Be able to add more than 100 tracks.
  • Improve interface, ideally by migrating to a web-based UI.

Conclusion

I find this a genuinely very useful tool to get a lot more enjoyable mileage out of my Spotify library. Hopefully others will too.

As ever, if you’re looking for a freelancer – perhaps with reasonable Python skills! – to assist you with Infrastructure, DevOps and SysAdmin issues, do get in touch.