Use assets API for song HLS streams

This commit is contained in:
glomatico
2026-06-30 09:55:48 -03:00
parent b0c5335767
commit a7d141b7b1
3 changed files with 54 additions and 10 deletions
+36
View File
@@ -11,6 +11,7 @@ from .constants import (
APPLE_MUSIC_ALBUM_API_URI,
APPLE_MUSIC_AMP_API_URL,
APPLE_MUSIC_ARTIST_API_URI,
APPLE_MUSIC_ASSETS_API_URI,
APPLE_MUSIC_COOKIE_DOMAIN,
APPLE_MUSIC_HOMEPAGE_URL,
APPLE_MUSIC_LIBRARY_ALBUM_API_URI,
@@ -633,6 +634,41 @@ class AppleMusicApi:
return search_results
async def get_assets(
self,
media_id: str,
kind: str = "song",
include_license_urls: bool = True,
hls_encryption: str = "CBC",
hls_profile: str = "enhancedHls",
) -> dict:
log = logger.bind(
action="get_assets",
media_id=media_id,
kind=kind,
include_license_urls=include_license_urls,
hls_encryption=hls_encryption,
hls_profile=hls_profile,
)
params = {
"id": media_id,
"kind": kind,
"includeLicenseUrls": include_license_urls,
"hlsEncryption": hls_encryption,
}
if hls_profile:
params["hlsProfile"] = hls_profile
assets = await self._amp_request(
APPLE_MUSIC_ASSETS_API_URI,
params,
)
log.debug("success", assets=assets)
return assets
async def get_extended_api_data(
self,
next_uri: str | None,
+1
View File
@@ -21,6 +21,7 @@ APPLE_MUSIC_LIBRARY_MUSIC_VIDEO_API_URI = (
APPLE_MUSIC_LIBRARY_ALBUM_API_URI = "/v1/me/library/albums/{album_id}"
APPLE_MUSIC_LIBRARY_PLAYLIST_API_URI = "/v1/me/library/playlists/{playlist_id}"
APPLE_MUSIC_SEARCH_API_URI = "/v1/catalog/{storefront}/search"
APPLE_MUSIC_ASSETS_API_URI = "/v1/play/assets"
APPLE_MUSIC_LIBRARY_SONGS_API_URI = "/v1/me/library/songs"
APPLE_MUSIC_LIBRARY_MUSIC_VIDEOS_API_URI = "/v1/me/library/music-videos"
APPLE_MUSIC_LIBRARY_ALBUMS_API_URI = "/v1/me/library/albums"
+17 -10
View File
@@ -210,12 +210,12 @@ class AppleMusicSongInterface:
log.debug("no_m3u8_master_url")
async def _get_m3u8_master_url_from_metadata(
async def _get_m3u8_master_url_from_assets(
self,
song_metadata: dict,
) -> str | None:
log = logger.bind(
action="get_m3u8_master_url_from_metadata",
action="get_m3u8_master_url_from_assets",
song_id=song_metadata["id"],
)
@@ -223,14 +223,21 @@ class AppleMusicSongInterface:
log.debug("library_song_no_m3u8_master_url")
return None
if "extendedAssetUrls" not in song_metadata["attributes"]:
song_metadata = (
await self.base.apple_music_api.get_song(
song_metadata["id"],
)
)["data"][0]
play_params = song_metadata["attributes"].get("playParams", {})
assets = await self.base.apple_music_api.get_assets(
play_params.get("id") or song_metadata["id"],
play_params.get("kind", "song"),
)
enhanced = song_metadata["attributes"]["extendedAssetUrls"].get("enhancedHls")
asset = next(
(
asset
for asset in assets.get("results", {}).get("assets", [])
if asset.get("url")
),
None,
)
enhanced = asset["url"] if asset else None
if enhanced:
enhanced = self._switch_m3u8_master_url_to_default(enhanced)
@@ -249,7 +256,7 @@ class AppleMusicSongInterface:
if playback:
return self._get_m3u8_from_playback(playback)
else:
return await self._get_m3u8_master_url_from_metadata(song_metadata)
return await self._get_m3u8_master_url_from_assets(song_metadata)
async def get_stream_info(
self,