Skip to content

Instantly share code, notes, and snippets.

@EderSantana
Last active June 29, 2020 08:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EderSantana/fd217c2e79359ae9de27c0595d9bce4b to your computer and use it in GitHub Desktop.
Save EderSantana/fd217c2e79359ae9de27c0595d9bce4b to your computer and use it in GitHub Desktop.
Updated insert_songs.py from spotify2am
import time
import struct
import urllib.parse, urllib.request
def construct_request_body(timestamp, itunes_identifier):
hex = "61 6a 43 41 00 00 00 45 6d 73 74 63 00 00 00 04 55 94 17 a3 6d 6c 69 64 00 00 00 04 00 00 00 00 6d 75 73 72 00 00 00 04 00 00 00 81 6d 69 6b 64 00 00 00 01 02 6d 69 64 61 00 00 00 10 61 65 41 69 00 00 00 08 00 00 00 00 11 8c d9 2c 00"
body = bytearray.fromhex(hex);
body[16:20] = struct.pack('>I', timestamp)
body[-5:] = struct.pack('>I', itunes_identifier)
return body
def add_song(itunes_identifier):
data = construct_request_body(int(time.time()), itunes_identifier)
headers = {
"Host": "ld-7.itunes.apple.com:443",
"X-Apple-Private-Listening": "false",
"Proxy-Connection": "keep-alive",
"X-Apple-Store-Front": "143441-1,32",
"Client-iTunes-Sharing-Version": "3.15",
"Accept-Language": "en-US;q=1.0",
"Client-Cloud-DAAP-Version": "1.3/iTunes-12.8.0.150",
"Accept-Encoding": "gzip",
"X-Apple-itre": "0",
"Client-DAAP-Version": "3.13",
"User-Agent": "iTunes/12.8 (Macintosh; OS X 10.13.6) AppleWebKit/605.3.8 hwp/t8002 (dt:1)",
"Connection": "keep-alive",
"Content-Type": "application/x-dmap-tagged",
# Replace the values of the next three headers with the values you intercepted
"X-Dsid" : "TODO:REPLACE_THIS",
"Cookie" : "TODO:REPLACE_THIS",
"X-Guid" : "TODO:REPLACE_THIS",
"Content-Length" : "77"
}
request = urllib.request.Request("https://ld-4.itunes.apple.com/WebObjects/MZDaap.woa/daap/databases/1/cloud-add", data, headers)
urllib.request.urlopen(request)
with open('itunes.csv') as itunes_identifiers_file:
for line in itunes_identifiers_file:
itunes_identifier = int(line)
try:
add_song(itunes_identifier)
print("Successfuly inserted a song!")
# Try playing with the interval here to circumvent the API rate limit
time.sleep(30)
except Exception as e:
print("Something went wrong while inserting " + str(itunes_identifier) + " : " + str(e))

Follow the general instructions at https://github.com/simonschellaert/spotify2am .
This will export your Spotify playlist to csv and add the songs to your iTunes Library.

Notes:
For the proxy part I used https://mitmproxy.org/ instead of Charles. Charles isn't free.

mitmproxy is fairly easy to use, just don't forget to setup your HTTPS proxy server at 127.0.0.1 port 8080 (Settings -> Network -> Advanced -> Proxies).

If the insert_songs.py step doesn't work for you, try the one in this gist.
Note the TODO:REPLACE_THIS , you will get that info on mitmproxy when you add a new song from Apple Music to your Library.
For me, it showed up on a POST request to ld-7.itunes.apple.com If that file doesn't work either, use mitmproxy to see all the other header info to update your request.

spotify2am will add all the songs to the general library. To create individual playlists once all the songs are added to your Library, do:

  1. convert the csv file exported in the process above to tab separated values https://github.com/sandeharsa/SpotifyToiTunes
  2. use iTunes -> File -> Library -> Import Playlist on your tab separated file. Playlist will have same name as your tab file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment