Spotify custom playlist maker

A Spotify engineer has invited users to try a new tool that lets you create custom playlists. Called Nelson, this Glitch project lets you choose various genres and change various parameters to create the playlist of your dreams. And while it isn't perfect, it sure is a lot of fun.

Spotify is famous for its ability to create personalized playlists for each individual user. It does this by analyzing what you've already listened to and recommending other songs of a similar nature. And it's rather shocking quite how well Spotify knows you after years of listening.

Parameters Personalize Playlists

Nelson was revealed in a tweet by "developer advocate engineer" Arielle Vaniderstine. It's an internal project that lets users play with various parameters to create custom playlists. First, you pick various genres, and then you fiddle with the sliders to create your perfect playlist.

The parameters range from the obvious [popularity and tempo] to the peculiar [energy and danceability] and finally to the mind-boggling [valence and speechiness]. Luckily, Spotify explains what each of these audio elements refer to in a post on the Spotify developers blog.

As you fiddle with the parameters Nelson will adjust the playlist accordingly. And if you're a Spotify Premium user you can then play the playlist immediately either on Spotify or in your web browser. Which means you could use Nelson to create endless playlists filled with music.

The Age of Music Streaming

Nelson is a fantastic tool that utilizes the incredible amount of data Spotify collects about every song on the service. So while there are some truly timeless ways to discover new music to listen to, this is one of the few that's only possible in the age of streaming we're lucky enough to live in.

Do you have a Spotify account? If so, how do you feel about Spotify's ability to create playlists just for you? Have you tried experimenting with Nelson yet? If so, how well did it perform? Did you discover new artists and/or songs worth listening to? Please let us know in the comments below!

Image Credit: Andrew Mager via Flickr

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 you’ll 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, you’ll 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, here’s what we’re 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 you’ll get by running the code, as 10 recommendations are sampled at random between all suitable tracks]:

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.

Let’s 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:

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 here’s the corresponding output:

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, we’ll use the Playlists endpoint.

First, let’s 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 playlist’s details in the request’s 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:

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

Don’t 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, there’s 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ủ Đề