Spotify Controller: A Python Script to Automate Spotify Usage
I never used to like songs on Spotify. Rather, I’d directly put them into a playlist. The reason being that although I enjoy listening to most genres of music I don’t like to listen to multiple genres at the same time. Therefore, I like to separate music by genre, or not strictly genre but vibes (if that can be quantified). It’s fun to have a playlist that begins in disco/funk, moves to house and after three hours ends up in techno. But anyway, the point is I would never listen to my Liked Songs playlist.
The issue is that my Spotify suggestions haven’t been great recently and tend to get stuck in one or two genres. I think this is because the few tracks I have liked are from a small group of artists. I hope that by liking the tracks in my playlist the suggestions Spotify gives me will improve.
The task is to like all the tracks in my playlists. My next problem is that the Spotify UI involves a lot of clicking and changing views to get anything done. Now, I have the classic developers' dilemma. Do I do this task manually in 30 minutes or spend an afternoon (or two) building a way to automate this? Well, I’m writing this blog so obviously, I decided to automate.
Looking For Ways to Automate Spotify
I noticed that the Spotify Web API does not have an endpoint for liking tracks so I had a few ideas:
- Build a browser extension to control safari. However, I have recently moved to macOS and I'm still climbing that learning curve. Apparently, the trackpad is good and one should use it over
alt+tab/crtl+tab
etc... - Use Selenium and Python to automate a browser session. I kept hitting walls on this and again realised the learning curve was more than I was willing to put in for 30 minutes manual work.
- Eventually, I realised adding songs to Saved tracks was the same as liking songs. This is possible via the Spotify Web API. I did try to handwrite a python script to access the Spotify Web API directly but I found the authentication too difficult to wrap my head around.
- Finally, I settled on using Spotipy. This task wouldn’t be a great exploration project after all but just writing a basic script to do what I intended and no more.
The code
Like All Songs in Your Playlists
This script allows you to like all the songs that are in your private and collaborative playlists. It primarily uses the spotipy library which is a thin wrapper over the Spotify Web API.
Requirements for this to work:
- Create an app in the Spotify developer dashboard
- Make the
REDIRECT_URI
https://localhost
- Put your
CLIENT_ID
andCLIENT_SECRET
in aconfig.py
(see example) - Follow the links from the terminal when prompted
Unlike all Songs from a Playlist
This script allows you to unlike all the songs from a specific playlist. The name of the playlist must be valid for the currently logged in user. You only need to authenticate once so this script should work if you followed the steps above for the first script.
Struggles & Conclusions
I have left all half-baked attempts at solutions in the repo as evidence that I tried. My biggest struggle is that I had this simple idea “How can I like all the songs in my playlists without using the Spotify UI” that morphed into multiple failures. I failed to learn how to make a browser extension. I failed to learn how to use Selenium with Safari & Chrome. I failed to understand OAuth and the Spotify Web API enough to write my own API wrapper. I failed at setting proper expectations for this little project.
I am not even happy with the final state of the scripts. How does one use what I have made. They have to add their own API access keys and adapt the code. However, writing an application for others to use was not my goal and something that would definitely suffer from the 80/20 rule. That is, 20% of the work (accessing the API) to achieve an application has been done and it solves 80% of the problem. Solving the other 20% (making it useful to others) would require 80% more work.
I did, however, succeed somewhat in wrapping up a small project and publicly posting it (to my own blog). And that is an achievement in and of itself. Hopefully, this is a stepping stone for better things to come.
Maybe, I’ll return to Selenium some day or take the time to understand OAuth. But for now a new idea is the shiniest object. I attended the ACCU 2021 Conference this week and found about Zig.