How to move from Apple Music to Spotify

Apple Music sucks. Let’s not beat about the bush here. I’ve paid for Apple Music since it was released (minus the mandatory free trial) and I’m sick to death of the poor user experience, serious lack of updates and general awful time I’ve had with the service. If anyone disputes Apple’s declining software quality, shove Apple Music in their face for 10 days and they’ll be converted. I could rant for days about how it permanently damaged my music collection or how poorly integrated it is with iTunes but I leave that for another post. Right now, I’m helping anyone out there with similar frustrations migrate (back) to Spotify who will hopefully accept us with open arms.

The existing solutions for migrating my Apple Music library to Spotify were either non-existant, too expensive or plain didn’t work. I wasn’t going to go through each track in my big library, find it on Spotify and add it to my playlists. That goes against everything I believe in: if you have to do a repetitive task to the point where it’s boring, automate it. So I did what I’m good at and I banged out a couple of lines of code. Polytunes was born.

Polytunes

Polytunes is an open source command line tool the will do the grunt work for you. You simply configure it to talk to your music libraries and let it do it’s job of searching for the tracks, adding them to their playlists, removing duplicates and so on. It makes things a breeze but does require a little command line knowledge and some setup. Hopefully, in the future, we can get some sort of user interface going but for now, this is a guide for migrating your music library on the command line.

First off, let’s install Polytunes. You need npm and Node.js v6.0.0 (or above) installed. We install polytunes via npm:

$ npm install -g polytunes

Next, we need to configure the drivers used to communicated with your music libraries.

Configuring your Apple Music Library

The Apple Music driver works by extracting data from an iTunes library export file. To export your library, open iTunes and go to File > Library > Export Library and save the XML file somewhere you’ll remember (try the Desktop).

Export your Apple Music Library.

Now, remember the path to that XML file, you’ll need it later.

Configuring your Spotify Library

I’ll assume you have a Spotify account at this point, have tried it out and wondering why you ever left at all. Communicating with the Spotify is tricky. Spotify has an awesome Web API that we use to manipulate your music library but it uses a pretty complicated login flow call OAuth. It’s not exactly friendly to CLI apps, to say the least. We need to manually generate our API tokens that will allow us to talk to Spotify’s API and migrate your library.

  1. Create your Spotify Client.

We need to let Spotify know that’ll we be communicating with it and to do that, we have to create an Application in the Spotify Developer Portal. This will give us a Client ID and secret, note them. It doesn’t matter about the details of the application, it’s just us that will be using it.

Create your Spotify application in the Developer Portal.

2. Get our request bin.

The Spotify login flow requires an online server to talk to once we try to login via our application. Since we’re only a lowly CLI app, we don’t have such luxuries. For the purposes of this simple import, we can use a simple, free online service to do this for us called RequestB.in. Request Bin gives us a random URL and saves any requests that hit that URL for us to come back and inspect. This is useful because the Spotify API requires a URL to store login information once we complete our login request.

Go to http://requestb.in and get your unique bin URL.

3. Add our Redirect URI.

Now, back to the Spotify developer portal, we add our unique request bin URL (without the “?inspect” suffix!) as a Redirect URI on our application. Save the changes to your application.

Adding our request bin as a Redirect URI on our Spotify application.

4. Get our OAuth code.

Now we need to get our code for the next step in the OAuth process. We need our Client ID (which you can get from our application page on the Spotify Developer Portal) and our request bin URL to insert them into the following URL (replace “<CLIENT_ID>” and “<REQUEST_BIN_URL>”):

https://accounts.spotify.com/authorize?client_id=<CLIENT_ID>
&redirect_uri=<REQUEST_BIN_URL>&response_type=code&scope=playlist-read-private%20playlist-read-collaborative%20playlist-modify-public%20playlist-modify-private%20user-library-read%20user-library-modify%20user-read-private

Stick the modified URL into your browser and you should be presented with a Spotify Login or Authorization page. Login to your account and authorize your app and you should be redirected to our unique Request Bin that just displays a loud “OK” page. Don’t close page! Copy the URL in your browser, it should have a long code like:

