feat(playout): replace schedule event dicts with objects

This commit is contained in:
jo 2023-03-04 21:50:12 +01:00 committed by Kyle Robbertze
parent 684e7a6f24
commit a1db2a157a
11 changed files with 646 additions and 667 deletions

View file

@ -1,11 +1,16 @@
import random
from datetime import timedelta
from datetime import datetime
import pytest
from dateutil.parser import isoparse
from libretime_api_client.v2 import ApiClient
from libretime_playout.player.events import EventKind
from libretime_playout.player.events import (
ActionEvent,
EventKind,
FileEvent,
WebStreamEvent,
event_isoparse,
)
from libretime_playout.player.schedule import (
generate_file_events,
generate_live_events,
@ -268,110 +273,112 @@ SCHEDULE = [
def test_generate_live_events():
show_instance_3 = SHOW_INSTANCE_3.copy()
show_instance_3["starts_at"] = isoparse(show_instance_3["starts_at"])
show_instance_3["ends_at"] = isoparse(show_instance_3["ends_at"])
show_instance_3["starts_at"] = event_isoparse(show_instance_3["starts_at"])
show_instance_3["ends_at"] = event_isoparse(show_instance_3["ends_at"])
result = {}
generate_live_events(result, show_instance_3, 0.0)
assert result == {
"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": ActionEvent(
start=datetime(2022, 9, 5, 13, 0),
end=datetime(2022, 9, 5, 13, 0),
type=EventKind.ACTION,
event_type="kick_out",
),
}
result = {}
generate_live_events(result, show_instance_3, 2.0)
assert result == {
"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-12-59-58": ActionEvent(
start=datetime(2022, 9, 5, 12, 59, 58),
end=datetime(2022, 9, 5, 12, 59, 58),
type=EventKind.ACTION,
event_type="switch_off",
),
"2022-09-05-13-00-00": ActionEvent(
start=datetime(2022, 9, 5, 13, 0),
end=datetime(2022, 9, 5, 13, 0),
type=EventKind.ACTION,
event_type="kick_out",
),
}
def test_generate_file_events():
schedule_1 = SCHEDULE_1.copy()
schedule_1["starts_at"] = isoparse(schedule_1["starts_at"])
schedule_1["ends_at"] = isoparse(schedule_1["ends_at"])
schedule_1["starts_at"] = event_isoparse(schedule_1["starts_at"])
schedule_1["ends_at"] = event_isoparse(schedule_1["ends_at"])
result = {}
generate_file_events(result, schedule_1, FILE_2, SHOW_1)
assert result == {
"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": FileEvent(
start=datetime(2022, 9, 5, 11, 0),
end=datetime(2022, 9, 5, 11, 5, 2),
type=EventKind.FILE,
row_id=1,
uri=None,
id=2,
show_name="Show 1",
fade_in=500.0,
fade_out=500.0,
cue_in=13.7008,
cue_out=315.845,
track_title="My Friend the Forest",
artist_name="Nils Frahm",
mime="audio/flac",
replay_gain=11.46,
filesize=10000,
file_ready=False,
)
}
def test_generate_webstream_events():
schedule_5 = SCHEDULE_5.copy()
schedule_5["starts_at"] = isoparse(schedule_5["starts_at"])
schedule_5["ends_at"] = isoparse(schedule_5["ends_at"])
schedule_5["starts_at"] = event_isoparse(schedule_5["starts_at"])
schedule_5["ends_at"] = event_isoparse(schedule_5["ends_at"])
result = {}
generate_webstream_events(result, schedule_5, WEBSTREAM_1, SHOW_3)
assert result == {
"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-10-00": WebStreamEvent(
start=datetime(2022, 9, 5, 12, 9, 55),
end=datetime(2022, 9, 5, 12, 9, 55),
type=EventKind.WEB_STREAM_BUFFER_START,
row_id=5,
uri="http://stream.radio.org/main.ogg",
id=1,
show_name="Show 3",
),
"2022-09-05-12-10-00_0": WebStreamEvent(
start=datetime(2022, 9, 5, 12, 10),
end=datetime(2022, 9, 5, 12, 40),
type=EventKind.WEB_STREAM_OUTPUT_START,
row_id=5,
uri="http://stream.radio.org/main.ogg",
id=1,
show_name="Show 3",
),
"2022-09-05-12-40-00": WebStreamEvent(
start=datetime(2022, 9, 5, 12, 40),
end=datetime(2022, 9, 5, 12, 40),
type=EventKind.WEB_STREAM_BUFFER_END,
row_id=5,
uri="http://stream.radio.org/main.ogg",
id=1,
show_name="Show 3",
),
"2022-09-05-12-40-00_0": WebStreamEvent(
start=datetime(2022, 9, 5, 12, 40),
end=datetime(2022, 9, 5, 12, 40),
type=EventKind.WEB_STREAM_OUTPUT_END,
row_id=5,
uri="http://stream.radio.org/main.ogg",
id=1,
show_name="Show 3",
),
}
@ -412,229 +419,223 @@ 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) == {
"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-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,
},
"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,
},
"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,
},
"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",
},
"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",
},
"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,
},
"2022-09-05-11-00-00": FileEvent(
start=datetime(2022, 9, 5, 11, 0),
end=datetime(2022, 9, 5, 11, 5, 2),
type=EventKind.FILE,
row_id=1,
uri=None,
id=2,
show_name="Show 1",
fade_in=500.0,
fade_out=500.0,
cue_in=13.7008,
cue_out=315.845,
track_title="My Friend the Forest",
artist_name="Nils Frahm",
mime="audio/flac",
replay_gain=11.46,
filesize=10000,
file_ready=False,
),
"2022-09-05-11-05-02": FileEvent(
start=datetime(2022, 9, 5, 11, 5, 2),
end=datetime(2022, 9, 5, 11, 10),
type=EventKind.FILE,
row_id=2,
uri=None,
id=4,
show_name="Show 1",
fade_in=500.0,
fade_out=500.0,
cue_in=0.0,
cue_out=297.8558,
track_title="#2",
artist_name="Nils Frahm",
mime="audio/flac",
replay_gain=-1.65,
filesize=10000,
file_ready=False,
),
"2022-09-05-11-10-00": FileEvent(
start=datetime(2022, 9, 5, 11, 10),
end=datetime(2022, 9, 5, 12, 8, 59),
type=EventKind.FILE,
row_id=3,
uri=None,
id=5,
show_name="Show 2",
fade_in=500.0,
fade_out=500.0,
cue_in=0.0,
cue_out=3539.13,
track_title="Democracy Now! 2022-09-05 Monday",
artist_name="Democracy Now! Audio",
mime="audio/mp3",
replay_gain=-1.39,
filesize=10000,
file_ready=False,
),
"2022-09-05-12-08-59": FileEvent(
start=datetime(2022, 9, 5, 12, 8, 59),
end=datetime(2022, 9, 5, 12, 10),
type=EventKind.FILE,
row_id=4,
uri=None,
id=4,
show_name="Show 2",
fade_in=500.0,
fade_out=500.0,
cue_in=0.0,
cue_out=61.0,
track_title="#2",
artist_name="Nils Frahm",
mime="audio/flac",
replay_gain=-1.65,
filesize=10000,
file_ready=False,
),
"2022-09-05-12-10-00": WebStreamEvent(
start=datetime(2022, 9, 5, 12, 9, 55),
end=datetime(2022, 9, 5, 12, 9, 55),
type=EventKind.WEB_STREAM_BUFFER_START,
row_id=5,
uri="http://stream.radio.org/main.ogg",
id=1,
show_name="Show 3",
),
"2022-09-05-12-10-00_0": WebStreamEvent(
start=datetime(2022, 9, 5, 12, 10),
end=datetime(2022, 9, 5, 12, 40),
type=EventKind.WEB_STREAM_OUTPUT_START,
row_id=5,
uri="http://stream.radio.org/main.ogg",
id=1,
show_name="Show 3",
),
"2022-09-05-12-40-00": WebStreamEvent(
start=datetime(2022, 9, 5, 12, 40),
end=datetime(2022, 9, 5, 12, 40),
type=EventKind.WEB_STREAM_BUFFER_END,
row_id=5,
uri="http://stream.radio.org/main.ogg",
id=1,
show_name="Show 3",
),
"2022-09-05-12-40-00_0": WebStreamEvent(
start=datetime(2022, 9, 5, 12, 40),
end=datetime(2022, 9, 5, 12, 40),
type=EventKind.WEB_STREAM_OUTPUT_END,
row_id=5,
uri="http://stream.radio.org/main.ogg",
id=1,
show_name="Show 3",
),
"2022-09-05-12-40-00_1": FileEvent(
start=datetime(2022, 9, 5, 12, 40),
end=datetime(2022, 9, 5, 12, 53, 23),
type=EventKind.FILE,
row_id=6,
uri=None,
id=3,
show_name="Show 3",
fade_in=500.0,
fade_out=500.0,
cue_in=55.1211,
cue_out=858.4,
track_title="All Melody",
artist_name="Nils Frahm",
mime="audio/flac",
replay_gain=-2.13,
filesize=10000,
file_ready=False,
),
"2022-09-05-12-53-23": FileEvent(
start=datetime(2022, 9, 5, 12, 53, 23),
end=datetime(2022, 9, 5, 12, 58, 25),
type=EventKind.FILE,
row_id=7,
uri=None,
id=2,
show_name="Show 3",
fade_in=500.0,
fade_out=500.0,
cue_in=13.7008,
cue_out=315.845,
track_title="My Friend the Forest",
artist_name="Nils Frahm",
mime="audio/flac",
replay_gain=11.46,
filesize=10000,
file_ready=False,
),
"2022-09-05-12-58-25": FileEvent(
start=datetime(2022, 9, 5, 12, 58, 25),
end=datetime(2022, 9, 5, 13, 0),
type=EventKind.FILE,
row_id=8,
uri=None,
id=1,
show_name="Show 3",
fade_in=500.0,
fade_out=500.0,
cue_in=8.25245,
cue_out=95.0,
track_title="The Dane",
artist_name="Nils Frahm",
mime="audio/flac",
replay_gain=4.52,
filesize=10000,
file_ready=False,
),
"2022-09-05-12-59-58": ActionEvent(
start=datetime(2022, 9, 5, 12, 59, 58),
end=datetime(2022, 9, 5, 12, 59, 58),
type=EventKind.ACTION,
event_type="switch_off",
),
"2022-09-05-13-00-00": ActionEvent(
start=datetime(2022, 9, 5, 13, 0),
end=datetime(2022, 9, 5, 13, 0),
type=EventKind.ACTION,
event_type="kick_out",
),
"2022-09-05-13-00-00_0": FileEvent(
start=datetime(2022, 9, 5, 13, 0),
end=datetime(2022, 9, 5, 13, 5, 2),
type=EventKind.FILE,
row_id=9,
uri=None,
id=2,
show_name="Show 4",
fade_in=500.0,
fade_out=500.0,
cue_in=13.7008,
cue_out=315.845,
track_title="My Friend the Forest",
artist_name="Nils Frahm",
mime="audio/flac",
replay_gain=11.46,
filesize=10000,
file_ready=False,
),
"2022-09-05-13-05-02": FileEvent(
start=datetime(2022, 9, 5, 13, 5, 2),
end=datetime(2022, 9, 5, 13, 10),
type=EventKind.FILE,
row_id=10,
uri=None,
id=4,
show_name="Show 4",
fade_in=500.0,
fade_out=500.0,
cue_in=0.0,
cue_out=297.8558,
track_title="#2",
artist_name="Nils Frahm",
mime="audio/flac",
replay_gain=-1.65,
filesize=10000,
file_ready=False,
),
}