Breaking into the industry: My Journey to becoming a Software Engineer #9 — Spotify Playlist

M E
4 min readJul 27, 2021

I love music! Actually, that statement is underrated. Music has always been a huge part of my life. It is therapeutic and it has ways of describing how I feel when words fail.

Have you ever tried creating a playlist on Spotify? If so, how hard is it to come up with songs to add? Sometimes you want your playlist to be organized and contains relevant songs that is related the the playlist theme. But that never really happens, right? Well, at least in my experience :) I get so lost in the rabbit hole and gets stuck with a particular artist/artists that my playlist theme gets so clouded with irrelevant songs that does not even match my theme. Thus, in order to fix this problem of mine, why not use BeautifulSoup to scrape song titles let’s say in the Billboard’s Hot 100 Songs and then use the Spotify API to create a playlist and automatically add each songs to the playlist.

Back to the future
If the user input 2005–06–04, the song titles and artists will be scraped using BeautifulSoup and create a list.

Since the Billboard’s Hot 100 Songs can travel back in time, I can ask the user which time period they want to travel back (must travel 88mph, just kidding) and by using BeautifulSoup, I can scrape the Billboard’s website and create a list of the song titles and artists found on the website.

SPOTIPY API

First and foremost, we need to import spotipy library to get full access to all the music data in Spotify. Additionally, in order to be able to access own’s Spotify account, the user must log in to their own account, go to the Developer Dashboard and create a new Spotify App. After creating a Spotify App, copy the Client ID and Client Secret. These will be used to pass on as parameters when creating a Spotify playlist. Check this website for more information about Spotipy Documentation.

In order to get authorization to access Spotify’s account, user must pass the Client ID and Client Secret received after creating a new Spotify App. Moreover, the user must also pass “playlist-modify-private” in order to create a private playlist. After using the Spotipy Authentication, it will lead the user to the spotify_redirect_uri, and must copy the entire URL into the prompt. After entering the URL to the prompt, restart and the file called ‘token.txt’ will be automatically generated. Then voila! The user is authenticated to access its Spotify Account, create a playlist and add songs to the playlist created.

Getting authentication to access Spotify Account
Create a Spotify Playlist and save the playlist’s URI in a variable called PLAYLIST_ID

Adding Songs to the Spotify Playlist

Spotipy URI

Before being able to add the songs to the user’s Spotify account, we need to access either the song’s ID, URI or URL. But in our case, we will use the song’s URI. First, we must loop through the song_list, then we will search each song using spotipy’s ‘search’ function by passing on parameters such as the track name which is the ‘song’ and the year (based on the user input). The reason why we specified the type parameter as ‘track’ because it returns a list of tracks’ IDs, URIs, or URLs. We need the track’s URIs in order to be able to add the song to the Spotify Playlist. Thus at the end of the for loop, we will end up with a list (SPOTIFY_URI) of song’s URIs.

Create a list of song’s URIs

Then, we will use the SPOTIFY_URI list as the ‘items’ parameter when adding the songs to the playlist.

Add songs using

Here is a snippet of what it would look like at the end of it all.

The full code can be accessed on my GitHub Account. :)

--

--