/
/
/
1"""Utility helpers for the Apple Music provider."""
2
3from __future__ import annotations
4
5import re
6from typing import Any
7
8from music_assistant_models.enums import MediaType
9from music_assistant_models.errors import MusicAssistantError
10
11
12def is_library_id(library_id: Any) -> bool:
13 """Return True if the ID matches the Apple Music library ID format."""
14 if not isinstance(library_id, str):
15 return False
16 return bool(re.fullmatch(r"[ailp]\.[a-zA-Z0-9]+", library_id))
17
18
19def is_catalog_id(catalog_id: str) -> bool:
20 """Return True if the ID is a catalog ID (numeric or starts with 'pl.')."""
21 return catalog_id.isnumeric() or catalog_id.startswith("pl.")
22
23
24def translate_media_type_to_apple_type(media_type: MediaType) -> str:
25 """Translate a MediaType to the Apple Music API endpoint segment."""
26 match media_type:
27 case MediaType.ARTIST:
28 return "artists"
29 case MediaType.ALBUM:
30 return "albums"
31 case MediaType.TRACK:
32 return "songs"
33 case MediaType.PLAYLIST:
34 return "playlists"
35 raise MusicAssistantError(f"Unsupported media type: {media_type}")
36