cfr #121 Co-authored-by: Christophe Siraut <d@tobald.eu.org> Co-authored-by: bkfox <thomas bkfox net> Co-authored-by: Thomas Kairos <thomas@bkfox.net> Reviewed-on: #131 Co-authored-by: Chris Tactic <ctactic@noreply.git.radiocampus.be> Co-committed-by: Chris Tactic <ctactic@noreply.git.radiocampus.be>
99 lines
2.7 KiB
Python
Executable File
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
|