From ad6c0bb0423507050a75dd9c4c961e60e7f3e19d Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Tue, 10 Jun 2025 12:51:52 +0100 Subject: [PATCH] implement stream command group minor bump --- pyproject.toml | 2 +- src/slobs_cli/stream.py | 105 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 src/slobs_cli/stream.py diff --git a/pyproject.toml b/pyproject.toml index 7e87b00..54b1d8c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "slobs-cli" -version = "0.2.0" +version = "0.3.0" description = "A command line application for Streamlabs Desktop" authors = [{ name = "onyx-and-iris", email = "code@onyxandiris.online" }] dependencies = ["pyslobs>=2.0.4", "asyncclick>=8.1.8"] diff --git a/src/slobs_cli/stream.py b/src/slobs_cli/stream.py new file mode 100644 index 0000000..3576266 --- /dev/null +++ b/src/slobs_cli/stream.py @@ -0,0 +1,105 @@ +import asyncclick as click +from anyio import create_task_group +from pyslobs import StreamingService + +from .cli import cli + + +@cli.group() +def stream(): + """Stream management commands.""" + + +@stream.command() +@click.pass_context +async def start(ctx: click.Context): + """Start the stream.""" + + conn = ctx.obj["connection"] + ss = StreamingService(conn) + + async def _run(conn): + current_state = await ss.get_model() + active = current_state.streaming_status != "offline" + + if active: + conn.close() + raise click.Abort(click.style("Stream is already active.", fg="red")) + + await ss.toggle_streaming() + click.echo("Stream started.") + conn.close() + + async with create_task_group() as tg: + tg.start_soon(conn.background_processing) + tg.start_soon(_run, conn) + + +@stream.command() +@click.pass_context +async def stop(ctx: click.Context): + """Stop the stream.""" + + conn = ctx.obj["connection"] + ss = StreamingService(conn) + + async def _run(conn): + current_state = await ss.get_model() + active = current_state.streaming_status != "offline" + + if not active: + conn.close() + raise click.Abort(click.style("Stream is already inactive.", fg="red")) + + await ss.toggle_streaming() + click.echo("Stream stopped.") + conn.close() + + async with create_task_group() as tg: + tg.start_soon(conn.background_processing) + tg.start_soon(_run, conn) + + +@stream.command() +@click.pass_context +async def status(ctx: click.Context): + """Get the current stream status.""" + + conn = ctx.obj["connection"] + ss = StreamingService(conn) + + async def _run(conn): + current_state = await ss.get_model() + status = current_state.streaming_status + click.echo(f"Current stream status: {status}") + conn.close() + + async with create_task_group() as tg: + tg.start_soon(conn.background_processing) + tg.start_soon(_run, conn) + + +@stream.command() +@click.pass_context +async def toggle(ctx: click.Context): + """Toggle the stream status.""" + + conn = ctx.obj["connection"] + ss = StreamingService(conn) + + async def _run(conn): + current_state = await ss.get_model() + active = current_state.streaming_status != "offline" + + if active: + await ss.toggle_streaming() + click.echo("Stream stopped.") + else: + await ss.toggle_streaming() + click.echo("Stream started.") + + conn.close() + + async with create_task_group() as tg: + tg.start_soon(conn.background_processing) + tg.start_soon(_run, conn)