Skip to content
Snippets Groups Projects
Commit 178db3ee authored by David Trattnig's avatar David Trattnig
Browse files

refact: expand items after fetch

parent 6668881f
No related branches found
No related tags found
1 merge request!35ORM-less scheduling
...@@ -186,8 +186,8 @@ class ApiFetcher(threading.Thread): ...@@ -186,8 +186,8 @@ class ApiFetcher(threading.Thread):
timeslot = Timeslot( timeslot = Timeslot(
id=api_ts.id, id=api_ts.id,
repetition_id=api_ts.repetition_of_id, repetition_id=api_ts.repetition_of_id,
start=api_ts.start, start=api_ts.start.timestamp(),
end=api_ts.end, end=api_ts.end.timestamp(),
show=show, show=show,
episode=episode, episode=episode,
) )
...@@ -196,6 +196,9 @@ class ApiFetcher(threading.Thread): ...@@ -196,6 +196,9 @@ class ApiFetcher(threading.Thread):
playlist_schedule = self.fetch_playlist(api_ts.schedule_default_playlist_id) playlist_schedule = self.fetch_playlist(api_ts.schedule_default_playlist_id)
playlist_show = self.fetch_playlist(api_ts.show_default_playlist_id) playlist_show = self.fetch_playlist(api_ts.show_default_playlist_id)
timeslot.set_playlists(playlist_timeslot, playlist_schedule, playlist_show) timeslot.set_playlists(playlist_timeslot, playlist_schedule, playlist_show)
self.expand_item_duration(playlist_timeslot)
self.expand_item_duration(playlist_schedule)
self.expand_item_duration(playlist_show)
timeslots.append(timeslot) timeslots.append(timeslot)
return timeslots return timeslots
...@@ -234,7 +237,13 @@ class ApiFetcher(threading.Thread): ...@@ -234,7 +237,13 @@ class ApiFetcher(threading.Thread):
title=entry.file.metadata.title, title=entry.file.metadata.title,
) )
item = PlaylistItem(entry.uri, entry.duration, 100, metadata) # Convert nanoseconds to seconds
# TODO remove after tank is using seconds for duration
duration: float = None
if entry.duration:
duration = SU.nano_to_seconds(entry.duration)
item = PlaylistItem(entry.uri, duration, 100, metadata)
# "Hidden Functionality" to feed engine with M3U playlists # "Hidden Functionality" to feed engine with M3U playlists
# via Tank's "Stream" playlist item type # via Tank's "Stream" playlist item type
...@@ -250,6 +259,45 @@ class ApiFetcher(threading.Thread): ...@@ -250,6 +259,45 @@ class ApiFetcher(threading.Thread):
return playlist return playlist
@private
def expand_item_duration(self, playlist: Playlist):
"""
Expand item to the timeslot gap left.
If some playlist item doesn't have a duration assigned, its duration is expanded to the
remaining duration of the playlist (= timeslot duration minus playlist items with
duration).
If there is more than one item without duration, such items are removed from the
playlist.
Args:
playlist (Playlist): the playlist with items to expand.
@private
"""
timeslot_duration: float = playlist.get_timeslot().get_duration()
playlist_duration: float = 0.0
items_wo_duration: list(PlaylistItem) = []
for item in playlist.get_items():
if not item.duration:
items_wo_duration.append(item)
else:
playlist_duration += item.duration
if len(items_wo_duration) == 1:
items_wo_duration[0].duration = timeslot_duration - playlist_duration
self.logger.info(f"Expand duration for playlist item #{items_wo_duration[0]}")
elif len(items_wo_duration) > 1:
# This case should actually never happen, as Tank does not allow more than one item w/o
# duration anymore
for item in reversed(items_wo_duration[1:-1]):
msg = f"Delete playlist item without duration: {item}"
self.logger.error(SU.red(msg))
item.remove()
@private @private
def is_valid_timeslot(self, timeslot: API_TIMESLOT) -> bool: def is_valid_timeslot(self, timeslot: API_TIMESLOT) -> bool:
""" """
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment