mirror of
https://github.com/onyx-and-iris/obsws-cli.git
synced 2026-04-18 15:03:44 +00:00
first commit
This commit is contained in:
78
obsws_cli/app.py
Normal file
78
obsws_cli/app.py
Normal file
@@ -0,0 +1,78 @@
|
||||
"""Command line interface for the OBS WebSocket API."""
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Annotated
|
||||
|
||||
import obsws_python as obsws
|
||||
import typer
|
||||
from pydantic import ConfigDict
|
||||
from pydantic_settings import BaseSettings
|
||||
|
||||
from . import group, input, item, record, scene, stream
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
"""Settings for the OBS WebSocket client."""
|
||||
|
||||
model_config = ConfigDict(
|
||||
env_file=(
|
||||
'.env',
|
||||
Path.home() / '.config' / 'obsws-cli' / 'obsws.env',
|
||||
),
|
||||
env_file_encoding='utf-8',
|
||||
env_prefix='OBSWS_',
|
||||
)
|
||||
|
||||
HOST: str = 'localhost'
|
||||
PORT: int = 4455
|
||||
PASSWORD: str = '' # No password by default
|
||||
TIMEOUT: int = 5 # Timeout for requests in seconds
|
||||
|
||||
|
||||
app = typer.Typer()
|
||||
app.add_typer(scene.app, name='scene')
|
||||
app.add_typer(item.app, name='item')
|
||||
app.add_typer(group.app, name='group')
|
||||
app.add_typer(input.app, name='input')
|
||||
app.add_typer(record.app, name='record')
|
||||
app.add_typer(stream.app, name='stream')
|
||||
|
||||
|
||||
@app.command()
|
||||
def version(ctx: typer.Context):
|
||||
"""Get the OBS Client and WebSocket versions."""
|
||||
resp = ctx.obj['obsws'].get_version()
|
||||
typer.echo(
|
||||
f'OBS Client version: {resp.obs_version} with WebSocket version: {resp.obs_web_socket_version}'
|
||||
)
|
||||
|
||||
|
||||
@app.callback()
|
||||
def main(
|
||||
ctx: typer.Context,
|
||||
host: Annotated[str, typer.Option(help='WebSocket host')] = None,
|
||||
port: Annotated[int, typer.Option(help='WebSocket port')] = None,
|
||||
password: Annotated[str, typer.Option(help='WebSocket password')] = None,
|
||||
timeout: Annotated[int, typer.Option(help='WebSocket timeout')] = None,
|
||||
):
|
||||
"""obsws_cli is a command line interface for the OBS WebSocket API."""
|
||||
settings = Settings()
|
||||
# Allow overriding settings with command line options
|
||||
if host:
|
||||
settings.HOST = host
|
||||
if port:
|
||||
settings.PORT = port
|
||||
if password:
|
||||
settings.PASSWORD = password
|
||||
if timeout:
|
||||
settings.TIMEOUT = timeout
|
||||
|
||||
ctx.obj = ctx.ensure_object(dict)
|
||||
ctx.obj['obsws'] = ctx.with_resource(
|
||||
obsws.ReqClient(
|
||||
host=settings.HOST,
|
||||
port=settings.PORT,
|
||||
password=settings.PASSWORD,
|
||||
timeout=settings.TIMEOUT,
|
||||
)
|
||||
)
|
||||
Reference in New Issue
Block a user