music-assistant-server

3.1 KBPY
parsers.py
3.1 KB99 lines • python
1"""Parsers for Radio Paradise provider."""
2
3from typing import Any
4
5from music_assistant_models.enums import ImageType
6from music_assistant_models.media_items import (
7    MediaItemImage,
8    ProviderMapping,
9    Radio,
10)
11from music_assistant_models.streamdetails import StreamMetadata
12
13from .constants import RADIO_PARADISE_CHANNELS, STATION_ICONS_BASE_URL
14from .helpers import enhance_title_with_upcoming  # noqa: F401
15
16
17def parse_radio(channel_id: str, instance_id: str, provider_domain: str) -> Radio:
18    """Create a Radio object from cached channel information."""
19    channel_info = RADIO_PARADISE_CHANNELS.get(channel_id, {})
20
21    radio = Radio(
22        provider=instance_id,
23        item_id=channel_id,
24        name=channel_info.get("name", "Unknown Radio"),
25        provider_mappings={
26            ProviderMapping(
27                provider_domain=provider_domain,
28                provider_instance=instance_id,
29                item_id=channel_id,
30                available=True,
31            )
32        },
33    )
34
35    # Add static station icon
36    station_icon = channel_info.get("station_icon")
37    if station_icon:
38        icon_url = f"{STATION_ICONS_BASE_URL}/{station_icon}"
39        radio.metadata.add_image(
40            MediaItemImage(
41                provider=instance_id,
42                type=ImageType.THUMB,
43                path=icon_url,
44                remotely_accessible=True,
45            )
46        )
47
48    return radio
49
50
51def build_stream_metadata(current_song: dict[str, Any], metadata: dict[str, Any]) -> StreamMetadata:  # noqa: ARG001
52    """Build StreamMetadata with current track info and upcoming tracks.
53
54    Args:
55        current_song: Current track data from Radio Paradise API
56        metadata: Full metadata response with next song and block data
57
58    Returns:
59        StreamMetadata with track info and upcoming track previews
60    """
61    # Extract track info
62    artist = current_song.get("artist", "Unknown Artist")
63    title = current_song.get("title", "Unknown Title")
64    album = current_song.get("album")
65    year = current_song.get("year")
66
67    # Build album string with year if available
68    album_display = album
69    if album and year:
70        album_display = f"{album} ({year})"
71    elif year:
72        album_display = str(year)
73
74    # Get cover image URL
75    cover_path = current_song.get("cover")
76    image_url = None
77    if cover_path:
78        image_url = f"https://img.radioparadise.com/{cover_path}"
79
80    # Get track duration
81    duration = current_song.get("duration")
82    if duration:
83        duration = int(duration) // 1000  # Convert from ms to seconds
84
85    # Add upcoming tracks info to title for scrolling display
86    # next_song = metadata.get("next")
87    # block_data = metadata.get("block_data")
88    # TODO: Find a way to forward the next_song data to the frontend in the stream metadata
89    # enhanced_title = enhance_title_with_upcoming(title, current_song, next_song, block_data)
90    # enhanced_title = title  # TODO remove after frontend update
91
92    return StreamMetadata(
93        title=title,
94        artist=artist,
95        album=album_display,
96        image_url=image_url,
97        duration=duration,
98    )
99