2021-06-03 15:20:39 +02:00
|
|
|
import json
|
|
|
|
import logging
|
2023-03-21 14:15:32 +01:00
|
|
|
from functools import wraps
|
|
|
|
from time import sleep
|
2021-06-03 15:20:39 +02:00
|
|
|
|
2023-03-21 14:15:32 +01:00
|
|
|
from requests.exceptions import RequestException
|
2010-11-19 00:00:13 +01:00
|
|
|
|
2023-03-21 14:15:32 +01:00
|
|
|
from ._client import AbstractApiClient, Response
|
2020-01-30 14:47:36 +01:00
|
|
|
|
2023-02-26 01:27:00 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2022-07-20 15:01:08 +02:00
|
|
|
|
2023-03-21 14:15:32 +01:00
|
|
|
def retry_decorator(max_retries: int = 5):
|
|
|
|
def retry_request(func):
|
|
|
|
@wraps(func)
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
retries = max_retries
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
except RequestException as exception:
|
|
|
|
logger.warning(exception)
|
2013-04-19 00:02:54 +02:00
|
|
|
|
2023-03-21 14:15:32 +01:00
|
|
|
retries -= 1
|
|
|
|
if retries <= 0:
|
|
|
|
break
|
2013-04-19 00:02:54 +02:00
|
|
|
|
2023-03-21 14:15:32 +01:00
|
|
|
sleep(2.0)
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
return retry_request
|
|
|
|
|
|
|
|
|
|
|
|
class BaseApiClient(AbstractApiClient):
|
|
|
|
def __init__(self, base_url: str, api_key: str):
|
|
|
|
super().__init__(base_url=base_url)
|
|
|
|
self.session.headers.update({"Authorization": f"Api-Key {api_key}"})
|
|
|
|
self.session.params.update({"format": "json"}) # type: ignore[union-attr]
|
|
|
|
|
|
|
|
def version(self, **kwargs) -> Response:
|
|
|
|
return self._request(
|
|
|
|
"GET",
|
|
|
|
"/api/version",
|
|
|
|
**kwargs,
|
|
|
|
)
|
|
|
|
|
|
|
|
def register_component(self, component: str, **kwargs) -> Response:
|
|
|
|
return self._request(
|
|
|
|
"GET",
|
|
|
|
"/api/register-component",
|
|
|
|
params={"component": component},
|
|
|
|
**kwargs,
|
|
|
|
)
|
|
|
|
|
|
|
|
def notify_media_item_start_play(self, media_id, **kwargs) -> Response:
|
|
|
|
return self._request(
|
|
|
|
"GET",
|
|
|
|
"/api/notify-media-item-start-play",
|
|
|
|
params={"media_id": media_id},
|
|
|
|
**kwargs,
|
|
|
|
)
|
|
|
|
|
|
|
|
def update_liquidsoap_status(self, msg, stream_id, boot_time, **kwargs) -> Response:
|
|
|
|
return self._request(
|
|
|
|
"POST",
|
|
|
|
"/api/update-liquidsoap-status",
|
|
|
|
params={"stream_id": stream_id, "boot_time": boot_time},
|
|
|
|
data={"msg_post": msg},
|
|
|
|
**kwargs,
|
|
|
|
)
|
|
|
|
|
|
|
|
def update_source_status(self, sourcename, status, **kwargs) -> Response:
|
|
|
|
return self._request(
|
|
|
|
"GET",
|
|
|
|
"/api/update-source-status",
|
|
|
|
params={"sourcename": sourcename, "status": status},
|
|
|
|
**kwargs,
|
|
|
|
)
|
|
|
|
|
|
|
|
def check_live_stream_auth(self, username, password, djtype, **kwargs) -> Response:
|
|
|
|
return self._request(
|
|
|
|
"GET",
|
|
|
|
"/api/check-live-stream-auth",
|
|
|
|
params={"username": username, "password": password, "djtype": djtype},
|
|
|
|
**kwargs,
|
|
|
|
)
|
|
|
|
|
|
|
|
def notify_webstream_data(self, media_id, data, **kwargs) -> Response:
|
|
|
|
return self._request(
|
|
|
|
"POST",
|
|
|
|
"/api/notify-webstream-data",
|
|
|
|
params={"media_id": media_id},
|
|
|
|
data={"data": data}, # Data is already a json formatted string
|
|
|
|
**kwargs,
|
2022-02-22 18:19:16 +01:00
|
|
|
)
|
2012-07-12 23:58:29 +02:00
|
|
|
|
2023-03-21 14:15:32 +01:00
|
|
|
def rabbitmq_do_push(self, **kwargs) -> Response:
|
|
|
|
return self._request(
|
|
|
|
"GET",
|
|
|
|
"/api/rabbitmq-do-push",
|
|
|
|
**kwargs,
|
|
|
|
)
|
|
|
|
|
|
|
|
def push_stream_stats(self, data, **kwargs) -> Response:
|
|
|
|
return self._request(
|
|
|
|
"POST",
|
|
|
|
"/api/push-stream-stats",
|
|
|
|
data={"data": json.dumps(data)},
|
|
|
|
**kwargs,
|
|
|
|
)
|
|
|
|
|
|
|
|
def update_stream_setting_table(self, data, **kwargs) -> Response:
|
|
|
|
return self._request(
|
|
|
|
"POST",
|
|
|
|
"/api/update-stream-setting-table",
|
|
|
|
data={"data": json.dumps(data)},
|
|
|
|
**kwargs,
|
|
|
|
)
|
|
|
|
|
|
|
|
def update_metadata_on_tunein(self, **kwargs) -> Response:
|
|
|
|
return self._request(
|
|
|
|
"GET",
|
|
|
|
"/api/update-metadata-on-tunein",
|
|
|
|
**kwargs,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class ApiClient:
|
|
|
|
def __init__(self, base_url: str, api_key: str):
|
|
|
|
self._base_client = BaseApiClient(base_url=base_url, api_key=api_key)
|
|
|
|
|
|
|
|
def version(self):
|
2020-01-30 14:47:36 +01:00
|
|
|
try:
|
2023-03-21 14:15:32 +01:00
|
|
|
resp = self._base_client.version()
|
|
|
|
payload = resp.json()
|
|
|
|
return payload["api_version"]
|
|
|
|
except RequestException:
|
2020-01-30 14:47:36 +01:00
|
|
|
return -1
|
2011-02-23 23:03:27 +01:00
|
|
|
|
2012-08-28 21:00:02 +02:00
|
|
|
def notify_liquidsoap_started(self):
|
2013-02-03 06:40:41 +01:00
|
|
|
try:
|
2023-03-21 14:15:32 +01:00
|
|
|
self._base_client.rabbitmq_do_push()
|
|
|
|
except RequestException:
|
|
|
|
pass
|
2012-08-28 21:00:02 +02:00
|
|
|
|
2012-08-30 18:02:26 +02:00
|
|
|
def notify_media_item_start_playing(self, media_id):
|
2022-08-09 17:11:16 +02:00
|
|
|
"""
|
|
|
|
This is a callback from liquidsoap, we use this to notify
|
2012-10-31 15:48:03 +01:00
|
|
|
about the currently playing *song*. We get passed a JSON string
|
2022-08-09 17:11:16 +02:00
|
|
|
which we handed to liquidsoap in get_liquidsoap_data().
|
|
|
|
"""
|
2013-02-03 06:40:41 +01:00
|
|
|
try:
|
2023-03-21 14:15:32 +01:00
|
|
|
return self._base_client.notify_media_item_start_play(media_id=media_id)
|
|
|
|
except RequestException:
|
2013-02-03 06:40:41 +01:00
|
|
|
return None
|
2011-05-16 21:33:31 +02:00
|
|
|
|
2012-03-02 22:55:11 +01:00
|
|
|
def check_live_stream_auth(self, username, password, dj_type):
|
2013-02-03 06:40:41 +01:00
|
|
|
try:
|
2023-03-21 14:15:32 +01:00
|
|
|
return self._base_client.check_live_stream_auth(
|
|
|
|
username=username,
|
|
|
|
password=password,
|
|
|
|
djtype=dj_type,
|
2021-05-27 16:23:02 +02:00
|
|
|
)
|
2023-03-21 14:15:32 +01:00
|
|
|
except RequestException:
|
2013-02-03 06:40:41 +01:00
|
|
|
return {}
|
2011-04-25 18:49:01 +02:00
|
|
|
|
2011-09-16 23:51:28 +02:00
|
|
|
def register_component(self, component):
|
2022-08-09 17:11:16 +02:00
|
|
|
"""
|
|
|
|
Purpose of this method is to contact the server with a "Hey its
|
2012-10-31 15:54:02 +01:00
|
|
|
me!" message. This will allow the server to register the component's
|
|
|
|
(component = media-monitor, pypo etc.) ip address, and later use it
|
|
|
|
to query monit via monit's http service, or download log files via a
|
2022-08-09 17:11:16 +02:00
|
|
|
http server.
|
|
|
|
"""
|
2023-03-21 14:15:32 +01:00
|
|
|
return self._base_client.register_component(component=component)
|
2012-07-12 23:58:29 +02:00
|
|
|
|
2023-03-21 14:15:32 +01:00
|
|
|
@retry_decorator()
|
2011-12-24 16:59:09 +01:00
|
|
|
def notify_liquidsoap_status(self, msg, stream_id, time):
|
2023-03-21 14:15:32 +01:00
|
|
|
self._base_client.update_liquidsoap_status(
|
|
|
|
msg=msg,
|
|
|
|
stream_id=stream_id,
|
|
|
|
boot_time=time,
|
|
|
|
)
|
2012-07-12 23:58:29 +02:00
|
|
|
|
2023-03-21 14:15:32 +01:00
|
|
|
@retry_decorator()
|
2012-03-08 23:42:38 +01:00
|
|
|
def notify_source_status(self, sourcename, status):
|
2023-03-21 14:15:32 +01:00
|
|
|
return self._base_client.update_source_status(
|
|
|
|
sourcename=sourcename,
|
|
|
|
status=status,
|
|
|
|
)
|
2012-07-12 23:58:29 +02:00
|
|
|
|
2023-03-21 14:15:32 +01:00
|
|
|
@retry_decorator()
|
2012-08-15 21:12:44 +02:00
|
|
|
def notify_webstream_data(self, data, media_id):
|
|
|
|
"""
|
|
|
|
Update the server with the latest metadata we've received from the
|
|
|
|
external webstream
|
|
|
|
"""
|
2023-03-21 14:15:32 +01:00
|
|
|
return self._base_client.notify_webstream_data(
|
|
|
|
data=data,
|
|
|
|
media_id=str(media_id),
|
2021-05-27 16:23:02 +02:00
|
|
|
)
|
2012-11-05 20:02:55 +01:00
|
|
|
|
2012-11-02 22:50:43 +01:00
|
|
|
def push_stream_stats(self, data):
|
2023-03-21 14:15:32 +01:00
|
|
|
return self._base_client.push_stream_stats(data=data)
|
2013-01-08 23:32:27 +01:00
|
|
|
|
|
|
|
def update_stream_setting_table(self, data):
|
2013-02-03 06:40:41 +01:00
|
|
|
try:
|
2023-03-21 14:15:32 +01:00
|
|
|
return self._base_client.update_stream_setting_table(data=data)
|
|
|
|
except RequestException:
|
|
|
|
return None
|
2013-04-27 00:27:40 +02:00
|
|
|
|
2015-05-25 21:37:45 +02:00
|
|
|
def update_metadata_on_tunein(self):
|
2023-03-21 14:15:32 +01:00
|
|
|
self._base_client.update_metadata_on_tunein()
|
2024-04-13 19:12:45 +02:00
|
|
|
|
|
|
|
def trigger_task_manager(self):
|
|
|
|
self._base_client.version()
|