Love, Spotify & Python

--

Creator: yrabota | Credit: Getty Images/iStockphoto

Story of a lover’s quest turned Python Project!

Music is a great unifier of people and sometimes, it’s easier to communicate your feelings to your significant other through some poetic melody.

I realized that, over the years, I’d been sending Spotify song URLs to my girlfriend, SweetPea, (to yunno, express myself) without creating a comprehensive playlist of these expressions. Considering how much we text (and how lazy I am), manually extracting these URLs and creating this playlist was not an option so I set out to do this programmatically using:

  • Python,
  • the Spotify API,
  • and our exported WhatsApp chat.

If you’re only interested in the code, here’s a link to it in my github.

Else, let’s walk through the process!

Step 1: Export WhatsApp Chat Data

Open the desired chat (on your phone app) and click on the three dots at the top right, followed by “More” and then “Export Chat”. Since we’re interested in URLs (text), click the “Without Media” option to get up to 40,000 of your latest messages.

Depending on how much you chat, this could go as far back as a year or two (obviously, barely grazed 6 months for SweetPea and me 😏 ).

For all your historical chat data, there’s third-party apps you can use but I cannot vouch for their data privacy policies so thread softly. I’ll be using my limited exported data for this project.

Transfer the generated ‘txt’ file to a folder your PC and we’ll set up our folder for our python project.

Step 2: Extract URIs From WhatsApp Chat Data

I used an “ipynb” in VSCode for interactive analysis and my favorite VSCode functionalities but feel free to do this in any IDE of your choice.

Following best practices, I created a virtual environment using “pipenv” and installed the pandas and requests packages (available in the requirements.txt file).

Import relevant packages as shown below:

Next, read in the exported text file and make sure to specify the mode and encoding as shown below:

The message format from the WhatsApp export is as follows:

date, time - speaker: message\n

So using the code below, we extract the timestamp, speaker and message data into separate columns.

Now that we have all our messages in one column, we filter for messages that contain the Spotify song URL pattern “https://open.spotify.com/track/”.

Then using regex, we extract just the URLs (in case we typed some string alongside in the same message) and convert the URL to a Spotify URI. You can look up the difference here but basically, for example:

Spotify ID - 6rqhFgbbKwnb9MLmUQDhG6

Spotify URL - https://open.spotify.com/track/6rqhFgbbKwnb9MLmUQDhG6

Spotify URI - spotify:track:6rqhFgbbKwnb9MLmUQDhG6

This is shown in the code below:

Now that we have our URIs, we move to the last step!

Step 3 - Create and Update Playlist on Spotify

To complete this step, you’ll need two Spotify credentials.

  1. Username (SPOTIFY_USER_ID)

This is obtained here. You may be prompted to login to your Spotify account and you can copy the username as shown in the image below:

2. OAuth Token

This can be obtained here and generated as shown in the image below:

You should avoid storing your credentials in your source code, so you can store them as environment variables on the command line. Depending on whether you’re in a Mac, Windows or Linux operating system (OS), the syntax may differ so I just use the “os” package from python to store and retrieve these details.

With the os.environ function, you can store these credentials as environment variables and delete them from your code. This is OS agnostic so you won’t have to worry about your operating system. This is shown below:

Next, we define a function to create a new playlist in our Spotify account.

Then we call this function and store the response as we’ll need the playlist ID for the playlist it creates.

All things being equal, you should receive a 201 response from the call.

Call the function and extract the playlist ID as shown below:

You can verify that the playlist has been created by checking your Spotify page.

Finally, we populate the playlist with our previously extracted URIs.

We define a function to do this and call it as shown below:

All done! You have labored in love (as I have) and saved yourself the hassle of manually adding the songs!

Well done!

Other things I can choose to do with the data include:

  • generate individual and collective word clouds
  • time series sentimental analysis to try to spot times when we had a fight (they say love is like a roller coaster 😂 )
  • find out who says “I love you” more :relieved:
  • find out who uses more emojis, texts more and uses more words per text
  • find out when last we went a day without texting each other (ans - never)
  • make a Streamlit app that takes in WhatsApp text data and does some analytics populating the points above in a dashboard.
  • and tons of other fun stuff!

Feel free to connect with me on LinkedIn to talk about Love, Python, Spotify, Data and much more!

Link to full code on github here.

References:

--

--