2020-01-30 14:47:36 +01:00
|
|
|
###############################################################################
|
|
|
|
# This file holds the implementations for all the API clients.
|
|
|
|
#
|
|
|
|
# If you want to develop a new client, here are some suggestions: Get the fetch
|
|
|
|
# methods working first, then the push, then the liquidsoap notifier. You will
|
|
|
|
# probably want to create a script on your server side to automatically
|
|
|
|
# schedule a playlist one minute from the current time.
|
|
|
|
###############################################################################
|
|
|
|
import logging
|
2021-08-18 16:23:05 +02:00
|
|
|
from datetime import datetime, timedelta
|
2021-06-03 15:20:39 +02:00
|
|
|
|
|
|
|
from dateutil.parser import isoparse
|
|
|
|
|
2022-02-22 18:19:16 +01:00
|
|
|
from ._config import Config
|
2021-06-03 15:20:39 +02:00
|
|
|
from .utils import RequestProvider, fromisoformat, time_in_milliseconds, time_in_seconds
|
2020-01-30 14:47:36 +01:00
|
|
|
|
|
|
|
LIBRETIME_API_VERSION = "2.0"
|
|
|
|
|
|
|
|
api_endpoints = {}
|
|
|
|
|
2021-05-27 16:23:02 +02:00
|
|
|
api_endpoints["version_url"] = "version/"
|
|
|
|
api_endpoints["schedule_url"] = "schedule/"
|
|
|
|
api_endpoints["webstream_url"] = "webstreams/{id}/"
|
|
|
|
api_endpoints["show_instance_url"] = "show-instances/{id}/"
|
|
|
|
api_endpoints["show_url"] = "shows/{id}/"
|
|
|
|
api_endpoints["file_url"] = "files/{id}/"
|
|
|
|
api_endpoints["file_download_url"] = "files/{id}/download/"
|
|
|
|
|
2020-01-30 14:47:36 +01:00
|
|
|
|
|
|
|
class AirtimeApiClient:
|
2022-02-22 18:19:16 +01:00
|
|
|
API_BASE = "/api/v2"
|
|
|
|
|
2021-05-27 16:23:02 +02:00
|
|
|
def __init__(self, logger=None, config_path="/etc/airtime/airtime.conf"):
|
2022-02-22 18:19:16 +01:00
|
|
|
self.logger = logger or logging
|
|
|
|
|
|
|
|
config = Config(filepath=config_path)
|
|
|
|
self.base_url = config.general.public_url
|
|
|
|
self.api_key = config.general.api_key
|
2020-01-30 14:47:36 +01:00
|
|
|
|
2022-02-22 18:19:16 +01:00
|
|
|
self.services = RequestProvider(
|
|
|
|
base_url=self.base_url + self.API_BASE,
|
|
|
|
api_key=self.api_key,
|
|
|
|
endpoints=api_endpoints,
|
|
|
|
)
|
2020-01-30 14:47:36 +01:00
|
|
|
|
|
|
|
def get_schedule(self):
|
2021-08-18 16:23:05 +02:00
|
|
|
current_time = datetime.utcnow()
|
|
|
|
end_time = current_time + timedelta(days=1)
|
2020-01-30 14:47:36 +01:00
|
|
|
|
2021-05-27 16:23:02 +02:00
|
|
|
str_current = current_time.isoformat(timespec="seconds")
|
|
|
|
str_end = end_time.isoformat(timespec="seconds")
|
|
|
|
data = self.services.schedule_url(
|
|
|
|
params={
|
2022-01-25 23:45:00 +01:00
|
|
|
"ends__range": (f"{str_current}Z,{str_end}Z"),
|
2021-09-21 11:36:49 +02:00
|
|
|
"is_valid": True,
|
2021-10-01 15:15:07 +02:00
|
|
|
"playout_status__gt": 0,
|
2021-05-27 16:23:02 +02:00
|
|
|
}
|
|
|
|
)
|
2022-02-02 15:57:45 +01:00
|
|
|
result = {}
|
2020-01-30 14:47:36 +01:00
|
|
|
for item in data:
|
2021-05-27 16:23:02 +02:00
|
|
|
start = isoparse(item["starts"])
|
2022-02-02 14:12:18 +01:00
|
|
|
start_key = start.strftime("%Y-%m-%d-%H-%M-%S")
|
2021-05-27 16:23:02 +02:00
|
|
|
end = isoparse(item["ends"])
|
2022-02-02 14:12:18 +01:00
|
|
|
end_key = end.strftime("%Y-%m-%d-%H-%M-%S")
|
2020-01-30 14:47:36 +01:00
|
|
|
|
2021-05-27 16:23:02 +02:00
|
|
|
show_instance = self.services.show_instance_url(id=item["instance_id"])
|
|
|
|
show = self.services.show_url(id=show_instance["show_id"])
|
2020-01-30 14:47:36 +01:00
|
|
|
|
2022-02-02 15:57:45 +01:00
|
|
|
result[start_key] = {
|
2022-02-02 14:12:18 +01:00
|
|
|
"start": start_key,
|
|
|
|
"end": end_key,
|
2021-05-27 16:23:02 +02:00
|
|
|
"row_id": item["id"],
|
2021-06-30 18:41:14 +02:00
|
|
|
"show_name": show["name"],
|
2020-01-30 14:47:36 +01:00
|
|
|
}
|
2022-02-02 15:57:45 +01:00
|
|
|
current = result[start_key]
|
2021-05-27 16:23:02 +02:00
|
|
|
if item["file"]:
|
|
|
|
current["independent_event"] = False
|
|
|
|
current["type"] = "file"
|
|
|
|
current["id"] = item["file_id"]
|
|
|
|
|
|
|
|
fade_in = time_in_milliseconds(fromisoformat(item["fade_in"]))
|
|
|
|
fade_out = time_in_milliseconds(fromisoformat(item["fade_out"]))
|
|
|
|
|
|
|
|
cue_in = time_in_seconds(fromisoformat(item["cue_in"]))
|
|
|
|
cue_out = time_in_seconds(fromisoformat(item["cue_out"]))
|
|
|
|
|
|
|
|
current["fade_in"] = fade_in
|
|
|
|
current["fade_out"] = fade_out
|
|
|
|
current["cue_in"] = cue_in
|
|
|
|
current["cue_out"] = cue_out
|
|
|
|
|
|
|
|
info = self.services.file_url(id=item["file_id"])
|
|
|
|
current["metadata"] = info
|
|
|
|
current["uri"] = item["file"]
|
2021-06-30 18:41:14 +02:00
|
|
|
current["replay_gain"] = info["replay_gain"]
|
2021-05-27 16:23:02 +02:00
|
|
|
current["filesize"] = info["filesize"]
|
|
|
|
elif item["stream"]:
|
|
|
|
current["independent_event"] = True
|
|
|
|
current["id"] = item["stream_id"]
|
|
|
|
info = self.services.webstream_url(id=item["stream_id"])
|
|
|
|
current["uri"] = info["url"]
|
|
|
|
current["type"] = "stream_buffer_start"
|
2020-01-30 14:47:36 +01:00
|
|
|
# Stream events are instantaneous
|
2021-05-27 16:23:02 +02:00
|
|
|
current["end"] = current["start"]
|
|
|
|
|
2022-02-02 14:12:18 +01:00
|
|
|
result[f"{start_key}_0"] = {
|
2021-05-27 16:23:02 +02:00
|
|
|
"id": current["id"],
|
|
|
|
"type": "stream_output_start",
|
|
|
|
"start": current["start"],
|
|
|
|
"end": current["start"],
|
|
|
|
"uri": current["uri"],
|
|
|
|
"row_id": current["row_id"],
|
|
|
|
"independent_event": current["independent_event"],
|
2020-01-30 14:47:36 +01:00
|
|
|
}
|
|
|
|
|
2022-02-02 14:12:18 +01:00
|
|
|
result[end_key] = {
|
2021-05-27 16:23:02 +02:00
|
|
|
"type": "stream_buffer_end",
|
|
|
|
"start": current["end"],
|
|
|
|
"end": current["end"],
|
|
|
|
"uri": current["uri"],
|
|
|
|
"row_id": current["row_id"],
|
|
|
|
"independent_event": current["independent_event"],
|
2020-01-30 14:47:36 +01:00
|
|
|
}
|
|
|
|
|
2022-02-02 14:12:18 +01:00
|
|
|
result[f"{end_key}_0"] = {
|
2021-05-27 16:23:02 +02:00
|
|
|
"type": "stream_output_end",
|
|
|
|
"start": current["end"],
|
|
|
|
"end": current["end"],
|
|
|
|
"uri": current["uri"],
|
|
|
|
"row_id": current["row_id"],
|
|
|
|
"independent_event": current["independent_event"],
|
2020-01-30 14:47:36 +01:00
|
|
|
}
|
2022-02-02 15:57:45 +01:00
|
|
|
|
|
|
|
return {"media": result}
|
2021-08-20 13:35:33 +02:00
|
|
|
|
|
|
|
def update_file(self, file_id, payload):
|
|
|
|
data = self.services.file_url(id=file_id)
|
|
|
|
data.update(payload)
|
|
|
|
return self.services.file_url(id=file_id, _put_data=data)
|