Some simple Python 3 scripts to help you into importing your existing Spotify library into Apple Music.
Work in progress !
The first step is getting the songs you want to import into Apple Music into a CSV file. The simplest way to do this is using Exportify.
If you want to export you whole Spotify library, simply create a new playlist called All and drag your whole library into it using the Spotify desktop app. You can then export the playlist All using Exportify. Save the resulting file as spotify.csv in the same directory as the directory you cloned this repo into.
If you just want 1 particular playlist (as I was interested specifically in the 'Discover Weekly Playlist'), you can use the export-spotify-dw-playlist.py script found in this repository.
In order to add songs to our Apple Music library, we need their Apple Music identifier. Running python3 retrieve-identifiers.py
will use the spotify.csv file to create a new file itunes.csv with each line consisting of the Apple Music identifier of a song in your Spotify library.
Start iTunes and Charles (or another intercepting proxy you like). Make sure SSL Proxying is enabled and working correctly. Next, select a random song on Apple Music you don't have in your library yet, right click and choose 'Add to library'. If everything went well, you're now able to view all the request headers in Charles of a request to https://ld-4.itunes.apple.com/WebObjects/MZDaap.woa/daap/databases/1/cloud-add
. We're only interested in Cookie
, X-Dsid
and X-Guid
. Copy the value of these header and paste them in the appropriate place in insert-songs.py
(line 29 and further).
Next, run python3 insert-songs.py
and go grab a coffee. You're songs are now being imported into Apple Music.
Apple Music doesn't like it when we're adding a lot of songs in a small amount of time. If we do so, the API responds to all further request with 403 Too many requests
. After this, you're blocked from the API for an undetermined amount of time. We're currently trying to avoid this by waiting after each request. A delay of 30 seconds seems fine to import large libraries into Apple Music. If you're library is smaller, feel free to decrease the delay in inserts-songs.py
.
The original script used a method to retrieve the Apple Music identifier for a Spotify song that was quite basic. It simply compared the title and artist to find out if a Spotify and Apple Music song match. Some songs don't have the exact same title (extraneous spacing for example) in both services. This resulted in the script failing to retrieve an identifier for quite a number of songs. I've decided to add album as a third field in the input CSV file so that this can be used as an additional search term. Furthermore I added the artist to the song title for the initial search, to yield a result set from Apple Music that is more likely to contain the result we need. This improved the results considerably.
Some users are reporting errors like UnicodeEncodeError: 'ascii' codec can't encode character '\xd8' in ...
when running the scripts. The problem here is that Python is trying to convert UTF-8 characters to ASCII to print them to standard output. This can be solved by setting the PYTHONIOENCODING
environment variable to utf-8
.
In Bash, simply call the scripts like this: PYTHONIOENCODING=utf-8 python3 retrieve-identifiers.py
and PYTHONIOENCODING=utf-8 python3 insert-songs.Py