http://requestb.in/w5keyyw5?code=AQBwsoJYWFvF0n7XDNWtS1rRYjAvuqD3p-wTIyoLjYMUM0HCtwUMPpzfyhXeahsfXdDauiHDFY0gEUjbiWnd9PQV9eAXxSvgVb31u3WLVUTRNq9w0jR-hg5zE9IOO_U41gcFfdi-9mku3pl7QnxE2f0_qOcPBok-5Cu_EzFtsWeAl-tMJGfgVJreaM0otdfValZlu

This is the code were looking for, the final piece of the puzzle. We now send that token to the Spotify API (POST request) and we should have our access.

5. Posting the code to the Spotify API.

Were nearly there, promise. Now, we have all the data needed to get our access.

  • Client ID and Client Secret from our application page on the Spotify Developer Portal.
  • Request Bin URL from Step 3.
  • OAuth Code from Step 4.

We send these tokens to the Spotify API via a cURL request. Fill in the following fields with your data and execute the command.

$ curl -X POST \
-d grant_type=authorization_code \
-d code=<CODE> \
-d redirect_uri=<REQUEST_BIN_URL> \
-d client_id=<CLIENT_ID> \
-d client_secret=<CLIENT_SECRET> \
https://accounts.spotify.com/api/token

You should get a success response with the following data:

{
"access_token": "BQAoW1Xfake_JiTGoDh-...FaCbrk5TRFhEHzdvDukyQ",
"expires_in": 3600,
"refresh_token": "AQC3aSDyORjSUxdQ-...0bhHBw1T0fdsdf45DWZLc8U",
"token_type": "Bearer"
}

The refresh_token is the one were after. It should look a lot longer than above. With the refresh_token, client secret and id, we now have full access to the Spotify API for Polytunes.

We can configure Polytunes in a two ways, but for convenience, were going to use the environment route. Create a file called “.env” and add your tokens to it you got from the steps above. The contents of your .env file:

export SPOTIFY_CLIENT_ID=<CLIENT_ID>
export SPOTIFY_CLIENT_SECRET=<CLIENT_SECRET>
export SPOTIFY_REFRESH_TOKEN=<REFRESH_TOKEN>

Now, all we have to do is source that file and Polytunes should be configured. Give it a try and test the configuration with the “library” command:

$ source .env
$ polytunes library -d spotify playlists
[*] Getting Spotify playlists..
-> 2015 created by adriancooney. (1tKQSuYWtJAgtJ4TYwH2g3)
-> Library created by adriancooney. (3in3LhDqQdR9LdYoxHI8ga)
-> House created by adriancooney. (5c2V6EsYWXELjjunNTFpE5)
-> Groovers created by adriancooney. (3svlOvf9y6dsHt3x6ijZ2q)

Import your Apple Music library into Spotify

Now all the hard work is over, let Polytunes do that rest. Let’s fire up the importer and have it run on your library. Polytunes will copy over all playlists from your iTunes library to your Spotify library. For this step, we’ll need the path to your Apple Music library export file from the very start (replace “<PATH>” in the following command with it):

$ polytunes import --from apple --to spotify --apple-library <PATH>

Grab a coffee and let it do it’s thing. Polytunes checks to see if a song has already been added so you can run the above command as many times as you like and it won’t add any duplicates. If you only want to copy specific playlists, you can specify by name them in a space separated list (see “polytunes --help” for more information). The importer tries to match songs with the Spotify database and sometimes that doesn’t always happen (either because Spotify doesn’t have the rights or know the track) so it will skip these tracks.

[*] Collecting libraries..
[*] Importing from your Apple library to your Spotify library.
[*] Importing Apple playlist: ‘Music’
-> Track ‘Sometimes I Feel So Deserted’ by The Chemical Brothers imported to Spotify Library.
-> Track ‘The Hills’ by The Weeknd imported to Spotify Library.
-> Track ‘Dip’ by Danny Brown imported to Spotify Library.
-! No match for track on ‘Drive Me Crazy (feat. Vic Mensa)’ by KAYTRANADA from Apple Library playlist on Spotify.

Done! Your music library is now automatically being imported to Spotify. With apps like these, it paves the way for a more liberated music library that isn’t attached to any third party service but to you. Hopefully, in the future, we can bring this service online and add more drivers for all the music players. For now though, close iTunes and never go back.


If you thought this was useful or have any questions, reach out to me on Twitter or email. Thanks for reading! The source code for Polytunes is hosted on Github, come along and help out.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.