Last week Apple released Apple Music, the new streaming service available all over the World and as a big music-streaming-services fan, I decided to give it a try and use it for the free three months trial period next to Spotify which I have been using on a daily basis for the last two years.
So I had the problem to export music from Spotify and import it in Apple Music.
Unfortunately I didn’t found any solution to do it automatically, Apple Script documentation has no reference about scripting Apple Music and so I decided to write a simple script to export all my song titles and playlist from Spotify to a text file.
And, of course, I decided to use Python.
1 – Install software
To use the script attached later in this post, you need to install a Python module and get an access key from Spotify.
Ok, I know, it sounds crazy, but trust me, it’s super easy to do.
First install Brew according to the instruction on the site
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Second, install libspotify to installa Spotify official SDK:
brew install mopidy/mopidy/libspotify
Third, install pySpotify python module:
sudo pip install pyspotify
And that’s all for what concerns software: now you need an access key.
2 – Access Key and password
To get an access key, first you need to create an application with Spotify at this link.
Then, if you went through the process correctly, you’ll get in the end an access key. Write it down because we’ll use it in the next step.
Note: if you are accessing Spotify with a Facebook account, you need to log into your profile and create a password.
3 – The script
And finally, the script.
To use it you need only to specify your username and password and, if you want, the name of the output file and the separator betweeb playlist names and song tracks.
#!/usr/bin/env python # -*- coding: utf-8 -*- import spotify import threading import time separator = "|" outputfile = "spotify.txt" username = "**********" password = "**********" def connection_state_listener(session): if session.connection.state is spotify.ConnectionState.LOGGED_IN: logged_in_event.set() def myencode(text): for charset in ['US-ASCII', 'ISO-8859-1', 'UTF-8']: try: out = text.encode(charset) except UnicodeError: pass else: return out out = "" logged_in_event = threading.Event() session = spotify.Session() loop = spotify.EventLoop(session) loop.start() session.on(spotify.SessionEvent.CONNECTION_STATE_UPDATED, connection_state_listener) session.login(username, password) while not session.connection.state == 1: time.sleep(1) for playlists in session.playlist_container: for track in playlists.tracks: playlist = myencode(playlists.name) authors = ', '.join([myencode(author.load().name) for author in track.load().artists]) title = myencode(track.load().name) out += "%s%s%s%s%s\n" % (playlist,separator,authors,separator,title) with open(outputfile,'w') as f: f.write(out)
As you can see the script is very easy, but does a pretty good job extracting and saving all your playlists.