sintonia/playout/libretime_playout/notify/main.py

168 lines
4.4 KiB
Python
Raw Permalink Normal View History

2010-11-05 15:54:15 +01:00
"""
Python part of radio playout (pypo)
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
Main case:
2010-11-05 15:54:15 +01:00
- whenever LS starts playing a new track, its on_metadata callback calls
a function in ls (notify(m)) which then calls the python script here
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
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
"""
chore(deps): update pre-commit hook psf/black-pre-commit-mirror to v24 (#2917) [![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com) This PR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [psf/black-pre-commit-mirror](https://togithub.com/psf/black-pre-commit-mirror) | repository | major | `23.12.1` -> `24.1.1` | Note: The `pre-commit` manager in Renovate is not supported by the `pre-commit` maintainers or community. Please do not report any problems there, instead [create a Discussion in the Renovate repository](https://togithub.com/renovatebot/renovate/discussions/new) if you have any questions. --- ### Release Notes <details> <summary>psf/black-pre-commit-mirror (psf/black-pre-commit-mirror)</summary> ### [`v24.1.1`](https://togithub.com/psf/black-pre-commit-mirror/compare/24.1.0...24.1.1) [Compare Source](https://togithub.com/psf/black-pre-commit-mirror/compare/24.1.0...24.1.1) ### [`v24.1.0`](https://togithub.com/psf/black-pre-commit-mirror/compare/23.12.1...24.1.0) [Compare Source](https://togithub.com/psf/black-pre-commit-mirror/compare/23.12.1...24.1.0) </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Enabled. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://developer.mend.io/github/libretime/libretime). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4xMzUuMCIsInVwZGF0ZWRJblZlciI6IjM3LjE1My4yIiwidGFyZ2V0QnJhbmNoIjoibWFpbiJ9--> --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: jo <ljonas@riseup.net>
2024-02-02 20:24:25 +01:00
2023-02-26 01:27:00 +01:00
import logging
feat(playout): enhance playout logging (#1495) Some initial work on modernizing the playout app. This replace any custom logger or logging based logger with the logging tools from libretime_shared.logging and loguru. Removed all the thread/function assigned logger (self.logger = ...), as this makes it part of the logic (passing logger though function args) as it should not. Of a dedicated logger is required for a specific task, it should use the create_task_logger function. - refactor: remove dead code - refactor: remove py2 specific fix - feat: remove unused test command - feat: setup shared cli and logging tools - feat: replace logging with loguru - feat: setup shared cli and logging tools for notify - fix: warn method deos not exist - feat: make cli setup the entrypoint - fix: install shared modules globally in production use extra_requires to load local packages in dev environement - feat: configure log path in systemd service - feat: default behavior is to log to console only - feat: create log dir during install - chore: add comment - fix: don't create useless dir in install - fix: move notify logs to /var/log/libretime dir - fix: update setup_logger attrs - style: linting - fix: replace verbosity flag with log-level flag - feat: use shared logging tool in liquidsoap - fix: pass logger down to api client - feat: allow custom log_filepath in liquidsoap config - chore: add pylintrc to playout - refactor: fix pylint errors - feat: set liquidsoap log filepath in systemd service - fix: missing setup entrypoint update BREAKING CHANGE: for playout and liquidsoap the default log file path changed to None and will only log to the console when developing / testing. Unless you are running the app as a systemd service (production) the default logs filepaths changed: from "/var/log/airtime/pypo/pypo.log" to "/var/log/libretime/playout.log" and from "/var/log/airtime/pypo-liquidsoap/ls_script.log" to "/var/log/libretime/liquidsoap.log" BREAKING CHANGE: for playout-notify the default log file path changed from "/var/log/airtime/pypo/notify.log" to "/var/log/libretime/playout-notify.log"
2022-01-13 16:11:37 +01:00
from pathlib import Path
from typing import Literal, Optional
2010-11-05 15:54:15 +01:00
import click
from libretime_api_client.v1 import ApiClient as LegacyClient
from libretime_shared.cli import cli_config_options, cli_logging_options
from libretime_shared.config import DEFAULT_ENV_PREFIX
2023-02-26 01:27:00 +01:00
from libretime_shared.logging import setup_logger
from ..config import Config
2023-03-01 17:13:02 +01:00
logger = logging.getLogger(__name__)
2023-03-01 17:13:02 +01:00
# pylint: disable=too-few-public-methods
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,
)
pass_app = click.make_pass_decorator(App)
@click.group(context_settings={"auto_envvar_prefix": DEFAULT_ENV_PREFIX})
@cli_logging_options()
@cli_config_options()
@click.pass_context
def cli(
ctx: click.Context,
log_level: str,
log_filepath: Optional[Path],
config_filepath: Optional[Path],
):
"""
A gateway between Liquidsoap and the API.
"""
2023-02-26 01:27:00 +01:00
setup_logger(log_level, log_filepath, rotate=False)
ctx.obj = App(config=Config(config_filepath))
@cli.command()
@click.argument("media_id")
@pass_app
def media(app: App, media_id):
"""
Notify currently playing media.
Replaces: notify --media-id=#{m['schedule_table_id']}
"""
logger.info("Sending currently playing media id '%s'", media_id)
app.api_client.notify_media_item_start_playing(media_id)
@cli.command()
@click.argument("media_id")
@click.argument("data")
@pass_app
def webstream(app: App, media_id, data):
"""
Notify currently playing webstream.
Replaces: notify --webstream='#{json_str}' --media-id=#{!current_dyn_id}
"""
logger.info("Sending currently playing webstream '%s' data '%s'", media_id, data)
app.api_client.notify_webstream_data(data, media_id)
@cli.command()
@click.argument("name")
@click.argument("status")
@pass_app
def live(app: App, name, status):
"""
Notify currently playing live input.
Replaces: notify --source-name=#{sourcename} --source-status=#{status}
"""
logger.info("Sending currently playing live source '%s' status '%s'", name, status)
app.api_client.notify_source_status(name, status)
@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)
@cli.command()
@click.argument("stream_id")
@click.argument("time")
@click.option("--error", help="Error message if any occurred.")
@pass_app
def stream(app: App, stream_id, time, error):
"""
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
logger.info("Sending output stream '%s' status '%s'", stream_id, status)
app.api_client.notify_liquidsoap_status(status, stream_id, time)
@cli.command()
@pass_app
def started(app: App):
"""
Notify liquidsoap startup status.
Replaces: notify --liquidsoap-started
"""
logger.debug("Notifying server that Liquidsoap has started")
app.api_client.notify_liquidsoap_started()