2010-11-05 15:54:15 +01:00
|
|
|
"""
|
|
|
|
Python part of radio playout (pypo)
|
|
|
|
|
2010-11-30 00:34:22 +01:00
|
|
|
This function acts as a gateway between liquidsoap and the server API.
|
|
|
|
Mainly used to tell the platform what pypo/liquidsoap does.
|
2010-11-05 15:54:15 +01:00
|
|
|
|
2012-08-15 21:12:44 +02:00
|
|
|
Main case:
|
2010-11-05 15:54:15 +01:00
|
|
|
- whenever LS starts playing a new track, its on_metadata callback calls
|
2010-11-30 00:34:22 +01:00
|
|
|
a function in ls (notify(m)) which then calls the python script here
|
2012-08-15 21:12:44 +02:00
|
|
|
with the currently starting filename as parameter
|
2010-11-05 15:54:15 +01:00
|
|
|
- this python script takes this parameter, tries to extract the actual
|
2010-11-30 00:34:22 +01:00
|
|
|
media id from it, and then calls back to the API to tell about it about it.
|
2010-11-05 15:54:15 +01:00
|
|
|
|
|
|
|
"""
|
2024-02-02 20:24:25 +01:00
|
|
|
|
2023-02-26 01:27:00 +01:00
|
|
|
import logging
|
2022-01-13 16:11:37 +01:00
|
|
|
from pathlib import Path
|
2023-03-21 14:50:44 +01:00
|
|
|
from typing import Literal, Optional
|
2010-11-05 15:54:15 +01:00
|
|
|
|
2022-01-17 10:51:32 +01:00
|
|
|
import click
|
2022-07-22 16:26:43 +02:00
|
|
|
from libretime_api_client.v1 import ApiClient as LegacyClient
|
2023-02-25 20:57:03 +01:00
|
|
|
from libretime_shared.cli import cli_config_options, cli_logging_options
|
2022-05-05 09:41:32 +02:00
|
|
|
from libretime_shared.config import DEFAULT_ENV_PREFIX
|
2023-02-26 01:27:00 +01:00
|
|
|
from libretime_shared.logging import setup_logger
|
|
|
|
|
2023-02-25 20:57:03 +01:00
|
|
|
from ..config import Config
|
2022-01-17 10:51:32 +01:00
|
|
|
|
2023-03-01 17:13:02 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2023-02-25 20:57:03 +01:00
|
|
|
|
2023-03-01 17:13:02 +01:00
|
|
|
# pylint: disable=too-few-public-methods
|
2023-02-25 20:57:03 +01:00
|
|
|
class App:
|
|
|
|
config: Config
|
|
|
|
api_client: LegacyClient
|
|
|
|
|
|
|
|
def __init__(self, config: Config) -> None:
|
|
|
|
self.config = config
|
2023-03-21 14:16:29 +01:00
|
|
|
self.api_client = LegacyClient(
|
|
|
|
base_url=config.general.public_url,
|
|
|
|
api_key=config.general.api_key,
|
|
|
|
)
|
2023-02-25 20:57:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
pass_app = click.make_pass_decorator(App)
|
2022-01-17 10:51:32 +01:00
|
|
|
|
|
|
|
|
2022-05-05 09:41:32 +02:00
|
|
|
@click.group(context_settings={"auto_envvar_prefix": DEFAULT_ENV_PREFIX})
|
2022-01-20 06:30:35 +01:00
|
|
|
@cli_logging_options()
|
2023-02-25 20:57:03 +01:00
|
|
|
@cli_config_options()
|
|
|
|
@click.pass_context
|
|
|
|
def cli(
|
|
|
|
ctx: click.Context,
|
|
|
|
log_level: str,
|
|
|
|
log_filepath: Optional[Path],
|
|
|
|
config_filepath: Optional[Path],
|
|
|
|
):
|
2022-01-17 10:51:32 +01:00
|
|
|
"""
|
|
|
|
A gateway between Liquidsoap and the API.
|
|
|
|
"""
|
2023-02-26 01:27:00 +01:00
|
|
|
setup_logger(log_level, log_filepath, rotate=False)
|
2023-02-25 20:57:03 +01:00
|
|
|
ctx.obj = App(config=Config(config_filepath))
|
2022-01-17 10:51:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
@cli.command()
|
|
|
|
@click.argument("media_id")
|
2023-02-25 20:57:03 +01:00
|
|
|
@pass_app
|
|
|
|
def media(app: App, media_id):
|
2022-01-17 10:51:32 +01:00
|
|
|
"""
|
|
|
|
Notify currently playing media.
|
|
|
|
|
|
|
|
Replaces: notify --media-id=#{m['schedule_table_id']}
|
|
|
|
"""
|
2023-02-26 12:01:59 +01:00
|
|
|
logger.info("Sending currently playing media id '%s'", media_id)
|
2023-02-25 20:57:03 +01:00
|
|
|
app.api_client.notify_media_item_start_playing(media_id)
|
2022-01-17 10:51:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
@cli.command()
|
|
|
|
@click.argument("media_id")
|
|
|
|
@click.argument("data")
|
2023-02-25 20:57:03 +01:00
|
|
|
@pass_app
|
|
|
|
def webstream(app: App, media_id, data):
|
2022-01-17 10:51:32 +01:00
|
|
|
"""
|
|
|
|
Notify currently playing webstream.
|
|
|
|
|
|
|
|
Replaces: notify --webstream='#{json_str}' --media-id=#{!current_dyn_id}
|
|
|
|
"""
|
2023-02-26 12:01:59 +01:00
|
|
|
logger.info("Sending currently playing webstream '%s' data '%s'", media_id, data)
|
2023-02-25 20:57:03 +01:00
|
|
|
app.api_client.notify_webstream_data(data, media_id)
|
2022-01-17 10:51:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
@cli.command()
|
|
|
|
@click.argument("name")
|
|
|
|
@click.argument("status")
|
2023-02-25 20:57:03 +01:00
|
|
|
@pass_app
|
|
|
|
def live(app: App, name, status):
|
2022-01-17 10:51:32 +01:00
|
|
|
"""
|
|
|
|
Notify currently playing live input.
|
|
|
|
|
|
|
|
Replaces: notify --source-name=#{sourcename} --source-status=#{status}
|
|
|
|
"""
|
2023-02-26 12:01:59 +01:00
|
|
|
logger.info("Sending currently playing live source '%s' status '%s'", name, status)
|
2023-02-25 20:57:03 +01:00
|
|
|
app.api_client.notify_source_status(name, status)
|
2022-01-17 10:51:32 +01:00
|
|
|
|
|
|
|
|
2023-03-21 14:50:44 +01:00
|
|
|
@cli.command()
|
|
|
|
@click.argument("input_name", type=click.Choice(["main", "show"]))
|
|
|
|
@click.argument("username")
|
|
|
|
@click.argument("password")
|
|
|
|
@pass_app
|
|
|
|
def live_auth(
|
|
|
|
app: App,
|
|
|
|
input_name: Literal["main", "show"],
|
|
|
|
username: str,
|
|
|
|
password: str,
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
Check live stream user authentication.
|
|
|
|
"""
|
|
|
|
input_name_map = {"main": "master", "show": "dj"}
|
|
|
|
|
|
|
|
logger.info(
|
|
|
|
"Checking '%s' live stream user authentication '%s'",
|
|
|
|
input_name,
|
|
|
|
username,
|
|
|
|
)
|
|
|
|
resp = app.api_client.check_live_stream_auth(
|
|
|
|
username,
|
|
|
|
password,
|
|
|
|
input_name_map[input_name],
|
|
|
|
)
|
|
|
|
|
|
|
|
payload: dict = resp.json()
|
|
|
|
if payload.get("msg", False) is True:
|
|
|
|
raise SystemExit(0)
|
|
|
|
raise SystemExit(1)
|
|
|
|
|
|
|
|
|
2022-01-17 10:51:32 +01:00
|
|
|
@cli.command()
|
|
|
|
@click.argument("stream_id")
|
|
|
|
@click.argument("time")
|
|
|
|
@click.option("--error", help="Error message if any occurred.")
|
2023-02-25 20:57:03 +01:00
|
|
|
@pass_app
|
|
|
|
def stream(app: App, stream_id, time, error):
|
2022-01-17 10:51:32 +01:00
|
|
|
"""
|
|
|
|
Notify about output stream status.
|
|
|
|
|
|
|
|
Replaces: notify --error='#{msg}' --stream-id=#{stream} --time=#{!time}
|
|
|
|
Replaces: notify --connect --stream-id=#{stream} --time=#{!time}
|
|
|
|
"""
|
|
|
|
status = "OK"
|
|
|
|
if error is not None:
|
|
|
|
status = error
|
|
|
|
|
2023-02-26 12:01:59 +01:00
|
|
|
logger.info("Sending output stream '%s' status '%s'", stream_id, status)
|
2023-02-25 20:57:03 +01:00
|
|
|
app.api_client.notify_liquidsoap_status(status, stream_id, time)
|
2022-01-17 10:51:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
@cli.command()
|
2023-02-25 20:57:03 +01:00
|
|
|
@pass_app
|
|
|
|
def started(app: App):
|
2022-01-17 10:51:32 +01:00
|
|
|
"""
|
|
|
|
Notify liquidsoap startup status.
|
|
|
|
|
|
|
|
Replaces: notify --liquidsoap-started
|
|
|
|
"""
|
|
|
|
logger.debug("Notifying server that Liquidsoap has started")
|
2023-02-25 20:57:03 +01:00
|
|
|
app.api_client.notify_liquidsoap_started()
|