Automating Playlists on Spotify From the Music Folders of your Computer.

An interesting way to export your music files to Spotify’s playlists.

Cristóbal V
DataDrivenInvestor

--

Photo by Wes Hicks on Unsplash

In modern society music streaming platforms are gaining a lot of popularity compared to the old mechanisms like vinyl records, CDs, mp3, files, etc. That’s why most people are preferring to save their favorite songs on music apps like Spotify. despite some people still have their music on audio files saved on their computer music folders.

That’s my dad’s case, he is an old school music lover and all his music is located in external disks, pen drives, and CDs. but now he wants to start using streaming platforms because he is tired of wasting a lot of time finding his favorites songs on the bunch of music folders of his computer.

So using my little knowledge in Python I decided to create a way to automate the exportation of the music located on audio files of his computer to new playlists on Spotify. So let’s do it!.

Required Tools:

  • Spotify Credentials to access their Apis and Data acquisition.
  • Libraries Pandas and Numpy for data analysis.
  • Library Spotipy for creating playlists on Spotify.
  • Library eyed3 for obtaining features of music files.
  • Library OS for manipulating paths on the computer.
  • Libraries RE and CSV for data cleaning and data saving.

1. Obtaining the Data.

To start my project I decided to use a folder of my dad’s music to make my script. This folder contains 47 songs of different Post-Rock bands. The folder looks something like this:

Post-Rock Music Folder with different songs in MP3 (image by author)

I had to obtain the main information of all these files because I need them later to search the track on Spotify. To do this I used the library eyed3, which helps to analyze the features of an mp3 file. I decided to save the Title of the Track, Name of the Artist, and Name of the Album for each track. Some of these tracks didn’t have these features on their detailed information So I had to save the name of the file to tag the track instead. I also use the library OS to access the different paths of each track file.

Finally Obtaining the features and information of all the track files I saved this data into a CSV file using the CSV library. the complete code is on my Github.

#librariesimport eyed3
import os
import re
import csv
import numpy as np
import time
#Let the user introduce the path of the music folderpath = str(input("Please Introduce the Path Folder: "))#Save the name of each mp3 file on a list called "filenames"filenames = []
for r,d,f in os.walk(path):
for file in f:
if file.endswith(".mp3"):
filenames.append(file)
#Obtain the features of each mp3 file and saved them on 3 listsname_song = []
name_artist = []
name_album = []
for filename in filenames:
pathfile = path + "\\" + filename
audioinfo = eyed3.load(pathfile)

if not audioinfo.tag.title:
temp_name = re.findall("^(.+?).mp3",filename) [0].replace("_"," ")
name_song.append(temp_name)
else:
name_song.append(audioinfo.tag.title)
name_artist.append(audioinfo.tag.artist)
if not audioinfo.tag.album:
name_album.append(np.nan)
else:
name_album.append(audioinfo.tag.album)
time.sleep(0.05)#print the total number of the tracks obtainedprint(f"{str(len(name_song))} tracks obtained!")#Save the information into a csv file#let the user introduce the name of the new csv filename_file = str(input("Please Introduce the name of the new csv file: "))with open(name_file,"w",encoding="utf-8") as f:
w = csv.writer(f)
w.writerows(zip(name_song,name_artist,name_album))
f.close()
print(f"{name_file} saved and closed!")

With this script, I created a CSV file containing all the information required to search those tracks files on the Spotify database and then create the new playlists for my dad!.

Command Prompt view of the script working (image by author)

#Lame tag CRC check failed: this line appears when I run eyed3, taking a look at Google I realized is a common message when accessing or modify files too fast, but the total number of results doesn’t affect when appears this message.

2. Creating Playlists on Spotify.

To create new playlists on Spotify I used the library Spotipy that helps to automate processes, get a lot of different features of a wide variety of tracks, etc. You just need to get a Client ID, Client Secret, and Username Number to use the Spotify’s Apis and manipulate your library music and account data. You also need to specify the Authorization Scope of the different methods you want to automate.

To search the tracks from my dad’s Post-Rock music folder that I wanted to save on new Spotify playlists, I had to obtain first the ID of all these tracks. To do that I use some credentials to have access and manipulate my account on Spotify. I also use the search function documented on the spotipy library to search if the track is on the Spotify database. the complete code is on my Github.

Authorization code to access the Spotify User Library.

#librariesimport spotipy
from spotipy import SpotifyClientCredentials, util
import pandas as pd
client_id='YOUR_CLIENT_ID'
client_secret='YOUR_CLIENT_SECRET'
redirect_uri='YOUR_REDIRECT_URL'
username = 'YOUR_SPOTIFY_USER_NUMBER'
#The scope required to modify the user libraryscope_user = 'user-library-modify'#The scope required to modify the user playlistsscope_playlist = 'playlist-modify-public'#Credentials to access the user library musictoken_user=util.prompt_for_user_token(username,scope_user,client_id,client_secret,redirect_uri)sp_user = spotipy.Spotify(auth=token_user)#Credentiasl to access the user Playlists Musictoken_playlist=util.prompt_for_user_token(username,scope_playlist,client_id,client_secret,redirect_uri)sp_playlist = spotipy.Spotify(auth=token_playlist)

Code to obtain the Spotify ID of each track file and save them into Data Frame.

#Open the CSV file created with pandas and specify the headersheaders = ['song','artist','album'])
data = pd.read_csv("../music/music-file.csv",names=headers)
#Save the ID given by Spotify for each trackids = []for i in range(len(data)):query = data['artist'].loc[i] +" "+ data['song'].loc[i]
songs = sp_user.search(q=query,type='track',limit=2)
try:
ids.append(songs['tracks']['items'][0]['id'])
except IndexError:
print(f"ID Not Found: Please review the song's title with index {str(i)}")
ids.append(np.nan)

If you noticed I had to handle with Index Errors, that’s because some of the tracks didn’t have IDs on Spotify Database.

In case the track exists on the database, I saved the track ID given by Spotify on a new list called “ids”. In case the track doesn’t exist, the script gives me back the index of the track storage on my data frame to solve 2 possible issues:

  1. The title of the track is written slightly differently on the Spotify Music Database: The solution for this problem is just to modify the track title on the data frame and write the title located on Spotify. for instance, I had this problem with a song named “Twenty Two Fourteen” on my dad’s files and named “Twentytwofourteen” on Spotify Database. So I just modify the title of the song with the name used on Spotify.
  2. The track doesn’t exist on Spotify Music Database: In this case, the ID is replaced with NaN because Spotify doesn’t have the track on their Music Database.

Finally, I was able to create the playlist with the track files of my dad. Using the playlist scope and 2 functions of the Spotipy library, first I created the playlist called “Post-Rock” and then I use the ID of this new playlist to store all the tracks by its Spotify IDs.

#Create the new playlistnew_playlist = sp_playlist.user_playlist_create(username,"Post-Rock",public=True)#Populate the new playlist with the tracks using the Spotify Ids for each track.sp_playlist.user_playlist_add_tracks(username,playlist_id=new_playlist['id'],tracks=data['id'].tolist())

If you want to listen to the playlist you can access the link below.

Conclusion

Automating a task that can take a long time to complete, it can be useful in modern society considering the actual world is speeding the processes for any activity in an exponential way. Time becomes precious every day, so why not use the technology to automate slow and boring manual methods and spend the free time with our friends, family, or even creating new scripts to automate more things!.

My Articles:

References:

Gain Access to Expert View — Subscribe to DDI Intel

--

--