sintonia/api-client/libretime_api_client/v1.py

304 lines
11 KiB
Python
Raw Normal View History

###############################################################################
# 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 json
import logging
import time
import traceback
2020-01-30 14:47:36 +01:00
import urllib.parse
import requests
from libretime_shared.config import BaseConfig, GeneralConfig
2022-07-22 12:49:21 +02:00
from ._utils import ApiRequest, RequestProvider
2020-01-30 14:47:36 +01:00
class Config(BaseConfig):
general: GeneralConfig
AIRTIME_API_VERSION = "1.1"
2012-10-29 16:42:24 +01:00
2020-01-30 14:47:36 +01:00
api_endpoints = {}
# 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}"
# media-monitor
api_endpoints[
"upload_recorded"
] = "upload-recorded/format/json/api_key/{api_key}/fileid/{fileid}/showinstanceid/{showinstanceid}"
# show-recorder
api_endpoints["show_schedule_url"] = "recorded-shows/format/json/api_key/{api_key}"
api_endpoints["upload_file_url"] = "rest/media"
# pypo
api_endpoints[
"update_start_playing_url"
] = "notify-media-item-start-play/api_key/{api_key}/media_id/{media_id}/"
api_endpoints[
"get_stream_setting"
] = "get-stream-setting/format/json/api_key/{api_key}/"
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["get_bootstrap_info"] = "get-bootstrap-info/format/json/api_key/{api_key}"
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[
"get_stream_parameters"
] = "get-stream-parameters/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}"
################################################################################
2020-01-30 14:47:36 +01:00
# Airtime API Version 1 Client
################################################################################
class ApiClient:
API_BASE = "/api"
UPLOAD_RETRIES = 3
UPLOAD_WAIT = 60
def __init__(self, logger=None, config_path="/etc/libretime/config.yml"):
self.logger = logger or logging
2012-07-12 22:51:11 +02:00
config = Config(filepath=config_path)
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,
)
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"]
2020-01-30 14:47:36 +01:00
except Exception as e:
self.logger.exception(e)
return -1
def is_server_compatible(self, verbose=True):
logger = self.logger
api_version = self.__get_api_version()
2012-11-07 17:43:49 +01:00
# logger.info('Airtime version found: ' + str(version))
if api_version == -1:
if verbose:
2021-05-27 16:23:02 +02:00
logger.info("Unable to get Airtime API version number.\n")
return False
elif api_version[0:3] != AIRTIME_API_VERSION[0:3]:
if verbose:
2021-05-27 16:23:02 +02:00
logger.info("Airtime API version found: " + str(api_version))
logger.info(
"pypo is only compatible with API version: " + AIRTIME_API_VERSION
)
return False
else:
if verbose:
2021-05-27 16:23:02 +02:00
logger.info("Airtime API version found: " + str(api_version))
logger.info(
"pypo is only compatible with API version: " + AIRTIME_API_VERSION
)
return True
def notify_liquidsoap_started(self):
try:
self.services.notify_liquidsoap_started()
2020-01-16 15:32:51 +01:00
except Exception as e:
2020-01-23 11:37:49 +01:00
self.logger.exception(e)
def notify_media_item_start_playing(self, media_id):
2021-05-27 16:23:02 +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
2021-05-27 16:23:02 +02:00
which we handed to liquidsoap in get_liquidsoap_data()."""
try:
return self.services.update_start_playing_url(media_id=media_id)
2020-01-16 15:32:51 +01:00
except Exception as e:
2020-01-23 11:37:49 +01:00
self.logger.exception(e)
return None
def get_shows_to_record(self):
try:
return self.services.show_schedule_url()
2020-01-16 15:32:51 +01:00
except Exception as e:
2020-01-23 11:37:49 +01:00
self.logger.exception(e)
return None
def upload_recorded_show(self, files, show_id):
logger = self.logger
2021-05-27 16:23:02 +02:00
response = ""
retries = self.UPLOAD_RETRIES
retries_wait = self.UPLOAD_WAIT
url = self.construct_rest_url("upload_file_url")
logger.debug(url)
for i in range(0, retries):
logger.debug("Upload attempt: %s", i + 1)
logger.debug(files)
logger.debug(ApiRequest.API_HTTP_REQUEST_TIMEOUT)
try:
2021-05-27 16:23:02 +02:00
request = requests.post(
url, files=files, timeout=float(ApiRequest.API_HTTP_REQUEST_TIMEOUT)
)
response = request.json()
logger.debug(response)
"""
FIXME: We need to tell LibreTime that the uploaded track was recorded for a specific show
My issue here is that response does not yet have an id. The id gets generated at the point
where analyzer is done with it's work. We probably need to do what is below in analyzer
and also make sure that the show instance id is routed all the way through.
It already gets uploaded by this but the RestController does not seem to care about it. In
the end analyzer doesn't have the info in it's rabbitmq message and imports the show as a
regular track.
logger.info("uploaded show result as file id %s", response.id)
url = self.construct_url("upload_recorded")
url = url.replace('%%fileid%%', response.id)
url = url.replace('%%showinstanceid%%', show_id)
request.get(url)
logger.info("associated uploaded file %s with show instance %s", response.id, show_id)
"""
break
except requests.exceptions.HTTPError as exception:
logger.error(f"Http error code: {exception.response.status_code}")
logger.error("traceback: %s", traceback.format_exc())
2020-01-16 15:32:51 +01:00
except requests.exceptions.ConnectionError as e:
logger.error("Server is down: %s", e.args)
logger.error("traceback: %s", traceback.format_exc())
2020-01-16 15:32:51 +01:00
except Exception as e:
2020-01-23 11:37:49 +01:00
self.logger.exception(e)
2021-05-27 16:23:02 +02:00
# wait some time before next retry
time.sleep(retries_wait)
return response
def check_live_stream_auth(self, username, password, dj_type):
try:
return self.services.check_live_stream_auth(
2021-05-27 16:23:02 +02:00
username=username, password=password, djtype=dj_type
)
2020-01-16 15:32:51 +01:00
except Exception as e:
2020-01-23 11:37:49 +01:00
self.logger.exception(e)
return {}
def construct_rest_url(self, action_key):
"""
Constructs the base url for RESTful requests
"""
url = urllib.parse.urlsplit(self.base_url)
url.username = self.api_key
return f"{url.geturl()}/{api_endpoints[action_key]}"
def get_stream_setting(self):
return self.services.get_stream_setting()
def register_component(self, component):
2021-05-27 16:23:02 +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
2021-05-27 16:23:02 +02:00
http server."""
return self.services.register_component(component=component)
def notify_liquidsoap_status(self, msg, stream_id, time):
logger = self.logger
try:
2021-05-27 16:23:02 +02:00
# encoded_msg is no longer used server_side!!
encoded_msg = urllib.parse.quote("dummy")
2021-05-27 16:23:02 +02:00
self.services.update_liquidsoap_status.req(
_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)
2020-01-16 15:32:51 +01:00
except Exception as e:
2020-01-23 11:37:49 +01:00
self.logger.exception(e)
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)
2020-01-16 15:32:51 +01:00
except Exception as e:
2020-01-23 11:37:49 +01:00
self.logger.exception(e)
def get_bootstrap_info(self):
2021-05-27 16:23:02 +02:00
"""Retrieve infomations needed on bootstrap time"""
return self.services.get_bootstrap_info()
def notify_webstream_data(self, data, media_id):
"""
Update the server with the latest metadata we've received from the
external webstream
"""
2021-05-27 16:23:02 +02:00
self.logger.info(
self.services.notify_webstream_data.req(
_post_data={"data": data}, media_id=str(media_id)
).retry(5)
)
def get_stream_parameters(self):
response = self.services.get_stream_parameters()
self.logger.debug(response)
return response
def push_stream_stats(self, data):
# 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)}
)
return response
def update_stream_setting_table(self, data):
try:
2021-05-27 16:23:02 +02:00
response = self.services.update_stream_setting_table(
_post_data={"data": json.dumps(data)}
)
return response
2020-01-16 15:32:51 +01:00
except Exception as e:
2020-01-23 11:37:49 +01:00
self.logger.exception(e)
def update_metadata_on_tunein(self):
self.services.update_metadata_on_tunein()
class InvalidContentType(Exception):
pass