aircox/aircox_streamer/controllers/metadata.py
Thomas Kairos a24318bc84 #137: Sound et EpisodeSound, dashboard UI improvements (into #121) (#138)
#137

Deployment: **Upgrade to Liquidsoap 2.4**: code has been adapted to work with liquidsoap 2.4

Co-authored-by: bkfox <thomas bkfox net>
Reviewed-on: #138
2024-04-05 18:45:15 +02:00

99 lines
2.7 KiB
Python
Executable File

import logging
import tzlocal
from django.utils import timezone as tz
from django.utils.translation import gettext_lazy as _
__all__ = (
"Metadata",
"Request",
)
local_tz = tzlocal.get_localzone()
logger = logging.getLogger("aircox")
# FIXME liquidsoap does not manage timezones -- we have to convert
# 'on_air' metadata we get from it into utc one in order to work
# correctly.
class Metadata:
"""Base class for handling request metadata."""
controller = None
"""Controller."""
rid = None
"""Request id."""
uri = None
"""Request uri."""
status = None
"""Current playing status."""
request_status = None
"""Requests' status."""
air_time = None
"""Launch datetime."""
def __init__(self, controller=None, rid=None, data=None):
self.controller = controller
self.rid = rid
if data is not None:
self.validate(data)
@property
def is_playing(self):
"""True if the source is playing."""
# FIXME: validate on controller's current source?
return self.status == "playing"
@property
def status_verbose(self):
"""Verbose version of self's status (translated string)."""
status = self.validate_status(self.status)
return _(status) if status else ""
def fetch(self):
data = self.controller.send("request.metadata ", self.rid, parse=True)
if data:
self.validate(data)
def validate_status(self, status):
"""Return correct status for this metadata based on provided one and
controller.
:returns: status string
"""
on_air = self.controller.source
if "playing" and on_air and (on_air == self or on_air.rid == self.rid):
return "playing"
elif status in ("paused", "playing"):
return "paused"
else:
return "stopped"
def validate_air_time(self, air_time):
if air_time:
air_time = tz.datetime.strptime(air_time, "%Y/%m/%d %H:%M:%S")
return local_tz.localize(air_time)
def validate(self, data, as_dict=False):
"""Validate provided data and set as attribute (must already be
declared)"""
if as_dict and isinstance(data, list):
data = {v[0]: v[1] for v in data}
for key, value in data.items():
if hasattr(self, key) and not callable(getattr(self, key)):
setattr(self, key, value)
self.uri = data.get("initial_uri")
self.air_time = self.validate_air_time(data.get("on_air"))
self.status = self.validate_status(data.get("status"))
self.request_status = data.get("status")
class Request(Metadata):
title = None
artist = None