/
/
1"""Helper functions for Radio Paradise provider."""
2
3import time
4from typing import Any
5
6
7def get_current_block_position(block_data: dict[str, Any]) -> int:
8 """
9 Calculate current playback position within a Radio Paradise block.
10
11 :param block_data: Block data containing sched_time_millis.
12 """
13 current_time_ms = int(time.time() * 1000)
14 sched_time = int(block_data.get("sched_time_millis", current_time_ms))
15 return current_time_ms - sched_time
16
17
18def find_current_song(
19 songs: dict[str, dict[str, Any]], current_time_ms: int
20) -> dict[str, Any] | None:
21 """
22 Find which song should currently be playing based on elapsed time.
23
24 :param songs: Dictionary of songs from Radio Paradise block data.
25 :param current_time_ms: Current position in milliseconds within the block.
26 """
27 sorted_keys = sorted(songs.keys(), key=int)
28
29 for song_key in sorted_keys:
30 song = songs[song_key]
31 song_start = int(song.get("elapsed", 0))
32 song_duration = int(song.get("duration", 0))
33 song_end = song_start + song_duration
34
35 if song_start <= current_time_ms < song_end:
36 return song
37
38 # If no exact match, return first song
39 first_song = songs.get("0")
40 return first_song if first_song is not None else None
41
42
43def get_next_song(songs: dict[str, Any], current_song: dict[str, Any]) -> dict[str, Any] | None:
44 """
45 Get the next song that will play after the current song.
46
47 :param songs: Dictionary of songs from Radio Paradise block data.
48 :param current_song: The currently playing song dictionary.
49 """
50 current_event = current_song.get("event")
51 current_elapsed = int(current_song.get("elapsed", 0))
52 sorted_keys = sorted(songs.keys(), key=int)
53
54 for song_key in sorted_keys:
55 song: dict[str, Any] = songs[song_key]
56 if song.get("event") != current_event and int(song.get("elapsed", 0)) > current_elapsed:
57 return song
58 return None
59