From 00648b8f948f0cd478e262e6329a321149cea265 Mon Sep 17 00:00:00 2001 From: jo Date: Sun, 19 Feb 2023 18:29:05 +0100 Subject: [PATCH] refactor(playout): unwrap schedule.media dict --- playout/libretime_playout/player/fetch.py | 11 +- playout/libretime_playout/player/schedule.py | 4 +- playout/tests/player/schedule_test.py | 434 +++++++++---------- 3 files changed, 224 insertions(+), 225 deletions(-) diff --git a/playout/libretime_playout/player/fetch.py b/playout/libretime_playout/player/fetch.py index 30705cba8..e823bb4b2 100644 --- a/playout/libretime_playout/player/fetch.py +++ b/playout/libretime_playout/player/fetch.py @@ -18,6 +18,7 @@ from ..config import CACHE_DIR, POLL_INTERVAL, Config from ..liquidsoap.client import LiquidsoapClient from ..liquidsoap.models import Info, StreamPreferences, StreamState from ..timeout import ls_timeout +from .events import Events from .liquidsoap import PypoLiquidsoap from .schedule import get_schedule @@ -56,7 +57,7 @@ class PypoFetch(Thread): self.cache_dir = CACHE_DIR logger.debug("Cache dir %s", self.cache_dir) - self.schedule_data = [] + self.schedule_data: Events = {} logger.info("PypoFetch: init complete") # Handle a message from RabbitMQ, put it into our yucky global var. @@ -75,7 +76,7 @@ class PypoFetch(Thread): logger.info("Handling command: %s", command) if command == "update_schedule": - self.schedule_data = m["schedule"] + self.schedule_data = m["schedule"]["media"] self.process_schedule(self.schedule_data) elif command == "reset_liquidsoap_bootstrap": self.set_bootstrap_variables() @@ -196,10 +197,10 @@ class PypoFetch(Thread): # to the cache dir (Folder-structure: cache/YYYY-MM-DD-hh-mm-ss) # - runs the cleanup routine, to get rid of unused cached files - def process_schedule(self, schedule_data): + def process_schedule(self, events: Events): self.last_update_schedule_timestamp = time.time() - logger.debug(schedule_data) - media = schedule_data["media"] + logger.debug(events) + media = events media_filtered = {} # Download all the media and put playlists in liquidsoap "annotate" format diff --git a/playout/libretime_playout/player/schedule.py b/playout/libretime_playout/player/schedule.py index 470289afc..8368ad5a0 100644 --- a/playout/libretime_playout/player/schedule.py +++ b/playout/libretime_playout/player/schedule.py @@ -32,7 +32,7 @@ def insert_event(events: Events, event_key: str, event: AnyEvent): events[key] = event -def get_schedule(api_client: ApiClient) -> Dict[Literal["media"], Events]: +def get_schedule(api_client: ApiClient) -> Events: stream_preferences = StreamPreferences(**api_client.get_stream_preferences().json()) current_time = datetime.utcnow() @@ -75,7 +75,7 @@ def get_schedule(api_client: ApiClient) -> Dict[Literal["media"], Events]: webstream = api_client.get_webstream(item["stream"]).json() generate_webstream_events(events, item, webstream, show) - return {"media": dict(sorted(events.items()))} + return dict(sorted(events.items())) def generate_live_events( diff --git a/playout/tests/player/schedule_test.py b/playout/tests/player/schedule_test.py index eca1549f3..10217b5cf 100644 --- a/playout/tests/player/schedule_test.py +++ b/playout/tests/player/schedule_test.py @@ -412,231 +412,229 @@ def test_get_schedule(schedule, requests_mock, api_client: ApiClient): requests_mock.get(f"{base_url}/api/v2/webstreams/1", json=WEBSTREAM_1) assert get_schedule(api_client) == { - "media": { - "2022-09-05-11-00-00": { - "type": EventKind.FILE, - "row_id": 1, - "start": "2022-09-05-11-00-00", - "end": "2022-09-05-11-05-02", - "uri": None, - "id": 2, - "show_name": "Show 1", - "fade_in": 500.0, - "fade_out": 500.0, - "cue_in": 13.7008, - "cue_out": 315.845, - "metadata": { - "track_title": "My Friend the Forest", - "artist_name": "Nils Frahm", - "mime": "audio/flac", - }, - "replay_gain": "11.46", - "filesize": 10000, + "2022-09-05-11-00-00": { + "type": EventKind.FILE, + "row_id": 1, + "start": "2022-09-05-11-00-00", + "end": "2022-09-05-11-05-02", + "uri": None, + "id": 2, + "show_name": "Show 1", + "fade_in": 500.0, + "fade_out": 500.0, + "cue_in": 13.7008, + "cue_out": 315.845, + "metadata": { + "track_title": "My Friend the Forest", + "artist_name": "Nils Frahm", + "mime": "audio/flac", }, - "2022-09-05-11-05-02": { - "type": EventKind.FILE, - "row_id": 2, - "start": "2022-09-05-11-05-02", - "end": "2022-09-05-11-10-00", - "uri": None, - "id": 4, - "show_name": "Show 1", - "fade_in": 500.0, - "fade_out": 500.0, - "cue_in": 0.0, - "cue_out": 297.8558, - "metadata": { - "track_title": "#2", - "artist_name": "Nils Frahm", - "mime": "audio/flac", - }, - "replay_gain": "-1.65", - "filesize": 10000, + "replay_gain": "11.46", + "filesize": 10000, + }, + "2022-09-05-11-05-02": { + "type": EventKind.FILE, + "row_id": 2, + "start": "2022-09-05-11-05-02", + "end": "2022-09-05-11-10-00", + "uri": None, + "id": 4, + "show_name": "Show 1", + "fade_in": 500.0, + "fade_out": 500.0, + "cue_in": 0.0, + "cue_out": 297.8558, + "metadata": { + "track_title": "#2", + "artist_name": "Nils Frahm", + "mime": "audio/flac", }, - "2022-09-05-11-10-00": { - "type": EventKind.FILE, - "row_id": 3, - "start": "2022-09-05-11-10-00", - "end": "2022-09-05-12-08-59", - "uri": None, - "id": 5, - "show_name": "Show 2", - "fade_in": 500.0, - "fade_out": 500.0, - "cue_in": 0.0, - "cue_out": 3539.13, - "metadata": { - "track_title": "Democracy Now! 2022-09-05 Monday", - "artist_name": "Democracy Now! Audio", - "mime": "audio/mp3", - }, - "replay_gain": "-1.39", - "filesize": 10000, + "replay_gain": "-1.65", + "filesize": 10000, + }, + "2022-09-05-11-10-00": { + "type": EventKind.FILE, + "row_id": 3, + "start": "2022-09-05-11-10-00", + "end": "2022-09-05-12-08-59", + "uri": None, + "id": 5, + "show_name": "Show 2", + "fade_in": 500.0, + "fade_out": 500.0, + "cue_in": 0.0, + "cue_out": 3539.13, + "metadata": { + "track_title": "Democracy Now! 2022-09-05 Monday", + "artist_name": "Democracy Now! Audio", + "mime": "audio/mp3", }, - "2022-09-05-12-08-59": { - "type": EventKind.FILE, - "row_id": 4, - "start": "2022-09-05-12-08-59", - "end": "2022-09-05-12-10-00", - "uri": None, - "id": 4, - "show_name": "Show 2", - "fade_in": 500.0, - "fade_out": 500.0, - "cue_in": 0.0, - "cue_out": 61.0, - "metadata": { - "track_title": "#2", - "artist_name": "Nils Frahm", - "mime": "audio/flac", - }, - "replay_gain": "-1.65", - "filesize": 10000, + "replay_gain": "-1.39", + "filesize": 10000, + }, + "2022-09-05-12-08-59": { + "type": EventKind.FILE, + "row_id": 4, + "start": "2022-09-05-12-08-59", + "end": "2022-09-05-12-10-00", + "uri": None, + "id": 4, + "show_name": "Show 2", + "fade_in": 500.0, + "fade_out": 500.0, + "cue_in": 0.0, + "cue_out": 61.0, + "metadata": { + "track_title": "#2", + "artist_name": "Nils Frahm", + "mime": "audio/flac", }, - "2022-09-05-12-10-00": { - "type": EventKind.WEB_STREAM_BUFFER_START, - "row_id": 5, - "start": "2022-09-05-12-09-55", - "end": "2022-09-05-12-09-55", - "uri": "http://stream.radio.org/main.ogg", - "id": 1, + "replay_gain": "-1.65", + "filesize": 10000, + }, + "2022-09-05-12-10-00": { + "type": EventKind.WEB_STREAM_BUFFER_START, + "row_id": 5, + "start": "2022-09-05-12-09-55", + "end": "2022-09-05-12-09-55", + "uri": "http://stream.radio.org/main.ogg", + "id": 1, + }, + "2022-09-05-12-10-00_0": { + "type": EventKind.WEB_STREAM_OUTPUT_START, + "row_id": 5, + "start": "2022-09-05-12-10-00", + "end": "2022-09-05-12-40-00", + "uri": "http://stream.radio.org/main.ogg", + "id": 1, + "show_name": "Show 3", + }, + "2022-09-05-12-40-00": { + "type": EventKind.WEB_STREAM_BUFFER_END, + "row_id": 5, + "start": "2022-09-05-12-40-00", + "end": "2022-09-05-12-40-00", + "uri": "http://stream.radio.org/main.ogg", + "id": 1, + }, + "2022-09-05-12-40-00_0": { + "type": EventKind.WEB_STREAM_OUTPUT_END, + "row_id": 5, + "start": "2022-09-05-12-40-00", + "end": "2022-09-05-12-40-00", + "uri": "http://stream.radio.org/main.ogg", + "id": 1, + }, + "2022-09-05-12-40-00_1": { + "type": EventKind.FILE, + "row_id": 6, + "start": "2022-09-05-12-40-00", + "end": "2022-09-05-12-53-23", + "uri": None, + "id": 3, + "show_name": "Show 3", + "fade_in": 500.0, + "fade_out": 500.0, + "cue_in": 55.1211, + "cue_out": 858.4, + "metadata": { + "track_title": "All Melody", + "artist_name": "Nils Frahm", + "mime": "audio/flac", }, - "2022-09-05-12-10-00_0": { - "type": EventKind.WEB_STREAM_OUTPUT_START, - "row_id": 5, - "start": "2022-09-05-12-10-00", - "end": "2022-09-05-12-40-00", - "uri": "http://stream.radio.org/main.ogg", - "id": 1, - "show_name": "Show 3", + "replay_gain": "-2.13", + "filesize": 10000, + }, + "2022-09-05-12-53-23": { + "type": EventKind.FILE, + "row_id": 7, + "start": "2022-09-05-12-53-23", + "end": "2022-09-05-12-58-25", + "uri": None, + "id": 2, + "show_name": "Show 3", + "fade_in": 500.0, + "fade_out": 500.0, + "cue_in": 13.7008, + "cue_out": 315.845, + "metadata": { + "track_title": "My Friend the Forest", + "artist_name": "Nils Frahm", + "mime": "audio/flac", }, - "2022-09-05-12-40-00": { - "type": EventKind.WEB_STREAM_BUFFER_END, - "row_id": 5, - "start": "2022-09-05-12-40-00", - "end": "2022-09-05-12-40-00", - "uri": "http://stream.radio.org/main.ogg", - "id": 1, + "replay_gain": "11.46", + "filesize": 10000, + }, + "2022-09-05-12-58-25": { + "type": EventKind.FILE, + "row_id": 8, + "start": "2022-09-05-12-58-25", + "end": "2022-09-05-13-00-00", + "uri": None, + "id": 1, + "show_name": "Show 3", + "fade_in": 500.0, + "fade_out": 500.0, + "cue_in": 8.25245, + "cue_out": 95.0, + "metadata": { + "track_title": "The Dane", + "artist_name": "Nils Frahm", + "mime": "audio/flac", }, - "2022-09-05-12-40-00_0": { - "type": EventKind.WEB_STREAM_OUTPUT_END, - "row_id": 5, - "start": "2022-09-05-12-40-00", - "end": "2022-09-05-12-40-00", - "uri": "http://stream.radio.org/main.ogg", - "id": 1, + "replay_gain": "4.52", + "filesize": 10000, + }, + "2022-09-05-12-59-58": { + "type": EventKind.ACTION, + "event_type": "switch_off", + "start": "2022-09-05-12-59-58", + "end": "2022-09-05-12-59-58", + }, + "2022-09-05-13-00-00": { + "type": EventKind.ACTION, + "event_type": "kick_out", + "start": "2022-09-05-13-00-00", + "end": "2022-09-05-13-00-00", + }, + "2022-09-05-13-00-00_0": { + "type": EventKind.FILE, + "row_id": 9, + "start": "2022-09-05-13-00-00", + "end": "2022-09-05-13-05-02", + "uri": None, + "id": 2, + "show_name": "Show 4", + "fade_in": 500.0, + "fade_out": 500.0, + "cue_in": 13.7008, + "cue_out": 315.845, + "metadata": { + "track_title": "My Friend the Forest", + "artist_name": "Nils Frahm", + "mime": "audio/flac", }, - "2022-09-05-12-40-00_1": { - "type": EventKind.FILE, - "row_id": 6, - "start": "2022-09-05-12-40-00", - "end": "2022-09-05-12-53-23", - "uri": None, - "id": 3, - "show_name": "Show 3", - "fade_in": 500.0, - "fade_out": 500.0, - "cue_in": 55.1211, - "cue_out": 858.4, - "metadata": { - "track_title": "All Melody", - "artist_name": "Nils Frahm", - "mime": "audio/flac", - }, - "replay_gain": "-2.13", - "filesize": 10000, + "replay_gain": "11.46", + "filesize": 10000, + }, + "2022-09-05-13-05-02": { + "type": EventKind.FILE, + "row_id": 10, + "start": "2022-09-05-13-05-02", + "end": "2022-09-05-13-10-00", + "uri": None, + "id": 4, + "show_name": "Show 4", + "fade_in": 500.0, + "fade_out": 500.0, + "cue_in": 0.0, + "cue_out": 297.8558, + "metadata": { + "track_title": "#2", + "artist_name": "Nils Frahm", + "mime": "audio/flac", }, - "2022-09-05-12-53-23": { - "type": EventKind.FILE, - "row_id": 7, - "start": "2022-09-05-12-53-23", - "end": "2022-09-05-12-58-25", - "uri": None, - "id": 2, - "show_name": "Show 3", - "fade_in": 500.0, - "fade_out": 500.0, - "cue_in": 13.7008, - "cue_out": 315.845, - "metadata": { - "track_title": "My Friend the Forest", - "artist_name": "Nils Frahm", - "mime": "audio/flac", - }, - "replay_gain": "11.46", - "filesize": 10000, - }, - "2022-09-05-12-58-25": { - "type": EventKind.FILE, - "row_id": 8, - "start": "2022-09-05-12-58-25", - "end": "2022-09-05-13-00-00", - "uri": None, - "id": 1, - "show_name": "Show 3", - "fade_in": 500.0, - "fade_out": 500.0, - "cue_in": 8.25245, - "cue_out": 95.0, - "metadata": { - "track_title": "The Dane", - "artist_name": "Nils Frahm", - "mime": "audio/flac", - }, - "replay_gain": "4.52", - "filesize": 10000, - }, - "2022-09-05-12-59-58": { - "type": EventKind.ACTION, - "event_type": "switch_off", - "start": "2022-09-05-12-59-58", - "end": "2022-09-05-12-59-58", - }, - "2022-09-05-13-00-00": { - "type": EventKind.ACTION, - "event_type": "kick_out", - "start": "2022-09-05-13-00-00", - "end": "2022-09-05-13-00-00", - }, - "2022-09-05-13-00-00_0": { - "type": EventKind.FILE, - "row_id": 9, - "start": "2022-09-05-13-00-00", - "end": "2022-09-05-13-05-02", - "uri": None, - "id": 2, - "show_name": "Show 4", - "fade_in": 500.0, - "fade_out": 500.0, - "cue_in": 13.7008, - "cue_out": 315.845, - "metadata": { - "track_title": "My Friend the Forest", - "artist_name": "Nils Frahm", - "mime": "audio/flac", - }, - "replay_gain": "11.46", - "filesize": 10000, - }, - "2022-09-05-13-05-02": { - "type": EventKind.FILE, - "row_id": 10, - "start": "2022-09-05-13-05-02", - "end": "2022-09-05-13-10-00", - "uri": None, - "id": 4, - "show_name": "Show 4", - "fade_in": 500.0, - "fade_out": 500.0, - "cue_in": 0.0, - "cue_out": 297.8558, - "metadata": { - "track_title": "#2", - "artist_name": "Nils Frahm", - "mime": "audio/flac", - }, - "replay_gain": "-1.65", - "filesize": 10000, - }, - } + "replay_gain": "-1.65", + "filesize": 10000, + }, }