Custom Spotify playlist maker

Build your own playlist generator with Spotifys API [in Python!]

rob_med
Follow
Sep 13, 2019 · 6 min read
Photo by Heidi Sandstrom. on Unsplash

Spotify does a great job at providing us with maaaany [and always fresh!] playlists, to the point that it is basically 100% guaranteed that youll find one that satisfies you on any occasion.

But. Sometimes it is also lots of fun to generate your own playlists, according to whatever criteria you can think of.

For people who actually have fun like that [me, for one] Spotify provides the Web API that allow one to generate on-the-fly playlists based on specific properties such as artists, genres, happiness, BPMs, acousticness and lots more.

Since these are web API, you can use any development environment and language you like to query and get results from them.

In this post I will use Python, but nothing stops you from being brave and using C++, for example.

Photo by Hitesh Choudhary on Unsplash

The first thing you need to be able to use the API is a OAuth token, that allows you to authenticate your requests. To obtain one, the easiest way is to head over to //developer.spotify.com/console/get-recommendations/, scroll all the way to the bottom and click on the Get Token button.

After selecting the scopes that we need [in this case, only playlist-modify-private] and logging in, youll get a code that you can copy-paste and use to authenticate the requests.

NB: This is only a short-lived token; to generate and refresh tokens for an actual *serious* application you can follow the guide at

Once you have the token, we can start digging into building our very own playlist generator.

To do this, we will set some filters and query the Recommendations and Playlists endpoints to create and fill our playlist.

We will do this by directly invoking the Web APIs, using the [amazing] requests library to make the HTTP requests [duh] needed.

Specifically, heres what were gonna do:

1. an HTTP GET request to the /recommendations endpoint, to get the tracks;

2. an HTTP POST request to the /playlists endpoint to create a new playlist;

3. an HTTP POST request to the /playlist endpoint to add the songs.

First off, we import the requests library and set-up endpoint and filters.

Here, we set some basic filters for the recommendations [10 indie tracks, pretty good for the dance floor].

Next, we create our query, and send an HTTP GET request to the API service. Replace YOUR_TOKEN_HERE with the token obtained as explained before.

Finally, we get the JSON response from the APIs, and parse it to print the recommended tracks:

If all went well, you should now see the 10 tracks recommended. For example, this is the output I get [note that this can be different for what youll get by running the code, as 10 recommendations are sampled at random between all suitable tracks]:

1] "Rawnald Gregory Erickson the Second" by STRFKR
2] "Oh Devil" by Electric Guest
3] "Where I'm From" by Digable Planets
4] "Fidelity" by Regina Spektor
5] "Wraith Pinned to the Mist and Other Games" by of Montreal
6] "Pretty Girl" by Clairo
7] "Summer Days" by Rhye
8] "Psychic City - Classixx Remix" by YACHT
9] "This Must Be the Place [Naive Melody] - 2005 Remaster" by Talking Heads
10] "FACE" by BROCKHAMPTON

Pretty nice! Looks like our recommendations are on-point.

For now, we only set a couple of basic filters, however one can also specify more specific settings such as I want songs that sound like Franz Ferdinand or I want songs that sound like Cassius by Foals.

Photo by Cedrik Mosquera on Unsplash

To do this, we can set seeds for the tracks [or artists] we would like our playlist to sound like.

Lets try! To add the filter on the artists we need to set the seed_artists attribute in our query:

As shown above, the seed is passed as the ID of the artist. To retrieve this [the fast-and-easy way] you can browse to the artist on Spotify, right click on it and select Share -> Copy Spotify URI, that will copy to the clipboard a string like:

spotify:artist:0XNa1vTidXlvJ2gHSsRi4A

The part after spotify:artist: is the ID that you need.

Once that is done, the output we get is:

1] "What She Came For" by Franz Ferdinand
2] "Rawnald Gregory Erickson the Second" by STRFKR
3] "Oh Devil" by Electric Guest
4] "Wraith Pinned to the Mist and Other Games" by of Montreal
5] "Where I'm From" by Digable Planets
6] "Summer Days" by Rhye
7] "Ghost Ship" by Blur
8] "Wide Awake" by Parquet Courts
9] "Psychic City - Classixx Remix" by YACHT
10] "FACE" by BROCKHAMPTON

As we can observe, the recommendations are indeed changed to reflect better our new filter. Guitar-bands like Blur and Parquet Courts [indeed more in the Franz Ferdinand register] have now replaced artists like Regina Spektor and Clairo.

Photo by Melvin Darrell on Unsplash

Similarly, we can set the filter on the tracks by setting the seed_tracks attribute:

Again, the ID of a track can be obtained via its Spotify URI.

And heres the corresponding output:

1] "Rawnald Gregory Erickson the Second" by STRFKR
2] "Oh Devil" by Electric Guest
3] "Where I'm From" by Digable Planets
4] "Wraith Pinned to the Mist and Other Games" by of Montreal
5] "Summer Days" by Rhye
6] "FACE" by BROCKHAMPTON
7] "Heartbreaker" by Metronomy
8] "Psychic City - Classixx Remix" by YACHT
9] "Daft Punk Is Playing at My House" by LCD Soundsystem
10] "The Salmon Dance" by The Chemical Brothers

Electro-dancy bands like LCD Soundsystem, The Chemical Brothers and Metronomy are now in, nice!

This is cool, but for now we only printed out title and artist of our recommendation.

What we would like is, of course, to automatically build a playlist containing these recommendations.

To do this, well use the Playlists endpoint.

First, lets create a new playlist named Indie bands like Franz Ferdinand but using Python. Unlike before, we will now make a POST request to the APIs, including our new playlists details in the requests body.

The endpoint for this request is //api.spotify.com/v1/users/{user_id}/playlists, that requires to specify the user_id [in this case, our ID]. To retrieve this we can again use the Spotify URI, by browsing to our profile and clicking Share -> Copy Spotify URI. The ID is again the last part of the URI.

If the response returns a status code 201, everything worked and our playlist is now ready to be filled up.

To add songs, we need the ID of the newly created playlist [available in the JSON of the response]:

Again, 201 means all good!

We can now browse to our profile, and

There it is!

We can finally enjoy our freshly-generated custom playlist:

Indie bands like Franz Ferdinand and Foals but using Python, a playlist by Roberto Medico on

We and our partners use cookies to personalize your experience, to show you ads based on your interests, and for

open.spotify.com

You can find the full notebook with all the code here.

Dont forget to fill in your own token and user ID before running it!

Just plug-in your own token and start playing around with new filters, theres a whole lot!

Finally, you can find a live version of a custom generator here: this tool uses the APIs we just saw to generate custom playlists by selecting genre, mood or both.

In this case, no login is required as everything is authenticated using one account the owner of the created playlists.

Video liên quan

Chủ Đề