rryam / MusadoraKit

The ultimate companion to MusicKit.
http://exploringmusickit.com
MIT License
344 stars 22 forks source link

Share url for playlists #35

Closed bbauman1 closed 1 year ago

bbauman1 commented 1 year ago

Hey @rudrankriyam, this might not be possible from the api, but do you know if you can get a url to share library playlists?

It seems like there is a separate ID for playlists that is used in share urls, but I'm not able to find it. What I've tried:

In apple music you can get a shareable url for private library playlists like this

CleanShot 2023-09-24 at 09 37 46@2x

The url follows this format https://music.apple.com/us/playlist/${playlistId}

But when retrieving all library playlists, I took the ID of a playlist returned from the list and tried to recreate a url using that same format - https://music.apple.com/us/playlist/${playlistId}

For me this URL never works.

It appears to me that there is a different ID used for sharing playlists than the actual playlist ID returned from the api. What I'm seeing in my examples is that the share ID starts with pl. and the playlist ID returned from the api starts with p.

Any ideas?

rudrankriyam commented 1 year ago

Indeed, the library playlist has a unique identifier starting with p., and the catalogue playlist has pl..

You can get the catalog equivalent using MusadoraKit like this:


let libraryPlaylists = try! await MLibrary.playlists()

libraryPlaylists.forEach { libraryPlaylist in
  Task {
    debugPrint("Library playlist with ID: \(libraryPlaylist.id) and name: \(libraryPlaylist.name)")

    let catalogPlaylist = try? await libraryPlaylist.catalog

    debugPrint("Catalog playlist with ID: \(catalogPlaylist?.id) and name: \(catalogPlaylist?.name)")
  }
}

Otherwise, you can also get it in the PlayParameters like this:

let libraryPlaylists: LibraryPlaylists = try! await MLibrary.playlists(limit: 25)

libraryPlaylists.forEach { libraryPlaylist in
  Task {
    debugPrint("Library playlist with ID: \(libraryPlaylist.id), name: \(libraryPlaylist.attributes.name) and catalog ID: \(libraryPlaylist.globalID)")
  }
}

This is a custom structure in MusadoraKit LibraryPlaylist that is extremely helpful to work with library playlists (canEdit, isPublic, hasCatalog properties)

This is the output for me:

"Library playlist with ID: p.QvDQB9rsVbzXPGE, name: Adele and catalog ID: Optional(\"pl.741abd5bc88e43b88c3b31170f6afc4d\")"
"Library playlist with ID: p.gek1pdeULaPreDM, name: Bedtime Beats and catalog ID: Optional(\"pl.082e7836ea7a4244bf3f9c319560718f\")"
"Library playlist with ID: p.PkxVBgps2zOdV3r, name: BEAST MODE! The Ultimate Gym Motivation Playlist! and catalog ID: Optional(\"pl.81c09089b40740d6aefaf1c9db027c8a\")"
"Library playlist with ID: p.ZOAXdB5U4KXdrzA, name: Call Out My Name Vibes and catalog ID: Optional(\"pl.u-V9D7gLKcB15Ljla\")"
"Library playlist with ID: p.gek1Bq6tLaPreDM, name: Blessings (feat. Drake) Vibes and catalog ID: Optional(\"pl.u-e98lMKDizWD7Z61\")"
"Library playlist with ID: p.QvDQlA6sVbzXPGE, name: Cigarettes After Sex Essentials and catalog ID: Optional(\"pl.d39566769cae41d186ac2f9ad15ee0d1\")"
"Library playlist with ID: p.qQXL3WzHA7MoXk1, name: Chill Mix and catalog ID: Optional(\"pl.pm-58a310e0f946cdbcabea16797852ad60\")"

I hope this will help you work with share now!

bbauman1 commented 1 year ago

Ah got it. It looks like most of my playlists don't have a globalID unfortunately. Do you happen to know what makes a globalID become generated?

bbauman1 commented 1 year ago

Found some info here I'll close this thread. Thanks for the help https://developer.apple.com/forums/thread/707629

rudrankriyam commented 1 year ago

@bbauman1 can you check if the isPublic property for your playlist false?

bbauman1 commented 1 year ago

Yeah it is because the playlists were created via the api and it doesn't look like there is a way to make public playlists from the api