While I was going for a walk today I randomly started wondering if it was possible to download a Spotify playlist as a text file or CSV file. It didn't take long for me to find the Exportify project which can be used to make a backup copy of your Spotify playlists. It was easy to get it up and running by cloning the repository from github and then installing the dependencies and starting it with yarn.
When exportify is started it will run listening on localhost:3000 and open up its login page automatically in your browser. Then you just need to login to Spotify and choose the playlist you want to export to CSV.
I now have a backup of my playlists in case Spotify somehow loses them, however unlikely that is. But now I was wondering if I can download a list of songs from a CSV file. I decided the easiest way to do that would be to use youtube-dl since it has a built in youtube search facility. All I needed to do was get the artist and title from the CSV and send them to youtube-dl. A quick python script to do that looks something like
#!/usr/bin/env python3
import csv
import os
# open up the csv file and read the data into a list
with open('playlist.csv', newline="") as f:
reader = csv.reader(f)
data = list(reader)
# skip the first line which contains headings
for item in data[1:]:
# create a song name to search for using the artist and song title
fullname = item[3] + " " + item[1]
# download the audio as an mp3 using youtube-dl
youtubedl_cmd = "youtube-dl -x --audio-format mp3 --add-metadata 'ytsearch:%(fullname)s'" \
% { 'fullname':fullname }
os.system(youtubedl_cmd)
This can then be run in a directory containing your playlist.csv
$ mkdir output
$ cp my-spotify-list.csv output/playlist.csv
$ cd output
$ python3 ../exportify-dl.py
It will obviously need some fixing to sanitize the song names before sending them to youtube-dl, but it seems to work. Another improvement would be to check command line arguments to accept the CSV file and then create an output directory with the name of the playlist to save the files.