Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions music_assistant/providers/apple_music/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import pathlib
import re
import time
import unicodedata
from typing import TYPE_CHECKING, Any

import aiofiles
Expand Down Expand Up @@ -290,6 +291,26 @@ class AppleMusicProvider(MusicProvider):
# so make it an instance attribute
throttler = ThrottlerManager(rate_limit=1, period=2, initial_backoff=15)

@staticmethod
def _normalize_unicode(value: str | None) -> str | None:
"""Normalize Unicode strings to NFC form for consistent handling.

This ensures that Unicode characters like "é" are stored as single
codepoints rather than "e" + combining accent mark, which prevents
issues with string comparisons and memory bloat.

Args:
value: String to normalize, or None

Returns:
Normalized string, or None if input was None
"""
if value is None:
return None
if not isinstance(value, str):
return str(value)
return unicodedata.normalize("NFC", value)

async def handle_async_init(self) -> None:
"""Handle async initialization of the provider."""
self._music_user_token = self.config.get_value(
Expand Down Expand Up @@ -704,7 +725,7 @@ def _parse_artist(self, artist_obj: dict[str, Any]) -> Artist:
)
artist = Artist(
item_id=artist_id,
name=attributes.get("name"),
name=self._normalize_unicode(attributes.get("name")),
provider=self.domain,
provider_mappings={
ProviderMapping(
Expand Down Expand Up @@ -765,7 +786,7 @@ def _parse_album(
album = Album(
item_id=album_id,
provider=self.domain,
name=attributes.get("name"),
name=self._normalize_unicode(attributes.get("name")),
provider_mappings={
ProviderMapping(
item_id=album_id,
Expand All @@ -785,7 +806,7 @@ def _parse_album(
media_type=MediaType.ARTIST,
provider=self.lookup_key,
item_id=artist_name,
name=artist_name,
name=self._normalize_unicode(artist_name),
)
]
)
Expand Down Expand Up @@ -851,7 +872,7 @@ def _parse_track(
track = Track(
item_id=track_id,
provider=self.domain,
name=attributes.get("name"),
name=self._normalize_unicode(attributes.get("name")),
duration=attributes.get("durationInMillis", 0) / 1000,
provider_mappings={
ProviderMapping(
Expand Down Expand Up @@ -880,7 +901,7 @@ def _parse_track(
media_type=MediaType.ARTIST,
item_id=artist_name,
provider=self.lookup_key,
name=artist_name,
name=self._normalize_unicode(artist_name),
)
]
if albums := relationships.get("albums"):
Expand Down
Loading