Skip to content

Myp3a/apple-music-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

72 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Apple Music API

A library to interact with Apple Music API, both official and reverse-engineered. It allows you to use the catalog, control your library and download music.

FeaturesInstallationRequirementsUsageTODO

Features

  • Catalog search
  • Library search
  • Library edits
  • Playlists edits
  • Media downloads

Installation

You can install the library by using:

pip install git+https://github.com/Myp3a/apple-music-api.git

Requirements

You'll need three things to use all features of the library:

  • Apple Developer Token
  • Music User Token
  • Widevine Device File

Apple Developer Token

Allows interaction with all public API's. Required.
You can get it with the instruction here.

Probably, automatic token retriever could be made - Music app fetches the token with the request to https://sf-api-token-service.itunes.apple.com/apiToken, passing a lot of unknown data. If the data is app-specific, then there is a possibility that a developer token won't be needed anymore. If the data is tied to the user, then, well, everything will be as it is.

Music User Token

Allows interaction with your library. Required for library functions.
You can get it by following the instruction here

Alternative way

You can get both tokens by intercepting requests to https://music.apple.com/.

Widevine Device File

Required for decryption of songs.
You'll need an Android device for it. Dump keys using dumper, and then use pywidevine to generate device file using this command:

pywidevine create-device -t ANDROID -l 3 -k private_key.pem -c client_id.bin -o .

Usage

Example of searching catalog song information without user token:

import applemusic
from applemusic.api.catalog import CatalogTypes

DEV_TOKEN = "dev_token_goes_here"

cli = applemusic.ApiClient(DEV_TOKEN, storefront="us")

songs = cli.catalog.search("Ellie Goulding", CatalogTypes.Songs)
print(songs[0].name)           # Lights
print(songs[0].artist_name)    # Ellie Goulding

Adding library song to playlist

import applemusic
from applemusic.api.library import LibraryTypes

DEV_TOKEN = "dev_token_goes_here"
USER_TOKEN = "and_user_one_here"

cli = applemusic.ApiClient(DEV_TOKEN, USER_TOKEN)

songs = cli.library.search("Against The Current", LibraryTypes.Songs)
song = songs[0]
playlist = cli.playlist.create_playlist("My new playlist")
playlist.add_to_playlist(song)

Downloading a song

import applemusic
from applemusic.api.library import LibraryTypes

DEV_TOKEN = "dev_token_goes_here"
USER_TOKEN = "and_user_one_here"
WIDEVINE_DEVICE = "device.wvd"

cli = applemusic.ApiClient(DEV_TOKEN, USER_TOKEN, widevine_device_file=WIDEVINE_DEVICE)

songs = cli.library.search("TMNV", LibraryTypes.Songs)
song = songs[0]
catalog_song = song.get_catalog_song()

with open("song.m4a","wb") as outfile:
    outfile.write(catalog_song.audio())

TODO

  • Make easier bindings for actions
  • Cover more API methods
  • Make docs

Disclaimer: Apple Music API is an unofficial application and not affiliated with Apple.

About

WIP wrapper for Apple Music API

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages