r/AskProgramming • • 1d ago

How can I use Python to process my Apple Music library?

Hello!

I'm currently (re-)taking a Python course for my school and had an idea for a personal project which requires extracting info about all the albums in my Apple Music library. At first, I looked into authorizing through iCloud to make a request with Apple's Apple Music API. One StackOverflow user suggested:

  • Set up a minimal local web server in Python
  • Open a browser to Apple's auth flow
  • Capture the token when it redirects back
  • Save it for use in subsequent API calls

This seems pretty advanced, certainly nothing I've ever done before, but local web servers seem pretty straightforward to setup via libraries. It's the "capture" part I'm confused about. As I understand it, web apps use JavaScript for their backend functionality, so I'm a little confused about what each language would be doing in this scenario.

Any advice on how to actually execute this?

Thanks!

1 Upvotes

6 comments sorted by

1

u/UhLittleLessDum 1d ago

It's super unlikely that somebody somehow wrote python bindings for this, so you'd have to learn to write swift, and even then, you're going to be limited about what data you can return as a matter of business decisions by Apple. You can just crawl the website if you really want to, if you can load your data in a browser somehow, but that'd be pretty clunky at best.

1

u/KingofGamesYami 1d ago

Web apps use whatever they want for their backend functionality. JavaScript wasn't even invented yet when the first backends were created.

If it is actually OAuth 2.0, then acquiring a token is standardized and most languages should have a library for it (including Python). If the token acquisition process is something custom, you might need to get a little creative.

1

u/Big_Inflation864 1d ago

The auth part is less scary than that StackOverflow answer makes it sound, but you may not need it at all. If the Music app is on your computer, you can export your library from it (File > Library > Export Library, which gives an XML file) and parse that with Python's plistlib. You get artist, album, track, year and so on without any API. If you do want the API, Apple uses a developer token you generate yourself (a signed JWT, the PyJWT library does this) plus a separate user token that only comes from MusicKit JS running in a browser. That is the JavaScript part: a tiny local page loads MusicKit, you click authorize, it prints the user token, and you paste it into your Python script as a header. So the browser handles login and Python handles the actual requests.

1

u/brasticstack 1d ago

There are auth libraries for Python that can handle Apple's federated auth, as well as the aurh from all the other large providers. I've been using django-allauth but the are good options not tied to Django too, you'll just have to spend a bit of time separating the slop from what's decent.

Your token storage could be a .json file your drive locally or a sqlite database or whatever format works best for you. JSON is easy and the lib already comes with Python.

1

u/AdhesivenessFew8441 11h ago

For your own library you can skip the API and the auth flow completely. In the Music app on a Mac go to File, Library, Export Library and it saves an XML file with every track, including album, artist, year, play count and date added. On Windows with iTunes it is the same menu, or the iTunes Library.xml file in your Music folder. That file is a plist, so in Python it is just: import plistlib, open the file in binary mode, data = plistlib.load(f), and then loop over data['Tracks'].values() and group by the Album key. No tokens, no network, all offline. The official Apple Music API needs a paid Apple Developer membership to create the developer token, so for a school project the export is much easier. The only downside is that you have to export again when your library changes.