Thursday, 18 August, 2022 UTC


Summary

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:
https://medium.com/media/c09b16d8514d398ab1d707ce55b84130/href
Next, read in the exported text file and make sure to specify the mode and encoding as shown below:
https://medium.com/media/946d3dbe661613d284075e6c5bc34884/href
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.
https://medium.com/media/fc5e4f789aa0346c7fcac40c75239c7d/href
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:
https://medium.com/media/ae1890e268d994a0c1b5581d5f6355e6/href
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:
https://medium.com/media/3f42b7026535c9bf2d781e2c8b68ef6e/href
Next, we define a function to create a new playlist in our Spotify account.
https://medium.com/media/edfd63ba86092d82224d5188a0662d6e/href
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:
https://medium.com/media/77ea3f1a4b56d9ab761cbc548adb0949/href
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:
https://medium.com/media/c26572322d6c81a84decabf5671fe602/href
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:
  • Documentation | Spotify for Developers
  • Automate YouTube Music to Spotify with Python - GeeksforGeeks

Love, Spotify & Python was originally published in AR/VR Journey: Augmented & Virtual Reality Magazine on Medium, where people are continuing the conversation by highlighting and responding to this story.