2023-02-26 01:27:00 +01:00
|
|
|
import logging
|
2011-03-21 00:34:43 +01:00
|
|
|
import math
|
2021-06-03 15:20:39 +02:00
|
|
|
import time
|
2022-01-18 21:12:31 +01:00
|
|
|
from datetime import datetime
|
|
|
|
from queue import Queue
|
2012-03-06 02:47:14 +01:00
|
|
|
from threading import Thread
|
2023-02-26 00:11:49 +01:00
|
|
|
from typing import List, Tuple
|
2012-03-16 04:14:19 +01:00
|
|
|
|
2022-07-18 15:11:47 +02:00
|
|
|
from ..config import PUSH_INTERVAL, Config
|
2023-03-04 21:50:12 +01:00
|
|
|
from .events import AnyEvent, Events, FileEvent
|
2023-03-23 12:30:37 +01:00
|
|
|
from .liquidsoap import Liquidsoap
|
2022-07-18 15:11:47 +02:00
|
|
|
from .queue import PypoLiqQueue
|
2011-03-03 06:22:28 +01:00
|
|
|
|
2023-02-26 01:27:00 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2011-03-03 06:22:28 +01:00
|
|
|
|
2011-03-21 00:34:43 +01:00
|
|
|
class PypoPush(Thread):
|
2022-08-14 19:55:39 +02:00
|
|
|
name = "push"
|
2022-09-08 17:55:51 +02:00
|
|
|
daemon = True
|
2022-08-14 19:55:39 +02:00
|
|
|
|
2022-08-21 11:28:57 +02:00
|
|
|
def __init__(
|
|
|
|
self,
|
2023-03-21 19:40:56 +01:00
|
|
|
push_queue: "Queue[Events]",
|
2023-03-23 12:30:37 +01:00
|
|
|
liquidsoap: Liquidsoap,
|
2022-08-21 11:28:57 +02:00
|
|
|
config: Config,
|
|
|
|
):
|
2011-03-21 00:34:43 +01:00
|
|
|
Thread.__init__(self)
|
2022-08-21 11:28:57 +02:00
|
|
|
self.queue = push_queue
|
2011-03-21 00:34:43 +01:00
|
|
|
|
2013-04-26 04:11:26 +02:00
|
|
|
self.config = config
|
2011-03-03 06:22:28 +01:00
|
|
|
|
2023-03-21 19:40:56 +01:00
|
|
|
self.future_scheduled_queue: "Queue[Events]" = Queue()
|
2023-03-23 12:30:37 +01:00
|
|
|
self.liquidsoap = liquidsoap
|
2013-03-22 17:16:17 +01:00
|
|
|
|
2023-03-23 12:30:37 +01:00
|
|
|
self.plq = PypoLiqQueue(self.future_scheduled_queue, self.liquidsoap)
|
2013-03-15 17:50:23 +01:00
|
|
|
self.plq.start()
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2023-03-01 20:27:27 +01:00
|
|
|
def main(self) -> None:
|
2012-03-21 23:26:16 +01:00
|
|
|
loops = 0
|
2012-06-27 04:41:11 +02:00
|
|
|
heartbeat_period = math.floor(30 / PUSH_INTERVAL)
|
|
|
|
|
2023-02-26 00:11:49 +01:00
|
|
|
events = None
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2012-03-20 20:51:54 +01:00
|
|
|
while True:
|
|
|
|
try:
|
2023-02-26 00:11:49 +01:00
|
|
|
events = self.queue.get(block=True)
|
2023-03-01 18:17:34 +01:00
|
|
|
except Exception as exception: # pylint: disable=broad-exception-caught
|
2022-08-09 21:05:21 +02:00
|
|
|
logger.exception(exception)
|
|
|
|
raise exception
|
2023-03-01 17:13:02 +01:00
|
|
|
|
|
|
|
logger.debug(events)
|
|
|
|
# separate media_schedule list into currently_playing and
|
|
|
|
# scheduled_for_future lists
|
|
|
|
currently_playing, scheduled_for_future = self.separate_present_future(
|
|
|
|
events
|
|
|
|
)
|
|
|
|
|
2023-03-23 12:30:37 +01:00
|
|
|
self.liquidsoap.verify_correct_present_media(currently_playing)
|
2023-03-01 17:13:02 +01:00
|
|
|
self.future_scheduled_queue.put(scheduled_for_future)
|
2013-03-14 21:50:55 +01:00
|
|
|
|
2012-03-21 23:26:16 +01:00
|
|
|
if loops % heartbeat_period == 0:
|
2022-01-13 16:11:37 +01:00
|
|
|
logger.info("heartbeat")
|
2012-03-21 23:26:16 +01:00
|
|
|
loops = 0
|
|
|
|
loops += 1
|
2012-03-20 20:51:54 +01:00
|
|
|
|
2023-02-26 00:11:49 +01:00
|
|
|
def separate_present_future(self, events: Events) -> Tuple[List[AnyEvent], Events]:
|
|
|
|
now = datetime.utcnow()
|
2013-03-14 21:50:55 +01:00
|
|
|
|
2023-02-26 00:11:49 +01:00
|
|
|
present: List[AnyEvent] = []
|
|
|
|
future: Events = {}
|
2013-03-14 21:50:55 +01:00
|
|
|
|
2023-02-26 00:11:49 +01:00
|
|
|
for key in sorted(events.keys()):
|
|
|
|
item = events[key]
|
2013-03-14 21:50:55 +01:00
|
|
|
|
2021-10-11 23:43:25 +02:00
|
|
|
# Ignore track that already ended
|
2023-03-04 21:50:12 +01:00
|
|
|
if isinstance(item, FileEvent) and item.end < now:
|
2023-02-26 00:11:49 +01:00
|
|
|
logger.debug("ignoring ended media_item: %s", item)
|
2021-10-11 23:43:25 +02:00
|
|
|
continue
|
|
|
|
|
2023-03-04 21:50:12 +01:00
|
|
|
diff_sec = (now - item.start).total_seconds()
|
2013-03-14 21:50:55 +01:00
|
|
|
|
|
|
|
if diff_sec >= 0:
|
2023-02-26 00:11:49 +01:00
|
|
|
logger.debug("adding media_item to present: %s", item)
|
|
|
|
present.append(item)
|
2013-03-14 21:50:55 +01:00
|
|
|
else:
|
2023-02-26 00:11:49 +01:00
|
|
|
logger.debug("adding media_item to future: %s", item)
|
|
|
|
future[key] = item
|
2013-03-14 21:50:55 +01:00
|
|
|
|
|
|
|
return present, future
|
|
|
|
|
2023-03-01 20:27:27 +01:00
|
|
|
def run(self) -> None:
|
2014-03-11 23:01:29 +01:00
|
|
|
while True:
|
2021-05-27 16:23:02 +02:00
|
|
|
try:
|
|
|
|
self.main()
|
2023-03-01 18:17:34 +01:00
|
|
|
except Exception as exception: # pylint: disable=broad-exception-caught
|
2022-08-09 21:05:21 +02:00
|
|
|
logger.exception(exception)
|
2014-03-11 23:01:29 +01:00
|
|
|
time.sleep(5)
|