From 28857fbf49dcc66f70bac66ec2ef0ae31a58cff7 Mon Sep 17 00:00:00 2001 From: jo Date: Sun, 17 Jul 2022 12:54:14 +0200 Subject: [PATCH] refactor(playout): remove date_interval_to_seconds function Replace date_interval_to_seconds function with either timedelta.total_seconds() or seconds_between(). --- playout/libretime_playout/pure.py | 10 ------ playout/libretime_playout/pypofetch.py | 2 +- playout/libretime_playout/pypoliqqueue.py | 28 +++++------------ playout/libretime_playout/pypoliquidsoap.py | 35 ++++----------------- playout/libretime_playout/pypopush.py | 15 +-------- playout/libretime_playout/utils.py | 10 ++++++ 6 files changed, 26 insertions(+), 74 deletions(-) delete mode 100644 playout/libretime_playout/pure.py create mode 100644 playout/libretime_playout/utils.py diff --git a/playout/libretime_playout/pure.py b/playout/libretime_playout/pure.py deleted file mode 100644 index 0a2cfd942..000000000 --- a/playout/libretime_playout/pure.py +++ /dev/null @@ -1,10 +0,0 @@ -def date_interval_to_seconds(interval): - """ - Convert timedelta object into int representing the number of seconds. If - number of seconds is less than 0, then return 0. - """ - seconds = ( - interval.microseconds + (interval.seconds + interval.days * 24 * 3600) * 10**6 - ) / float(10**6) - - return seconds diff --git a/playout/libretime_playout/pypofetch.py b/playout/libretime_playout/pypofetch.py index 4581fec30..fdf088ee2 100644 --- a/playout/libretime_playout/pypofetch.py +++ b/playout/libretime_playout/pypofetch.py @@ -389,7 +389,7 @@ class PypoFetch(Thread): mimetypes.init(["%s/mime.types" % os.path.dirname(os.path.realpath(__file__))]) mime_ext = mimetypes.guess_extension(mime, strict=False) - length1 = pure.date_interval_to_seconds(end - start) + length1 = (end - start).total_seconds() length2 = media_item["cue_out"] - media_item["cue_in"] if abs(length2 - length1) > 1: diff --git a/playout/libretime_playout/pypoliqqueue.py b/playout/libretime_playout/pypoliqqueue.py index 55cdf3e90..7ece242a7 100644 --- a/playout/libretime_playout/pypoliqqueue.py +++ b/playout/libretime_playout/pypoliqqueue.py @@ -8,6 +8,8 @@ from threading import Thread from loguru import logger +from .utils import seconds_between + def keyboardInterruptHandler(signum, frame): logger.info("\nKeyboard Interrupt\n") @@ -45,11 +47,10 @@ class PypoLiqQueue(Thread): media_item = schedule_deque.popleft() self.pypo_liquidsoap.play(media_item) if len(schedule_deque): - time_until_next_play = self.date_interval_to_seconds( - schedule_deque[0]["start"] - datetime.utcnow() + time_until_next_play = seconds_between( + datetime.utcnow(), + schedule_deque[0]["start"], ) - if time_until_next_play < 0: - time_until_next_play = 0 else: time_until_next_play = None else: @@ -63,27 +64,14 @@ class PypoLiqQueue(Thread): schedule_deque.append(media_schedule[i]) if len(keys): - time_until_next_play = self.date_interval_to_seconds( - media_schedule[keys[0]]["start"] - datetime.utcnow() + time_until_next_play = seconds_between( + datetime.utcnow(), + media_schedule[keys[0]]["start"], ) else: time_until_next_play = None - def date_interval_to_seconds(self, interval): - """ - Convert timedelta object into int representing the number of seconds. If - number of seconds is less than 0, then return 0. - """ - seconds = ( - interval.microseconds - + (interval.seconds + interval.days * 24 * 3600) * 10**6 - ) / float(10**6) - if seconds < 0: - seconds = 0 - - return seconds - def run(self): try: self.main() diff --git a/playout/libretime_playout/pypoliquidsoap.py b/playout/libretime_playout/pypoliquidsoap.py index f43a569d3..ff0b39f22 100644 --- a/playout/libretime_playout/pypoliquidsoap.py +++ b/playout/libretime_playout/pypoliquidsoap.py @@ -5,6 +5,7 @@ from loguru import logger from .events import EventKind from .telnetliquidsoap import TelnetLiquidsoap +from .utils import seconds_between class PypoLiquidsoap: @@ -209,36 +210,12 @@ class PypoLiquidsoap: def modify_cue_point(self, link): assert self.is_file(link) - tnow = datetime.utcnow() + lateness = seconds_between(link["start"], datetime.utcnow()) - link_start = link["start"] - - diff_td = tnow - link_start - diff_sec = self.date_interval_to_seconds(diff_td) - - if diff_sec > 0: - logger.debug( - "media item was supposed to start %s ago. Preparing to start..", - diff_sec, - ) - original_cue_in_td = timedelta(seconds=float(link["cue_in"])) - link["cue_in"] = ( - self.date_interval_to_seconds(original_cue_in_td) + diff_sec - ) - - def date_interval_to_seconds(self, interval): - """ - Convert timedelta object into int representing the number of seconds. If - number of seconds is less than 0, then return 0. - """ - seconds = ( - interval.microseconds - + (interval.seconds + interval.days * 24 * 3600) * 10**6 - ) / float(10**6) - if seconds < 0: - seconds = 0 - - return seconds + if lateness > 0: + logger.debug(f"media item was supposed to start {lateness}s ago") + cue_in_orig = timedelta(seconds=float(link["cue_in"])) + link["cue_in"] = cue_in_orig.total_seconds() + lateness def clear_all_queues(self): self.telnet_liquidsoap.queue_clear_all() diff --git a/playout/libretime_playout/pypopush.py b/playout/libretime_playout/pypopush.py index 03075d490..9fa29d79c 100644 --- a/playout/libretime_playout/pypopush.py +++ b/playout/libretime_playout/pypopush.py @@ -85,8 +85,7 @@ class PypoPush(Thread): logger.debug(f"ignoring ended media_item: {media_item}") continue - diff_td = tnow - media_item["start"] - diff_sec = self.date_interval_to_seconds(diff_td) + diff_sec = (tnow - media_item["start"]).total_seconds() if diff_sec >= 0: logger.debug(f"adding media_item to present: {media_item}") @@ -97,18 +96,6 @@ class PypoPush(Thread): return present, future - def date_interval_to_seconds(self, interval): - """ - Convert timedelta object into int representing the number of seconds. If - number of seconds is less than 0, then return 0. - """ - seconds = ( - interval.microseconds - + (interval.seconds + interval.days * 24 * 3600) * 10**6 - ) / float(10**6) - - return seconds - @ls_timeout def stop_web_stream_all(self): try: diff --git a/playout/libretime_playout/utils.py b/playout/libretime_playout/utils.py new file mode 100644 index 000000000..8b67ce90b --- /dev/null +++ b/playout/libretime_playout/utils.py @@ -0,0 +1,10 @@ +from datetime import datetime + + +def seconds_between(base: datetime, target: datetime) -> float: + """ + Get seconds between base and target datetime. + + Return 0 if target is older than base. + """ + return max(0, (target - base).total_seconds())