2021-06-03 15:20:39 +02:00
|
|
|
import json
|
|
|
|
import logging
|
2020-01-30 14:47:36 +01:00
|
|
|
import urllib.parse
|
2021-06-03 15:20:39 +02:00
|
|
|
|
2022-07-20 15:01:08 +02:00
|
|
|
from libretime_shared.config import BaseConfig, GeneralConfig
|
2010-11-19 00:00:13 +01:00
|
|
|
|
2023-03-21 13:38:45 +01:00
|
|
|
from ._utils import RequestProvider
|
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
|
|
|
|
|
|
|
class Config(BaseConfig):
|
|
|
|
general: GeneralConfig
|
|
|
|
|
|
|
|
|
2014-02-14 22:22:15 +01:00
|
|
|
AIRTIME_API_VERSION = "1.1"
|
2010-11-08 22:54:54 +01:00
|
|
|
|
2012-10-29 16:42:24 +01:00
|
|
|
|
2020-01-30 14:47:36 +01:00
|
|
|
api_endpoints = {}
|
2013-04-19 00:02:54 +02:00
|
|
|
|
|
|
|
# URL to get the version number of the server API
|
2021-05-27 16:23:02 +02:00
|
|
|
api_endpoints["version_url"] = "version/api_key/{api_key}"
|
|
|
|
# URL to register a components IP Address with the central web server
|
|
|
|
api_endpoints[
|
|
|
|
"register_component"
|
|
|
|
] = "register-component/format/json/api_key/{api_key}/component/{component}"
|
|
|
|
|
|
|
|
# pypo
|
|
|
|
api_endpoints[
|
|
|
|
"update_start_playing_url"
|
|
|
|
] = "notify-media-item-start-play/api_key/{api_key}/media_id/{media_id}/"
|
|
|
|
api_endpoints[
|
|
|
|
"update_liquidsoap_status"
|
|
|
|
] = "update-liquidsoap-status/format/json/api_key/{api_key}/msg/{msg}/stream_id/{stream_id}/boot_time/{boot_time}"
|
|
|
|
api_endpoints[
|
|
|
|
"update_source_status"
|
|
|
|
] = "update-source-status/format/json/api_key/{api_key}/sourcename/{sourcename}/status/{status}"
|
|
|
|
api_endpoints[
|
|
|
|
"check_live_stream_auth"
|
|
|
|
] = "check-live-stream-auth/format/json/api_key/{api_key}/username/{username}/password/{password}/djtype/{djtype}"
|
|
|
|
api_endpoints[
|
|
|
|
"notify_webstream_data"
|
|
|
|
] = "notify-webstream-data/api_key/{api_key}/media_id/{media_id}/format/json"
|
|
|
|
api_endpoints[
|
|
|
|
"notify_liquidsoap_started"
|
|
|
|
] = "rabbitmq-do-push/api_key/{api_key}/format/json"
|
|
|
|
api_endpoints["push_stream_stats"] = "push-stream-stats/api_key/{api_key}/format/json"
|
|
|
|
api_endpoints[
|
|
|
|
"update_stream_setting_table"
|
|
|
|
] = "update-stream-setting-table/api_key/{api_key}/format/json"
|
|
|
|
api_endpoints[
|
|
|
|
"update_metadata_on_tunein"
|
|
|
|
] = "update-metadata-on-tunein/api_key/{api_key}"
|
2013-04-19 00:02:54 +02:00
|
|
|
|
|
|
|
|
2022-07-22 13:27:16 +02:00
|
|
|
class ApiClient:
|
2022-02-22 18:19:16 +01:00
|
|
|
API_BASE = "/api"
|
|
|
|
UPLOAD_RETRIES = 3
|
|
|
|
UPLOAD_WAIT = 60
|
|
|
|
|
2023-02-26 01:27:00 +01:00
|
|
|
def __init__(self, config_path="/etc/libretime/config.yml"):
|
2022-08-12 15:12:39 +02:00
|
|
|
config = Config(config_path)
|
2022-02-22 18:19:16 +01:00
|
|
|
self.base_url = config.general.public_url
|
|
|
|
self.api_key = config.general.api_key
|
|
|
|
|
|
|
|
self.services = RequestProvider(
|
|
|
|
base_url=self.base_url + self.API_BASE,
|
|
|
|
api_key=self.api_key,
|
|
|
|
endpoints=api_endpoints,
|
|
|
|
)
|
2012-07-12 23:58:29 +02:00
|
|
|
|
2014-02-14 22:22:15 +01:00
|
|
|
def __get_api_version(self):
|
2020-01-30 14:47:36 +01:00
|
|
|
try:
|
2021-05-27 16:23:02 +02:00
|
|
|
return self.services.version_url()["api_version"]
|
2022-08-09 17:14:18 +02:00
|
|
|
except Exception as exception:
|
2023-02-26 01:27:00 +01:00
|
|
|
logger.exception(exception)
|
2020-01-30 14:47:36 +01:00
|
|
|
return -1
|
2011-02-23 23:03:27 +01:00
|
|
|
|
2012-07-12 23:58:29 +02:00
|
|
|
def is_server_compatible(self, verbose=True):
|
2014-02-14 22:22:15 +01:00
|
|
|
api_version = self.__get_api_version()
|
|
|
|
if api_version == -1:
|
|
|
|
if verbose:
|
2023-02-26 01:27:00 +01:00
|
|
|
logger.info("Unable to get Airtime API version number.\n")
|
2011-05-16 21:33:31 +02:00
|
|
|
return False
|
2022-08-09 17:16:48 +02:00
|
|
|
|
|
|
|
if api_version[0:3] != AIRTIME_API_VERSION[0:3]:
|
2013-02-04 22:05:58 +01:00
|
|
|
if verbose:
|
2023-02-26 12:01:59 +01:00
|
|
|
logger.info("Airtime API version found: %s", str(api_version))
|
2023-02-26 01:27:00 +01:00
|
|
|
logger.info(
|
2023-02-26 12:01:59 +01:00
|
|
|
"pypo is only compatible with API version: %s", AIRTIME_API_VERSION
|
2021-05-27 16:23:02 +02:00
|
|
|
)
|
2011-05-16 21:33:31 +02:00
|
|
|
return False
|
2022-08-09 17:16:48 +02:00
|
|
|
|
|
|
|
if verbose:
|
2023-02-26 12:01:59 +01:00
|
|
|
logger.info("Airtime API version found: %s", str(api_version))
|
2023-02-26 01:27:00 +01:00
|
|
|
logger.info(
|
2023-02-26 12:01:59 +01:00
|
|
|
"pypo is only compatible with API version: %s", AIRTIME_API_VERSION
|
2022-08-09 17:16:48 +02:00
|
|
|
)
|
|
|
|
return True
|
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:
|
|
|
|
self.services.notify_liquidsoap_started()
|
2022-08-09 17:14:18 +02:00
|
|
|
except Exception as exception:
|
2023-02-26 01:27:00 +01:00
|
|
|
logger.exception(exception)
|
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:
|
|
|
|
return self.services.update_start_playing_url(media_id=media_id)
|
2022-08-09 17:14:18 +02:00
|
|
|
except Exception as exception:
|
2023-02-26 01:27:00 +01:00
|
|
|
logger.exception(exception)
|
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:
|
|
|
|
return self.services.check_live_stream_auth(
|
2021-05-27 16:23:02 +02:00
|
|
|
username=username, password=password, djtype=dj_type
|
|
|
|
)
|
2022-08-09 17:14:18 +02:00
|
|
|
except Exception as exception:
|
2023-02-26 01:27:00 +01:00
|
|
|
logger.exception(exception)
|
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.
|
|
|
|
"""
|
2013-02-04 22:05:58 +01:00
|
|
|
return self.services.register_component(component=component)
|
2012-07-12 23:58:29 +02:00
|
|
|
|
2011-12-24 16:59:09 +01:00
|
|
|
def notify_liquidsoap_status(self, msg, stream_id, time):
|
2011-10-11 02:14:27 +02:00
|
|
|
try:
|
2021-05-27 16:23:02 +02:00
|
|
|
# encoded_msg is no longer used server_side!!
|
|
|
|
encoded_msg = urllib.parse.quote("dummy")
|
2021-09-13 21:42:37 +02:00
|
|
|
|
2021-05-27 16:23:02 +02:00
|
|
|
self.services.update_liquidsoap_status.req(
|
2021-09-13 21:42:37 +02:00
|
|
|
_post_data={"msg_post": msg},
|
|
|
|
msg=encoded_msg,
|
|
|
|
stream_id=stream_id,
|
|
|
|
boot_time=time,
|
2021-05-27 16:23:02 +02:00
|
|
|
).retry(5)
|
2022-08-09 17:14:18 +02:00
|
|
|
except Exception as exception:
|
2023-02-26 01:27:00 +01:00
|
|
|
logger.exception(exception)
|
2012-07-12 23:58:29 +02:00
|
|
|
|
2012-03-08 23:42:38 +01:00
|
|
|
def notify_source_status(self, sourcename, status):
|
|
|
|
try:
|
2021-05-27 16:23:02 +02:00
|
|
|
return self.services.update_source_status.req(
|
|
|
|
sourcename=sourcename, status=status
|
|
|
|
).retry(5)
|
2022-08-09 17:14:18 +02:00
|
|
|
except Exception as exception:
|
2023-02-26 01:27:00 +01:00
|
|
|
logger.exception(exception)
|
2012-07-12 23:58:29 +02:00
|
|
|
|
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-02-26 01:27:00 +01:00
|
|
|
logger.info(
|
2021-05-27 16:23:02 +02:00
|
|
|
self.services.notify_webstream_data.req(
|
|
|
|
_post_data={"data": data}, media_id=str(media_id)
|
|
|
|
).retry(5)
|
|
|
|
)
|
2012-11-05 20:02:55 +01:00
|
|
|
|
2012-11-02 22:50:43 +01:00
|
|
|
def push_stream_stats(self, data):
|
2012-11-07 23:20:12 +01:00
|
|
|
# TODO : users of this method should do their own error handling
|
2021-05-27 16:23:02 +02:00
|
|
|
response = self.services.push_stream_stats(
|
|
|
|
_post_data={"data": json.dumps(data)}
|
|
|
|
)
|
2013-02-04 22:05:58 +01:00
|
|
|
return response
|
2013-01-08 23:32:27 +01:00
|
|
|
|
|
|
|
def update_stream_setting_table(self, data):
|
2013-02-03 06:40:41 +01:00
|
|
|
try:
|
2021-05-27 16:23:02 +02:00
|
|
|
response = self.services.update_stream_setting_table(
|
|
|
|
_post_data={"data": json.dumps(data)}
|
|
|
|
)
|
2013-02-03 06:40:41 +01:00
|
|
|
return response
|
2022-08-09 17:14:18 +02:00
|
|
|
except Exception as exception:
|
2023-02-26 01:27:00 +01:00
|
|
|
logger.exception(exception)
|
2013-04-27 00:27:40 +02:00
|
|
|
|
2015-05-25 21:37:45 +02:00
|
|
|
def update_metadata_on_tunein(self):
|
|
|
|
self.services.update_metadata_on_tunein()
|