convert screenshot commands

This commit is contained in:
onyx-and-iris 2025-07-24 03:52:50 +01:00
parent 93b066090b
commit e77627b845
2 changed files with 23 additions and 31 deletions

View File

@ -34,6 +34,7 @@ for sub_app in (
'scene', 'scene',
'scenecollection', 'scenecollection',
'sceneitem', 'sceneitem',
'screenshot',
): ):
module = importlib.import_module(f'.{sub_app}', package=__package__) module = importlib.import_module(f'.{sub_app}', package=__package__)
app.command(module.app) app.command(module.app)

View File

@ -4,66 +4,57 @@ from pathlib import Path
from typing import Annotated from typing import Annotated
import obsws_python as obsws import obsws_python as obsws
import typer from cyclopts import App, Argument, Parameter
from . import console from . import console
from .alias import SubTyperAliasGroup from .context import Context
from .enum import ExitCode
from .error import OBSWSCLIError
app = typer.Typer(cls=SubTyperAliasGroup) app = App(name='screenshot', help='Commands for taking screenshots using OBS.')
@app.callback() @app.command(name=['save', 'sv'])
def main():
"""Take screenshots using OBS."""
@app.command('save | sv')
def save( def save(
ctx: typer.Context,
source_name: Annotated[ source_name: Annotated[
str, str,
typer.Argument( Argument(
..., hint='Name of the source to take a screenshot of.',
show_default=False,
help='Name of the source to take a screenshot of.',
), ),
], ],
output_path: Annotated[ output_path: Annotated[
Path, Path,
# Since the CLI and OBS may be running on different platforms, # Since the CLI and OBS may be running on different platforms,
# we won't validate the path here. # we won't validate the path here.
typer.Argument( Argument(
..., hint='Path to save the screenshot (must include file name and extension).',
show_default=False,
file_okay=True,
dir_okay=False,
help='Path to save the screenshot (must include file name and extension).',
), ),
], ],
/,
width: Annotated[ width: Annotated[
float, float,
typer.Option( Parameter(
help='Width of the screenshot.', help='Width of the screenshot.',
), ),
] = 1920, ] = 1920,
height: Annotated[ height: Annotated[
float, float,
typer.Option( Parameter(
help='Height of the screenshot.', help='Height of the screenshot.',
), ),
] = 1080, ] = 1080,
quality: Annotated[ quality: Annotated[
float, float,
typer.Option( Parameter(
min=-1,
max=100,
help='Quality of the screenshot.', help='Quality of the screenshot.',
), ),
] = -1, ] = -1,
*,
ctx: Annotated[Context, Parameter(parse=False)],
): ):
"""Take a screenshot and save it to a file.""" """Take a screenshot and save it to a file."""
try: try:
ctx.obj['obsws'].save_source_screenshot( ctx.client.save_source_screenshot(
name=source_name, name=source_name,
img_format=output_path.suffix.lstrip('.').lower(), img_format=output_path.suffix.lstrip('.').lower(),
file_path=str(output_path), file_path=str(output_path),
@ -74,16 +65,16 @@ def save(
except obsws.error.OBSSDKRequestError as e: except obsws.error.OBSSDKRequestError as e:
match e.code: match e.code:
case 403: case 403:
console.err.print( raise OBSWSCLIError(
'The [yellow]image format[/yellow] (file extension) must be included in the file name, ' 'The [yellow]image format[/yellow] (file extension) must be included in the file name, '
"for example: '/path/to/screenshot.png'.", "for example: '/path/to/screenshot.png'.",
code=ExitCode.ERROR,
) )
raise typer.Exit(1)
case 600: case 600:
console.err.print( raise OBSWSCLIError(
f'No source was found by the name of [yellow]{source_name}[/yellow]' 'No source was found by the name of [yellow]{source_name}[/yellow]',
code=ExitCode.ERROR,
) )
raise typer.Exit(1)
case _: case _:
raise raise