mirror of
https://github.com/onyx-and-iris/obsws-python.git
synced 2026-04-18 05:53:32 +00:00
Compare commits
88 Commits
ce6873f57a
...
autogen-re
| Author | SHA1 | Date | |
|---|---|---|---|
| d83594760c | |||
| 46a85f2904 | |||
| fe9f305afe | |||
| 7126a2efe0 | |||
| 3d312c18ed | |||
| 9d99ea0aea | |||
| 4e9fb934be | |||
| f70583d7ca | |||
| 82baa40e79 | |||
| 3bce50701e | |||
| 8e8062d5c8 | |||
| 6f64e884d8 | |||
| 90abc4f9ee | |||
| f564f53c69 | |||
| 4d9dfa9d11 | |||
| 9e361f0f8a | |||
| 797161a6f2 | |||
| 0fe78197fc | |||
|
|
9c4c5a1df9 | ||
| f52ac163b8 | |||
|
|
197a60a7cd | ||
|
|
633093ead4 | ||
| 935392a0b6 | |||
| d2f2926334 | |||
|
|
58cd50dd6c | ||
| 7614cdfe4a | |||
|
|
9402f2e472 | ||
| ef8df5cf4d | |||
| 1abca0c7e4 | |||
| 85180c1d94 | |||
| f4db1ad95c | |||
| efaee7594e | |||
| 2cebd5eedb | |||
| cac236c004 | |||
| 6aa6db09eb | |||
| f1c2efa4a1 | |||
|
|
4654d2529f | ||
| 1494208f63 | |||
|
|
d217630289 | ||
|
|
5bfe792fa6 | ||
| 3c36619173 | |||
| c4cf817042 | |||
| ba5da8dfef | |||
| 83577e2d61 | |||
|
|
8aa2e78ba6 | ||
| 780f07e25f | |||
| 70a422696e | |||
| a7ef61018b | |||
| 013cf15024 | |||
| f88e8ee3a6 | |||
| 6fa24fe609 | |||
| ffd215aadf | |||
| f3e75c0ddf | |||
| 5db7a705c5 | |||
| ca72b92eb3 | |||
| 98b17b6749 | |||
| 5462c47b65 | |||
| 126e5cb0a4 | |||
|
|
4ced7193df | ||
|
|
468c63f697 | ||
|
|
24f8487d93 | ||
| 2c07f242ad | |||
| 4e45de17ea | |||
| 491a26aaf7 | |||
| d84d30b752 | |||
| 9e3c1d3f37 | |||
| 82b6cdcd04 | |||
|
|
64a7c2b753 | ||
|
|
15559fdb33 | ||
|
|
3adf094481 | ||
|
|
9c41f2bb59 | ||
| d1c7462cc6 | |||
| 2de7151739 | |||
| 91ba90056c | |||
|
|
5e68262a80 | ||
|
|
ef0f770c0c | ||
| 48e90c82fb | |||
| cc9b1e2c72 | |||
| 41b0dfbe4b | |||
| cf888b0c4a | |||
| 92e2c29bd6 | |||
| 335fa42948 | |||
| 83afe31e04 | |||
| 5294e1afe2 | |||
| c6cbe1c894 | |||
|
|
13ef8108df | ||
|
|
3786739eee | ||
|
|
71c1e65483 |
175
.gitignore
vendored
175
.gitignore
vendored
@@ -1,8 +1,14 @@
|
|||||||
|
# Generated by ignr: github.com/onyx-and-iris/ignr
|
||||||
|
|
||||||
|
## Python ##
|
||||||
# Byte-compiled / optimized / DLL files
|
# Byte-compiled / optimized / DLL files
|
||||||
__pycache__/
|
__pycache__/
|
||||||
*.py[cod]
|
*.py[codz]
|
||||||
*$py.class
|
*$py.class
|
||||||
|
|
||||||
|
# C extensions
|
||||||
|
*.so
|
||||||
|
|
||||||
# Distribution / packaging
|
# Distribution / packaging
|
||||||
.Python
|
.Python
|
||||||
build/
|
build/
|
||||||
@@ -17,11 +23,22 @@ parts/
|
|||||||
sdist/
|
sdist/
|
||||||
var/
|
var/
|
||||||
wheels/
|
wheels/
|
||||||
|
share/python-wheels/
|
||||||
*.egg-info/
|
*.egg-info/
|
||||||
.installed.cfg
|
.installed.cfg
|
||||||
*.egg
|
*.egg
|
||||||
MANIFEST
|
MANIFEST
|
||||||
|
|
||||||
|
# PyInstaller
|
||||||
|
# Usually these files are written by a python script from a template
|
||||||
|
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||||
|
*.manifest
|
||||||
|
*.spec
|
||||||
|
|
||||||
|
# Installer logs
|
||||||
|
pip-log.txt
|
||||||
|
pip-delete-this-directory.txt
|
||||||
|
|
||||||
# Unit test / coverage reports
|
# Unit test / coverage reports
|
||||||
htmlcov/
|
htmlcov/
|
||||||
.tox/
|
.tox/
|
||||||
@@ -32,19 +49,169 @@ htmlcov/
|
|||||||
nosetests.xml
|
nosetests.xml
|
||||||
coverage.xml
|
coverage.xml
|
||||||
*.cover
|
*.cover
|
||||||
|
*.py.cover
|
||||||
.hypothesis/
|
.hypothesis/
|
||||||
.pytest_cache/
|
.pytest_cache/
|
||||||
|
cover/
|
||||||
|
|
||||||
|
# Translations
|
||||||
|
*.mo
|
||||||
|
*.pot
|
||||||
|
|
||||||
|
# Django stuff:
|
||||||
|
*.log
|
||||||
|
local_settings.py
|
||||||
|
db.sqlite3
|
||||||
|
db.sqlite3-journal
|
||||||
|
|
||||||
|
# Flask stuff:
|
||||||
|
instance/
|
||||||
|
.webassets-cache
|
||||||
|
|
||||||
|
# Scrapy stuff:
|
||||||
|
.scrapy
|
||||||
|
|
||||||
|
# Sphinx documentation
|
||||||
|
docs/_build/
|
||||||
|
|
||||||
|
# PyBuilder
|
||||||
|
.pybuilder/
|
||||||
|
target/
|
||||||
|
|
||||||
|
# Jupyter Notebook
|
||||||
|
.ipynb_checkpoints
|
||||||
|
|
||||||
|
# IPython
|
||||||
|
profile_default/
|
||||||
|
ipython_config.py
|
||||||
|
|
||||||
|
# pyenv
|
||||||
|
# For a library or package, you might want to ignore these files since the code is
|
||||||
|
# intended to run in multiple environments; otherwise, check them in:
|
||||||
|
# .python-version
|
||||||
|
|
||||||
|
# pipenv
|
||||||
|
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
||||||
|
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
||||||
|
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
||||||
|
# install all needed dependencies.
|
||||||
|
#Pipfile.lock
|
||||||
|
|
||||||
|
# UV
|
||||||
|
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
||||||
|
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
||||||
|
# commonly ignored for libraries.
|
||||||
|
#uv.lock
|
||||||
|
|
||||||
|
# poetry
|
||||||
|
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
||||||
|
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
||||||
|
# commonly ignored for libraries.
|
||||||
|
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
||||||
|
#poetry.lock
|
||||||
|
#poetry.toml
|
||||||
|
|
||||||
|
# pdm
|
||||||
|
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
||||||
|
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
||||||
|
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
||||||
|
#pdm.lock
|
||||||
|
#pdm.toml
|
||||||
|
.pdm-python
|
||||||
|
.pdm-build/
|
||||||
|
|
||||||
|
# pixi
|
||||||
|
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
||||||
|
#pixi.lock
|
||||||
|
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
||||||
|
# in the .venv directory. It is recommended not to include this directory in version control.
|
||||||
|
.pixi
|
||||||
|
|
||||||
|
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
||||||
|
__pypackages__/
|
||||||
|
|
||||||
|
# Celery stuff
|
||||||
|
celerybeat-schedule
|
||||||
|
celerybeat.pid
|
||||||
|
|
||||||
|
# SageMath parsed files
|
||||||
|
*.sage.py
|
||||||
|
|
||||||
# Environments
|
# Environments
|
||||||
.env
|
.env
|
||||||
|
.envrc
|
||||||
.venv
|
.venv
|
||||||
env/
|
env/
|
||||||
venv/
|
venv/
|
||||||
ENV/
|
ENV/
|
||||||
env.bak/
|
env.bak/
|
||||||
venv.bak/
|
venv.bak/
|
||||||
.hatch
|
|
||||||
|
|
||||||
# Test/config
|
# Spyder project settings
|
||||||
quick.py
|
.spyderproject
|
||||||
|
.spyproject
|
||||||
|
|
||||||
|
# Rope project settings
|
||||||
|
.ropeproject
|
||||||
|
|
||||||
|
# mkdocs documentation
|
||||||
|
/site
|
||||||
|
|
||||||
|
# mypy
|
||||||
|
.mypy_cache/
|
||||||
|
.dmypy.json
|
||||||
|
dmypy.json
|
||||||
|
|
||||||
|
# Pyre type checker
|
||||||
|
.pyre/
|
||||||
|
|
||||||
|
# pytype static type analyzer
|
||||||
|
.pytype/
|
||||||
|
|
||||||
|
# Cython debug symbols
|
||||||
|
cython_debug/
|
||||||
|
|
||||||
|
# PyCharm
|
||||||
|
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
||||||
|
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
||||||
|
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||||
|
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||||
|
#.idea/
|
||||||
|
|
||||||
|
# Abstra
|
||||||
|
# Abstra is an AI-powered process automation framework.
|
||||||
|
# Ignore directories containing user credentials, local state, and settings.
|
||||||
|
# Learn more at https://abstra.io/docs
|
||||||
|
.abstra/
|
||||||
|
|
||||||
|
# Visual Studio Code
|
||||||
|
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
||||||
|
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
||||||
|
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
||||||
|
# you could uncomment the following to ignore the entire vscode folder
|
||||||
|
.vscode/
|
||||||
|
|
||||||
|
# Ruff stuff:
|
||||||
|
.ruff_cache/
|
||||||
|
|
||||||
|
# PyPI configuration file
|
||||||
|
.pypirc
|
||||||
|
|
||||||
|
# Cursor
|
||||||
|
# Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
|
||||||
|
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
|
||||||
|
# refer to https://docs.cursor.com/context/ignore-files
|
||||||
|
.cursorignore
|
||||||
|
.cursorindexingignore
|
||||||
|
|
||||||
|
# Marimo
|
||||||
|
marimo/_static/
|
||||||
|
marimo/_lsp/
|
||||||
|
__marimo__/
|
||||||
|
|
||||||
|
# End of ignr
|
||||||
|
|
||||||
|
tools/
|
||||||
|
|
||||||
|
test-*.py
|
||||||
config.toml
|
config.toml
|
||||||
10
.pre-commit-config.yaml
Normal file
10
.pre-commit-config.yaml
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
repos:
|
||||||
|
- repo: local
|
||||||
|
hooks:
|
||||||
|
- id: format
|
||||||
|
name: format
|
||||||
|
entry: hatch run style:fmt
|
||||||
|
language: system
|
||||||
|
pass_filenames: false
|
||||||
|
verbose: true
|
||||||
|
files: \.(py)$
|
||||||
71
README.md
71
README.md
@@ -1,19 +1,19 @@
|
|||||||
[](https://badge.fury.io/py/obsws-python)
|
[](https://badge.fury.io/py/obsws-python)
|
||||||
[](https://github.com/aatikturk/obsstudio_sdk/blob/main/LICENSE)
|
[](https://github.com/aatikturk/obsstudio_sdk/blob/main/LICENSE)
|
||||||
|
[](https://github.com/pypa/hatch)
|
||||||
[](https://github.com/psf/black)
|
[](https://github.com/psf/black)
|
||||||
[](https://pycqa.github.io/isort/)
|
[](https://pycqa.github.io/isort/)
|
||||||
|
|
||||||
# A Python SDK for OBS Studio WebSocket v5.0
|
# A Python SDK for OBS Studio WebSocket v5.0
|
||||||
|
|
||||||
This is a wrapper around OBS Websocket.
|
|
||||||
Not all endpoints in the official documentation are implemented.
|
Not all endpoints in the official documentation are implemented.
|
||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
|
|
||||||
- [OBS Studio](https://obsproject.com/)
|
- [OBS Studio](https://obsproject.com/)
|
||||||
- [OBS Websocket v5 Plugin](https://github.com/obsproject/obs-websocket/releases/tag/5.0.0)
|
- [OBS Websocket v5 Plugin](https://github.com/obsproject/obs-websocket/releases/tag/5.0.0)
|
||||||
- With the release of OBS Studio version 28, Websocket plugin is included by default. But it should be manually installed for earlier versions of OBS.
|
- With the release of OBS Studio version 28, Websocket plugin is included by default. But it should be manually installed for earlier versions of OBS.
|
||||||
- Python 3.10 or greater
|
- Python 3.9 or greater
|
||||||
|
|
||||||
### How to install using pip
|
### How to install using pip
|
||||||
|
|
||||||
@@ -25,9 +25,10 @@ pip install obsws-python
|
|||||||
|
|
||||||
By default the clients connect with parameters:
|
By default the clients connect with parameters:
|
||||||
|
|
||||||
- `host`: "localhost"
|
- `host`: "localhost"
|
||||||
- `port`: 4455
|
- `port`: 4455
|
||||||
- `password`: None
|
- `password`: ""
|
||||||
|
- `timeout`: None
|
||||||
|
|
||||||
You may override these parameters by storing them in a toml config file or passing them as keyword arguments.
|
You may override these parameters by storing them in a toml config file or passing them as keyword arguments.
|
||||||
|
|
||||||
@@ -44,7 +45,7 @@ port = 4455
|
|||||||
password = "mystrongpass"
|
password = "mystrongpass"
|
||||||
```
|
```
|
||||||
|
|
||||||
It should be placed next to your `__main__.py` file.
|
It should be placed in your user home directory.
|
||||||
|
|
||||||
#### Otherwise:
|
#### Otherwise:
|
||||||
|
|
||||||
@@ -54,7 +55,7 @@ Example `__main__.py`:
|
|||||||
import obsws_python as obs
|
import obsws_python as obs
|
||||||
|
|
||||||
# pass conn info if not in config.toml
|
# pass conn info if not in config.toml
|
||||||
cl = obs.ReqClient(host='localhost', port=4455, password='mystrongpass')
|
cl = obs.ReqClient(host='localhost', port=4455, password='mystrongpass', timeout=3)
|
||||||
|
|
||||||
# Toggle the mute state of your Mic input
|
# Toggle the mute state of your Mic input
|
||||||
cl.toggle_input_mute('Mic/Aux')
|
cl.toggle_input_mute('Mic/Aux')
|
||||||
@@ -62,7 +63,7 @@ cl.toggle_input_mute('Mic/Aux')
|
|||||||
|
|
||||||
### Requests
|
### Requests
|
||||||
|
|
||||||
Method names for requests match the API calls but snake cased.
|
Method names for requests match the API calls but snake cased. If a successful call is made with the Request client and the response is expected to contain fields then a response object will be returned. You may then access the response fields as class attributes. They will be snake cased.
|
||||||
|
|
||||||
example:
|
example:
|
||||||
|
|
||||||
@@ -70,14 +71,29 @@ example:
|
|||||||
# load conn info from config.toml
|
# load conn info from config.toml
|
||||||
cl = obs.ReqClient()
|
cl = obs.ReqClient()
|
||||||
|
|
||||||
# GetVersion
|
# GetVersion, returns a response object
|
||||||
resp = cl.get_version()
|
resp = cl.get_version()
|
||||||
|
# Access it's field as an attribute
|
||||||
|
print(f"OBS Version: {resp.obs_version}")
|
||||||
|
|
||||||
|
|
||||||
# SetCurrentProgramScene
|
# SetCurrentProgramScene
|
||||||
cl.set_current_program_scene("BRB")
|
cl.set_current_program_scene("BRB")
|
||||||
```
|
```
|
||||||
|
|
||||||
For a full list of requests refer to [Requests](https://github.com/obsproject/obs-websocket/blob/master/docs/generated/protocol.md#requests)
|
#### `send(param, data=None, raw=False)`
|
||||||
|
|
||||||
|
If you prefer to work with the JSON data directly the {ReqClient}.send() method accepts an argument, `raw`. If set to True the raw response data will be returned, instead of a response object.
|
||||||
|
|
||||||
|
example:
|
||||||
|
|
||||||
|
```python
|
||||||
|
resp = cl_req.send("GetVersion", raw=True)
|
||||||
|
|
||||||
|
print(f"response data: {resp}")
|
||||||
|
```
|
||||||
|
|
||||||
|
For a full list of requests refer to [Requests][obsws-reqs]
|
||||||
|
|
||||||
### Events
|
### Events
|
||||||
|
|
||||||
@@ -110,7 +126,7 @@ cl.callback.deregister(on_input_mute_state_changed)
|
|||||||
|
|
||||||
`register(fns)` and `deregister(fns)` accept both single functions and lists of functions.
|
`register(fns)` and `deregister(fns)` accept both single functions and lists of functions.
|
||||||
|
|
||||||
For a full list of events refer to [Events](https://github.com/obsproject/obs-websocket/blob/master/docs/generated/protocol.md#events)
|
For a full list of events refer to [Events][obsws-events]
|
||||||
|
|
||||||
### Attributes
|
### Attributes
|
||||||
|
|
||||||
@@ -128,9 +144,13 @@ def on_scene_created(data):
|
|||||||
|
|
||||||
### Errors
|
### Errors
|
||||||
|
|
||||||
If a request fails an `OBSSDKError` will be raised with a status code.
|
- `OBSSDKError`: Base error class.
|
||||||
|
- `OBSSDKTimeoutError`: Raised if a timeout occurs during sending/receiving a request or receiving an event
|
||||||
For a full list of status codes refer to [Codes](https://github.com/obsproject/obs-websocket/blob/master/docs/generated/protocol.md#requeststatus)
|
- `OBSSDKRequestError`: Raised when a request returns an error code.
|
||||||
|
- The following attributes are available:
|
||||||
|
- `req_name`: name of the request.
|
||||||
|
- `code`: request status code.
|
||||||
|
- For a full list of status codes refer to [Codes][obsws-codes]
|
||||||
|
|
||||||
### Logging
|
### Logging
|
||||||
|
|
||||||
@@ -149,18 +169,21 @@ logging.basicConfig(level=logging.DEBUG)
|
|||||||
|
|
||||||
### Tests
|
### Tests
|
||||||
|
|
||||||
First install development dependencies:
|
Install [hatch][hatch-install] and then:
|
||||||
|
|
||||||
`pip install -e .['dev']`
|
|
||||||
|
|
||||||
To run all tests:
|
|
||||||
|
|
||||||
```
|
```
|
||||||
pytest -v
|
hatch test
|
||||||
```
|
```
|
||||||
|
|
||||||
### Official Documentation
|
### Official Documentation
|
||||||
|
|
||||||
For the full documentation:
|
For the full documentation:
|
||||||
|
|
||||||
- [OBS Websocket SDK](https://github.com/obsproject/obs-websocket/blob/master/docs/generated/protocol.md#obs-websocket-501-protocol)
|
- [OBS Websocket SDK][obsws-pro]
|
||||||
|
|
||||||
|
|
||||||
|
[obsws-reqs]: https://github.com/obsproject/obs-websocket/blob/master/docs/generated/protocol.md#requests
|
||||||
|
[obsws-events]: https://github.com/obsproject/obs-websocket/blob/master/docs/generated/protocol.md#events
|
||||||
|
[obsws-codes]: https://github.com/obsproject/obs-websocket/blob/master/docs/generated/protocol.md#requeststatus
|
||||||
|
[obsws-pro]: https://github.com/obsproject/obs-websocket/blob/master/docs/generated/protocol.md#obs-websocket-501-protocol
|
||||||
|
[hatch-install]: https://hatch.pypa.io/latest/install/
|
||||||
|
|||||||
23
Taskfile.yaml
Normal file
23
Taskfile.yaml
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
version: '3'
|
||||||
|
|
||||||
|
tasks:
|
||||||
|
default:
|
||||||
|
desc: Generate ReqClient methods from the API spec
|
||||||
|
deps: [generate]
|
||||||
|
|
||||||
|
generate:
|
||||||
|
desc: Generate ReqClient methods from the API spec
|
||||||
|
deps: [pull-protocol-json]
|
||||||
|
cmds:
|
||||||
|
- python tools/generate.py
|
||||||
|
- hatch run style:fmt
|
||||||
|
internal: true
|
||||||
|
|
||||||
|
pull-protocol-json:
|
||||||
|
desc: Pull the latest API spec from the obs-websocket repository
|
||||||
|
preconditions:
|
||||||
|
- sh: '[ ! -f tools/protocol.json ] || [ "$(find tools/protocol.json -mmin +1440)" ]'
|
||||||
|
msg: 'The protocol.json file is up to date (last modified less than 24 hours ago).'
|
||||||
|
cmds:
|
||||||
|
- curl -sSfL -o tools/protocol.json "https://raw.githubusercontent.com/obsproject/obs-websocket/refs/heads/master/docs/generated/protocol.json"
|
||||||
|
internal: true
|
||||||
@@ -6,7 +6,7 @@ Registers a list of callback functions to hook into OBS events.
|
|||||||
|
|
||||||
Simply run the code and trigger the events, press `<Enter>` to exit.
|
Simply run the code and trigger the events, press `<Enter>` to exit.
|
||||||
|
|
||||||
This example assumes the existence of a `config.toml`, placed next to `__main__.py`:
|
This example assumes the existence of a `config.toml`, placed in your user home directory:
|
||||||
|
|
||||||
```toml
|
```toml
|
||||||
[connection]
|
[connection]
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
import time
|
import os
|
||||||
|
from threading import Event
|
||||||
|
|
||||||
import obsws_python as obs
|
import obsws_python as obs
|
||||||
|
|
||||||
|
|
||||||
class Observer:
|
class Observer:
|
||||||
def __init__(self):
|
def __init__(self, host, port, password, stop_event):
|
||||||
self._client = obs.EventClient()
|
self._client = obs.EventClient(host=host, port=port, password=password)
|
||||||
|
self._stop_event = stop_event
|
||||||
self._client.callback.register(
|
self._client.callback.register(
|
||||||
[
|
[
|
||||||
self.on_current_program_scene_changed,
|
self.on_current_program_scene_changed,
|
||||||
@@ -15,7 +17,12 @@ class Observer:
|
|||||||
]
|
]
|
||||||
)
|
)
|
||||||
print(f"Registered events: {self._client.callback.get()}")
|
print(f"Registered events: {self._client.callback.get()}")
|
||||||
self.running = True
|
|
||||||
|
def __enter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, exc_type, exc_value, exc_traceback):
|
||||||
|
self._client.disconnect()
|
||||||
|
|
||||||
def on_current_program_scene_changed(self, data):
|
def on_current_program_scene_changed(self, data):
|
||||||
"""The current program scene has changed."""
|
"""The current program scene has changed."""
|
||||||
@@ -31,13 +38,16 @@ class Observer:
|
|||||||
|
|
||||||
def on_exit_started(self, _):
|
def on_exit_started(self, _):
|
||||||
"""OBS has begun the shutdown process."""
|
"""OBS has begun the shutdown process."""
|
||||||
print(f"OBS closing!")
|
print("OBS closing!")
|
||||||
self._client.unsubscribe()
|
self._stop_event.set()
|
||||||
self.running = False
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
observer = Observer()
|
host = os.getenv("OBSWS_HOST", "localhost")
|
||||||
|
port = int(os.getenv("OBSWS_PORT", 4455))
|
||||||
|
password = os.getenv("OBSWS_PASSWORD", "")
|
||||||
|
|
||||||
while observer.running:
|
stop_event = Event()
|
||||||
time.sleep(0.1)
|
|
||||||
|
with Observer(host, port, password, stop_event) as observer:
|
||||||
|
stop_event.wait()
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ Requires [Python Keyboard library](https://github.com/boppreh/keyboard).
|
|||||||
|
|
||||||
Simply run the code and press the assigned hotkeys. Press `ctrl+enter` to exit.
|
Simply run the code and press the assigned hotkeys. Press `ctrl+enter` to exit.
|
||||||
|
|
||||||
This example assumes the existence of a `config.toml`, placed next to `__main__.py`:
|
This example assumes the existence of a `config.toml`, placed in your user home directory:
|
||||||
|
|
||||||
```toml
|
```toml
|
||||||
[connection]
|
[connection]
|
||||||
|
|||||||
@@ -1,15 +1,23 @@
|
|||||||
import inspect
|
import inspect
|
||||||
|
import os
|
||||||
|
|
||||||
|
import keyboard # type: ignore
|
||||||
|
|
||||||
import keyboard
|
|
||||||
import obsws_python as obs
|
import obsws_python as obs
|
||||||
|
|
||||||
|
|
||||||
class Observer:
|
class Observer:
|
||||||
def __init__(self):
|
def __init__(self, host, port, password):
|
||||||
self._client = obs.EventClient()
|
self._client = obs.EventClient(host=host, port=port, password=password)
|
||||||
self._client.callback.register(self.on_current_program_scene_changed)
|
self._client.callback.register(self.on_current_program_scene_changed)
|
||||||
print(f"Registered events: {self._client.callback.get()}")
|
print(f"Registered events: {self._client.callback.get()}")
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, exc_type, exc_value, exc_traceback):
|
||||||
|
self._client.disconnect()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def event_identifier(self):
|
def event_identifier(self):
|
||||||
return inspect.stack()[1].function
|
return inspect.stack()[1].function
|
||||||
@@ -27,17 +35,20 @@ def version():
|
|||||||
|
|
||||||
|
|
||||||
def set_scene(scene, *args):
|
def set_scene(scene, *args):
|
||||||
req_client.set_current_program_scene(scene)
|
req_client.set_current_program_scene(scene_name=scene)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
req_client = obs.ReqClient()
|
host = os.getenv("OBSWS_HOST", "localhost")
|
||||||
observer = Observer()
|
port = int(os.getenv("OBSWS_PORT", 4455))
|
||||||
|
password = os.getenv("OBSWS_PASSWORD", "")
|
||||||
|
|
||||||
keyboard.add_hotkey("0", version)
|
with obs.ReqClient(host=host, port=port, password=password) as req_client:
|
||||||
keyboard.add_hotkey("1", set_scene, args=("START",))
|
with Observer(host, port, password) as observer:
|
||||||
keyboard.add_hotkey("2", set_scene, args=("BRB",))
|
keyboard.add_hotkey("0", version)
|
||||||
keyboard.add_hotkey("3", set_scene, args=("END",))
|
keyboard.add_hotkey("1", set_scene, args=("START",))
|
||||||
|
keyboard.add_hotkey("2", set_scene, args=("BRB",))
|
||||||
|
keyboard.add_hotkey("3", set_scene, args=("END",))
|
||||||
|
|
||||||
print("press ctrl+enter to quit")
|
print("press ctrl+enter to quit")
|
||||||
keyboard.wait("ctrl+enter")
|
keyboard.wait("ctrl+enter")
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ Prints POSTFADER level values for audio device `Desktop Audio`. If mute toggled
|
|||||||
|
|
||||||
## Use
|
## Use
|
||||||
|
|
||||||
This example assumes the existence of a `config.toml`, placed next to `__main__.py`:
|
This example assumes the existence of a `config.toml`, placed in your user home directory:
|
||||||
|
|
||||||
```toml
|
```toml
|
||||||
[connection]
|
[connection]
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import os
|
||||||
from enum import IntEnum
|
from enum import IntEnum
|
||||||
from math import log
|
from math import log
|
||||||
|
|
||||||
@@ -9,6 +10,8 @@ LEVELTYPE = IntEnum(
|
|||||||
start=0,
|
start=0,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
DEVICE = "Desktop Audio"
|
||||||
|
|
||||||
|
|
||||||
def on_input_mute_state_changed(data):
|
def on_input_mute_state_changed(data):
|
||||||
"""An input's mute state has changed."""
|
"""An input's mute state has changed."""
|
||||||
@@ -32,15 +35,21 @@ def on_input_volume_meters(data):
|
|||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
client = obs.EventClient(subs=(obs.Subs.LOW_VOLUME | obs.Subs.INPUTVOLUMEMETERS))
|
host = os.getenv("OBSWS_HOST", "localhost")
|
||||||
client.callback.register([on_input_volume_meters, on_input_mute_state_changed])
|
port = int(os.getenv("OBSWS_PORT", 4455))
|
||||||
|
password = os.getenv("OBSWS_PASSWORD", "")
|
||||||
|
|
||||||
while cmd := input("<Enter> to exit>\n"):
|
with obs.EventClient(
|
||||||
if not cmd:
|
host=host,
|
||||||
break
|
port=port,
|
||||||
|
password=password,
|
||||||
|
subs=(obs.Subs.LOW_VOLUME | obs.Subs.INPUTVOLUMEMETERS),
|
||||||
|
) as client:
|
||||||
|
client.callback.register([on_input_volume_meters, on_input_mute_state_changed])
|
||||||
|
|
||||||
|
while _ := input("Press <Enter> to exit\n"):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
DEVICE = "Desktop Audio"
|
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ Collects the names of all available scenes, rotates through them and prints thei
|
|||||||
|
|
||||||
## Use
|
## Use
|
||||||
|
|
||||||
This example assumes the existence of a `config.toml`, placed next to `__main__.py`:
|
This example assumes the existence of a `config.toml`, placed in your user home directory:
|
||||||
|
|
||||||
```toml
|
```toml
|
||||||
[connection]
|
[connection]
|
||||||
|
|||||||
@@ -1,16 +1,21 @@
|
|||||||
|
import os
|
||||||
import time
|
import time
|
||||||
|
|
||||||
import obsws_python as obs
|
import obsws_python as obs
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
with obs.ReqClient() as client:
|
host = os.getenv("OBSWS_HOST", "localhost")
|
||||||
|
port = int(os.getenv("OBSWS_PORT", 4455))
|
||||||
|
password = os.getenv("OBSWS_PASSWORD", "")
|
||||||
|
|
||||||
|
with obs.ReqClient(host=host, port=port, password=password) as client:
|
||||||
resp = client.get_scene_list()
|
resp = client.get_scene_list()
|
||||||
scenes = [di.get("sceneName") for di in reversed(resp.scenes)]
|
scenes = [di.get("sceneName") for di in reversed(resp.scenes)]
|
||||||
|
|
||||||
for scene in scenes:
|
for scene in scenes:
|
||||||
print(f"Switching to scene {scene}")
|
print(f"Switching to scene {scene}")
|
||||||
client.set_current_program_scene(scene)
|
client.set_current_program_scene(scene_name=scene)
|
||||||
time.sleep(0.5)
|
time.sleep(0.5)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
from .enum import Subs
|
|
||||||
from .events import EventClient
|
from .events import EventClient
|
||||||
from .reqs import ReqClient
|
from .reqs import ReqClient
|
||||||
|
from .subs import Subs
|
||||||
from .version import version as __version__
|
from .version import version as __version__
|
||||||
|
|
||||||
__ALL__ = ["ReqClient", "EventClient", "Subs"]
|
__ALL__ = ["ReqClient", "EventClient", "Subs"]
|
||||||
|
|||||||
@@ -4,22 +4,26 @@ import json
|
|||||||
import logging
|
import logging
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from random import randint
|
from random import randint
|
||||||
|
from typing import Optional
|
||||||
try:
|
|
||||||
import tomllib
|
|
||||||
except ModuleNotFoundError:
|
|
||||||
import tomli as tomllib
|
|
||||||
|
|
||||||
import websocket
|
import websocket
|
||||||
|
from websocket import WebSocketTimeoutException
|
||||||
|
|
||||||
from .error import OBSSDKError
|
from .error import OBSSDKError, OBSSDKTimeoutError
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class ObsClient:
|
class ObsClient:
|
||||||
logger = logging.getLogger("baseclient.obsclient")
|
|
||||||
|
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
defaultkwargs = {"host": "localhost", "port": 4455, "password": None, "subs": 0}
|
self.logger = logger.getChild(self.__class__.__name__)
|
||||||
|
defaultkwargs = {
|
||||||
|
"host": "localhost",
|
||||||
|
"port": 4455,
|
||||||
|
"password": "",
|
||||||
|
"subs": 0,
|
||||||
|
"timeout": None,
|
||||||
|
}
|
||||||
if not any(key in kwargs for key in ("host", "port", "password")):
|
if not any(key in kwargs for key in ("host", "port", "password")):
|
||||||
kwargs |= self._conn_from_toml()
|
kwargs |= self._conn_from_toml()
|
||||||
kwargs = defaultkwargs | kwargs
|
kwargs = defaultkwargs | kwargs
|
||||||
@@ -27,21 +31,47 @@ class ObsClient:
|
|||||||
setattr(self, attr, val)
|
setattr(self, attr, val)
|
||||||
|
|
||||||
self.logger.info(
|
self.logger.info(
|
||||||
"Connecting with parameters: {host} {port} {password} {subs}".format(
|
"Connecting with parameters: host='{host}' port={port} password='{password}' subs={subs} timeout={timeout}".format(
|
||||||
**self.__dict__
|
**self.__dict__
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
self.ws = websocket.WebSocket()
|
try:
|
||||||
self.ws.connect(f"ws://{self.host}:{self.port}")
|
self.ws = websocket.WebSocket()
|
||||||
self.server_hello = json.loads(self.ws.recv())
|
self.ws.connect(f"ws://{self.host}:{self.port}", timeout=self.timeout)
|
||||||
|
self.server_hello = json.loads(self.ws.recv())
|
||||||
|
except ValueError as e:
|
||||||
|
self.logger.error(f"{type(e).__name__}: {e}")
|
||||||
|
raise
|
||||||
|
except (ConnectionRefusedError, TimeoutError, WebSocketTimeoutException) as e:
|
||||||
|
self.logger.exception(f"{type(e).__name__}: {e}")
|
||||||
|
raise
|
||||||
|
|
||||||
def _conn_from_toml(self) -> dict:
|
def _conn_from_toml(self) -> dict:
|
||||||
|
try:
|
||||||
|
import tomllib
|
||||||
|
except ModuleNotFoundError:
|
||||||
|
import tomli as tomllib
|
||||||
|
|
||||||
|
def get_filepath() -> Optional[Path]:
|
||||||
|
"""
|
||||||
|
traverses a list of paths for a 'config.toml'
|
||||||
|
returns the first config file found or None.
|
||||||
|
"""
|
||||||
|
filepaths = [
|
||||||
|
Path.cwd() / "config.toml",
|
||||||
|
Path.home() / "config.toml",
|
||||||
|
Path.home() / ".config" / "obsws-python" / "config.toml",
|
||||||
|
]
|
||||||
|
for filepath in filepaths:
|
||||||
|
if filepath.exists():
|
||||||
|
return filepath
|
||||||
|
|
||||||
conn = {}
|
conn = {}
|
||||||
filepath = Path.cwd() / "config.toml"
|
if filepath := get_filepath():
|
||||||
if filepath.exists():
|
|
||||||
with open(filepath, "rb") as f:
|
with open(filepath, "rb") as f:
|
||||||
conn = tomllib.load(f)
|
conn = tomllib.load(f)
|
||||||
|
self.logger.info(f"loading config from {filepath}")
|
||||||
return conn["connection"] if "connection" in conn else conn
|
return conn["connection"] if "connection" in conn else conn
|
||||||
|
|
||||||
def authenticate(self):
|
def authenticate(self):
|
||||||
@@ -66,10 +96,8 @@ class ObsClient:
|
|||||||
|
|
||||||
auth = base64.b64encode(
|
auth = base64.b64encode(
|
||||||
hashlib.sha256(
|
hashlib.sha256(
|
||||||
(
|
secret
|
||||||
secret.decode()
|
+ self.server_hello["d"]["authentication"]["challenge"].encode()
|
||||||
+ self.server_hello["d"]["authentication"]["challenge"]
|
|
||||||
).encode()
|
|
||||||
).digest()
|
).digest()
|
||||||
).decode()
|
).decode()
|
||||||
|
|
||||||
@@ -78,9 +106,15 @@ class ObsClient:
|
|||||||
self.ws.send(json.dumps(payload))
|
self.ws.send(json.dumps(payload))
|
||||||
try:
|
try:
|
||||||
response = json.loads(self.ws.recv())
|
response = json.loads(self.ws.recv())
|
||||||
return response["op"] == 2
|
if response["op"] != 2:
|
||||||
|
raise OBSSDKError(
|
||||||
|
"failed to identify client with the server, expected response with OpCode 2"
|
||||||
|
)
|
||||||
|
return response["d"]
|
||||||
except json.decoder.JSONDecodeError:
|
except json.decoder.JSONDecodeError:
|
||||||
raise OBSSDKError("failed to identify client with the server")
|
raise OBSSDKError(
|
||||||
|
"failed to identify client with the server, please check connection settings"
|
||||||
|
)
|
||||||
|
|
||||||
def req(self, req_type, req_data=None):
|
def req(self, req_type, req_data=None):
|
||||||
payload = {
|
payload = {
|
||||||
@@ -90,7 +124,11 @@ class ObsClient:
|
|||||||
if req_data:
|
if req_data:
|
||||||
payload["d"]["requestData"] = req_data
|
payload["d"]["requestData"] = req_data
|
||||||
self.logger.debug(f"Sending request {payload}")
|
self.logger.debug(f"Sending request {payload}")
|
||||||
self.ws.send(json.dumps(payload))
|
try:
|
||||||
response = json.loads(self.ws.recv())
|
self.ws.send(json.dumps(payload))
|
||||||
|
response = json.loads(self.ws.recv())
|
||||||
|
except WebSocketTimeoutException as e:
|
||||||
|
self.logger.exception(f"{type(e).__name__}: {e}")
|
||||||
|
raise OBSSDKTimeoutError("Timeout while trying to send the request") from e
|
||||||
self.logger.debug(f"Response received {response}")
|
self.logger.debug(f"Response received {response}")
|
||||||
return response["d"]
|
return response["d"]
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
from typing import Callable, Iterable, Union
|
from collections.abc import Callable, Iterable
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
from .util import as_dataclass, to_camel_case, to_snake_case
|
from .util import as_dataclass, to_camel_case, to_snake_case
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,18 @@
|
|||||||
class OBSSDKError(Exception):
|
class OBSSDKError(Exception):
|
||||||
"""general errors"""
|
"""Base class for OBSSDK errors"""
|
||||||
|
|
||||||
pass
|
|
||||||
|
class OBSSDKTimeoutError(OBSSDKError):
|
||||||
|
"""Exception raised when a connection times out"""
|
||||||
|
|
||||||
|
|
||||||
|
class OBSSDKRequestError(OBSSDKError):
|
||||||
|
"""Exception raised when a request returns an error code"""
|
||||||
|
|
||||||
|
def __init__(self, req_name, code, comment):
|
||||||
|
self.req_name = req_name
|
||||||
|
self.code = code
|
||||||
|
message = f"Request {self.req_name} returned code {self.code}."
|
||||||
|
if comment:
|
||||||
|
message += f" With message: {comment}"
|
||||||
|
super().__init__(message)
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import time
|
import threading
|
||||||
from threading import Thread
|
|
||||||
|
from websocket import WebSocketConnectionClosedException, WebSocketTimeoutException
|
||||||
|
|
||||||
from .baseclient import ObsClient
|
from .baseclient import ObsClient
|
||||||
from .callback import Callback
|
from .callback import Callback
|
||||||
from .enum import Subs
|
from .error import OBSSDKError, OBSSDKTimeoutError
|
||||||
|
from .subs import Subs
|
||||||
|
|
||||||
"""
|
"""
|
||||||
A class to interact with obs-websocket events
|
A class to interact with obs-websocket events
|
||||||
@@ -13,47 +15,77 @@ defined in official github repo
|
|||||||
https://github.com/obsproject/obs-websocket/blob/master/docs/generated/protocol.md#events
|
https://github.com/obsproject/obs-websocket/blob/master/docs/generated/protocol.md#events
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class EventClient:
|
class EventClient:
|
||||||
logger = logging.getLogger("events.eventclient")
|
|
||||||
DELAY = 0.001
|
|
||||||
|
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
|
self.logger = logger.getChild(self.__class__.__name__)
|
||||||
defaultkwargs = {"subs": Subs.LOW_VOLUME}
|
defaultkwargs = {"subs": Subs.LOW_VOLUME}
|
||||||
kwargs = defaultkwargs | kwargs
|
kwargs = defaultkwargs | kwargs
|
||||||
self.base_client = ObsClient(**kwargs)
|
self.base_client = ObsClient(**kwargs)
|
||||||
if self.base_client.authenticate():
|
try:
|
||||||
self.logger.info(f"Successfully identified {self} with the server")
|
success = self.base_client.authenticate()
|
||||||
|
self.logger.info(
|
||||||
|
f"Successfully identified {self} with the server using RPC version:{success['negotiatedRpcVersion']}"
|
||||||
|
)
|
||||||
|
except OBSSDKError as e:
|
||||||
|
self.logger.error(f"{type(e).__name__}: {e}")
|
||||||
|
raise
|
||||||
self.callback = Callback()
|
self.callback = Callback()
|
||||||
self.subscribe()
|
self.subscribe()
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, exc_type, exc_value, exc_traceback):
|
||||||
|
self.disconnect()
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
|
return type(
|
||||||
|
self
|
||||||
|
).__name__ + "(host='{host}', port={port}, password='{password}', subs={subs}, timeout={timeout})".format(
|
||||||
|
**self.base_client.__dict__,
|
||||||
|
)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
return type(self).__name__
|
return type(self).__name__
|
||||||
|
|
||||||
def subscribe(self):
|
def subscribe(self):
|
||||||
worker = Thread(target=self.trigger, daemon=True)
|
self.base_client.ws.settimeout(None)
|
||||||
worker.start()
|
stop_event = threading.Event()
|
||||||
|
self.worker = threading.Thread(
|
||||||
|
target=self.trigger, daemon=True, args=(stop_event,)
|
||||||
|
)
|
||||||
|
self.worker.start()
|
||||||
|
|
||||||
def trigger(self):
|
def trigger(self, stop_event):
|
||||||
"""
|
"""
|
||||||
Continuously listen for events.
|
Continuously listen for events.
|
||||||
|
|
||||||
Triggers a callback on event received.
|
Triggers a callback on event received.
|
||||||
"""
|
"""
|
||||||
self.running = True
|
while not stop_event.is_set():
|
||||||
while self.running:
|
try:
|
||||||
event = json.loads(self.base_client.ws.recv())
|
if response := self.base_client.ws.recv():
|
||||||
self.logger.debug(f"Event received {event}")
|
event = json.loads(response)
|
||||||
type_, data = (
|
self.logger.debug(f"Event received {event}")
|
||||||
event["d"].get("eventType"),
|
type_, data = (
|
||||||
event["d"].get("eventData"),
|
event["d"].get("eventType"),
|
||||||
)
|
event["d"].get("eventData"),
|
||||||
self.callback.trigger(type_, data if data else {})
|
)
|
||||||
time.sleep(self.DELAY)
|
self.callback.trigger(type_, data if data else {})
|
||||||
|
except WebSocketTimeoutException as e:
|
||||||
|
self.logger.exception(f"{type(e).__name__}: {e}")
|
||||||
|
raise OBSSDKTimeoutError("Timeout while waiting for event") from e
|
||||||
|
except (WebSocketConnectionClosedException, OSError) as e:
|
||||||
|
self.logger.debug(f"{type(e).__name__} terminating the event thread")
|
||||||
|
stop_event.set()
|
||||||
|
|
||||||
|
def disconnect(self):
|
||||||
|
"""stop listening for events"""
|
||||||
|
|
||||||
def unsubscribe(self):
|
|
||||||
"""
|
|
||||||
stop listening for events
|
|
||||||
"""
|
|
||||||
self.running = False
|
|
||||||
self.base_client.ws.close()
|
self.base_client.ws.close()
|
||||||
|
self.worker.join()
|
||||||
|
|
||||||
|
unsubscribe = disconnect
|
||||||
|
|||||||
3784
obsws_python/reqs.py
3784
obsws_python/reqs.py
File diff suppressed because it is too large
Load Diff
@@ -1 +1 @@
|
|||||||
version = "1.3.0"
|
version = "1.8.0"
|
||||||
|
|||||||
@@ -7,23 +7,10 @@ name = "obsws-python"
|
|||||||
dynamic = ["version"]
|
dynamic = ["version"]
|
||||||
description = "A Python SDK for OBS Studio WebSocket v5.0"
|
description = "A Python SDK for OBS Studio WebSocket v5.0"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
license = "GPL-3.0-only"
|
license = { text = "GPL-3.0-only" }
|
||||||
requires-python = ">=3.10"
|
requires-python = ">=3.9"
|
||||||
authors = [
|
authors = [{ name = "Adem Atikturk", email = "aatikturk@gmail.com" }]
|
||||||
{ name = "Adem Atikturk", email = "aatikturk@gmail.com" },
|
dependencies = ["tomli >= 2.0.1;python_version < '3.11'", "websocket-client"]
|
||||||
]
|
|
||||||
dependencies = [
|
|
||||||
"tomli >= 2.0.1;python_version < '3.11'",
|
|
||||||
"websocket-client",
|
|
||||||
]
|
|
||||||
|
|
||||||
[project.optional-dependencies]
|
|
||||||
dev = [
|
|
||||||
"black",
|
|
||||||
"isort",
|
|
||||||
"pytest",
|
|
||||||
"pytest-randomly",
|
|
||||||
]
|
|
||||||
|
|
||||||
[project.urls]
|
[project.urls]
|
||||||
Homepage = "https://github.com/aatikturk/obsws-python"
|
Homepage = "https://github.com/aatikturk/obsws-python"
|
||||||
@@ -32,12 +19,64 @@ Homepage = "https://github.com/aatikturk/obsws-python"
|
|||||||
path = "obsws_python/version.py"
|
path = "obsws_python/version.py"
|
||||||
|
|
||||||
[tool.hatch.build.targets.sdist]
|
[tool.hatch.build.targets.sdist]
|
||||||
include = [
|
include = ["/obsws_python"]
|
||||||
"/obsws_python",
|
|
||||||
]
|
[tool.hatch.env]
|
||||||
|
requires = ["hatch-dotenv"]
|
||||||
|
|
||||||
|
[tool.hatch.env.collectors.dotenv.e]
|
||||||
|
env-files = [".env"]
|
||||||
|
# fail-on-missing = true # breaks publish workflow
|
||||||
|
|
||||||
|
[tool.hatch.env.collectors.dotenv.hatch-test]
|
||||||
|
env-files = [".env"]
|
||||||
|
# fail-on-missing = true # breaks publish workflow
|
||||||
|
|
||||||
|
[tool.hatch.envs.default]
|
||||||
|
dependencies = ["pre-commit"]
|
||||||
|
|
||||||
|
[tool.hatch.envs.e]
|
||||||
|
dependencies = ["keyboard"]
|
||||||
|
# env-include = ["OBSWS_*"] # causes issues on Windows.
|
||||||
|
|
||||||
[tool.hatch.envs.e.scripts]
|
[tool.hatch.envs.e.scripts]
|
||||||
events = "py {root}\\examples\\events\\."
|
events = "python -m examples.events"
|
||||||
hotkeys = "py {root}\\examples\\hotkeys\\."
|
hotkeys = "python -m examples.hotkeys"
|
||||||
levels = "py {root}\\examples\\levels\\."
|
levels = "python -m examples.levels"
|
||||||
scene_rotate = "py {root}\\examples\\scene_rotate\\."
|
scene-rotate = "python -m examples.scene_rotate"
|
||||||
|
|
||||||
|
[tool.hatch.envs.hatch-test]
|
||||||
|
randomize = true
|
||||||
|
# env-include = ["OBSWS_*"] # causes issues on Windows.
|
||||||
|
|
||||||
|
[tool.hatch.envs.hatch-test.scripts]
|
||||||
|
run = "pytest{env:HATCH_TEST_ARGS:} {args}"
|
||||||
|
|
||||||
|
[[tool.hatch.envs.hatch-test.matrix]]
|
||||||
|
python = ["313", "312", "311", "310", "39"]
|
||||||
|
|
||||||
|
[tool.hatch.envs.style]
|
||||||
|
detached = true
|
||||||
|
dependencies = ["black", "isort"]
|
||||||
|
|
||||||
|
[tool.hatch.envs.style.scripts]
|
||||||
|
check = ["black --check --diff .", "isort --check-only --diff ."]
|
||||||
|
fmt = ["isort .", "black ."]
|
||||||
|
|
||||||
|
[tool.black]
|
||||||
|
line-length = 88
|
||||||
|
include = '\.pyi?$'
|
||||||
|
# 'extend-exclude' excludes files or directories in addition to the defaults
|
||||||
|
extend-exclude = '''
|
||||||
|
(
|
||||||
|
^/\.git/ # exclude all files in the .git directory
|
||||||
|
^/\.hatch/ # exclude all files in the .hatch directory
|
||||||
|
^/\.pytest_cache/ # exclude all files in the .pytest_cache directory
|
||||||
|
| .*_pb2.py # exclude autogenerated Protocol Buffer files anywhere in the project
|
||||||
|
)
|
||||||
|
'''
|
||||||
|
|
||||||
|
[tool.isort]
|
||||||
|
profile = "black"
|
||||||
|
skip = [".gitignore", ".dockerignore"]
|
||||||
|
skip_glob = [".git/*", ".hatch/*", ".pytest_cache/*"]
|
||||||
|
|||||||
2
setup.py
2
setup.py
@@ -40,7 +40,7 @@ EXTRAS_REQUIRE = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Python version requirement
|
# Python version requirement
|
||||||
PYTHON_REQUIRES = ">=3.10"
|
PYTHON_REQUIRES = ">=3.9"
|
||||||
|
|
||||||
setup(
|
setup(
|
||||||
name=PACKAGE_NAME,
|
name=PACKAGE_NAME,
|
||||||
|
|||||||
@@ -1,16 +1,25 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
import obsws_python as obs
|
import obsws_python as obs
|
||||||
|
|
||||||
req_cl = obs.ReqClient()
|
req_cl = obs.ReqClient(
|
||||||
|
host=os.getenv("OBSWS_HOST", "localhost"),
|
||||||
|
port=int(os.getenv("OBSWS_PORT", 4455)),
|
||||||
|
password=os.getenv("OBSWS_PASSWORD", ""),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def setup_module():
|
def setup_module():
|
||||||
req_cl.create_scene("START_TEST")
|
req_cl.create_scene(scene_name="START_TEST")
|
||||||
req_cl.create_scene("BRB_TEST")
|
req_cl.create_scene(scene_name="BRB_TEST")
|
||||||
req_cl.create_scene("END_TEST")
|
req_cl.create_scene(scene_name="END_TEST")
|
||||||
|
|
||||||
|
|
||||||
def teardown_module():
|
def teardown_module():
|
||||||
req_cl.remove_scene("START_TEST")
|
req_cl.remove_scene(scene_name="START_TEST")
|
||||||
req_cl.remove_scene("BRB_TEST")
|
req_cl.remove_scene(scene_name="BRB_TEST")
|
||||||
req_cl.remove_scene("END_TEST")
|
req_cl.remove_scene(scene_name="END_TEST")
|
||||||
|
resp = req_cl.get_studio_mode_enabled()
|
||||||
|
if resp.studio_mode_enabled:
|
||||||
|
req_cl.set_studio_mode_enabled(studio_mode_enabled=False)
|
||||||
req_cl.base_client.ws.close()
|
req_cl.base_client.ws.close()
|
||||||
|
|||||||
@@ -18,7 +18,12 @@ class TestAttrs:
|
|||||||
|
|
||||||
def test_get_current_program_scene_attrs(self):
|
def test_get_current_program_scene_attrs(self):
|
||||||
resp = req_cl.get_current_program_scene()
|
resp = req_cl.get_current_program_scene()
|
||||||
assert resp.attrs() == ["current_program_scene_name"]
|
assert resp.attrs() == [
|
||||||
|
"current_program_scene_name",
|
||||||
|
"current_program_scene_uuid",
|
||||||
|
"scene_name",
|
||||||
|
"scene_uuid",
|
||||||
|
]
|
||||||
|
|
||||||
def test_get_transition_kind_list_attrs(self):
|
def test_get_transition_kind_list_attrs(self):
|
||||||
resp = req_cl.get_transition_kind_list()
|
resp = req_cl.get_transition_kind_list()
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from obsws_python.callback import Callback
|
from obsws_python.callback import Callback
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
45
tests/test_error.py
Normal file
45
tests/test_error.py
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
import obsws_python as obsws
|
||||||
|
from tests import req_cl
|
||||||
|
|
||||||
|
|
||||||
|
class TestErrors:
|
||||||
|
__test__ = True
|
||||||
|
|
||||||
|
def test_it_raises_an_obssdk_error_on_incorrect_password(self):
|
||||||
|
bad_conn = {
|
||||||
|
"host": os.getenv("OBSWS_HOST", "localhost"),
|
||||||
|
"port": int(os.getenv("OBSWS_PORT", 4455)),
|
||||||
|
"password": "incorrectpassword",
|
||||||
|
}
|
||||||
|
with pytest.raises(
|
||||||
|
obsws.error.OBSSDKError,
|
||||||
|
match="failed to identify client with the server, please check connection settings",
|
||||||
|
):
|
||||||
|
obsws.ReqClient(**bad_conn)
|
||||||
|
|
||||||
|
def test_it_raises_an_obssdk_error_if_auth_enabled_but_no_password_provided(self):
|
||||||
|
bad_conn = {
|
||||||
|
"host": os.getenv("OBSWS_HOST", "localhost"),
|
||||||
|
"port": int(os.getenv("OBSWS_PORT", 4455)),
|
||||||
|
"password": "",
|
||||||
|
}
|
||||||
|
with pytest.raises(
|
||||||
|
obsws.error.OBSSDKError,
|
||||||
|
match="authentication enabled but no password provided",
|
||||||
|
):
|
||||||
|
obsws.ReqClient(**bad_conn)
|
||||||
|
|
||||||
|
def test_it_raises_a_request_error_on_invalid_request(self):
|
||||||
|
with pytest.raises(
|
||||||
|
obsws.error.OBSSDKRequestError,
|
||||||
|
match="Request SetCurrentProgramScene returned code 600. With message: No source was found by the name of `invalid`.",
|
||||||
|
) as exc_info:
|
||||||
|
req_cl.set_current_program_scene(scene_name="invalid")
|
||||||
|
|
||||||
|
e = exc_info.value
|
||||||
|
assert e.req_name == "SetCurrentProgramScene"
|
||||||
|
assert e.code == 600
|
||||||
@@ -11,35 +11,10 @@ class TestRequests:
|
|||||||
assert hasattr(resp, "obs_version")
|
assert hasattr(resp, "obs_version")
|
||||||
assert hasattr(resp, "obs_web_socket_version")
|
assert hasattr(resp, "obs_web_socket_version")
|
||||||
|
|
||||||
def test_get_hot_key_list(self):
|
def test_get_hotkey_list(self):
|
||||||
resp = req_cl.get_hot_key_list()
|
resp = req_cl.get_hotkey_list()
|
||||||
obsbasic_hotkey_list = [
|
assert resp.hotkeys
|
||||||
"OBSBasic.SelectScene",
|
assert any(x.startswith("OBSBasic.") for x in resp.hotkeys)
|
||||||
"OBSBasic.SelectScene",
|
|
||||||
"OBSBasic.SelectScene",
|
|
||||||
"OBSBasic.SelectScene",
|
|
||||||
"OBSBasic.StartStreaming",
|
|
||||||
"OBSBasic.StopStreaming",
|
|
||||||
"OBSBasic.ForceStopStreaming",
|
|
||||||
"OBSBasic.StartRecording",
|
|
||||||
"OBSBasic.StopRecording",
|
|
||||||
"OBSBasic.PauseRecording",
|
|
||||||
"OBSBasic.UnpauseRecording",
|
|
||||||
"OBSBasic.StartReplayBuffer",
|
|
||||||
"OBSBasic.StopReplayBuffer",
|
|
||||||
"OBSBasic.StartVirtualCam",
|
|
||||||
"OBSBasic.StopVirtualCam",
|
|
||||||
"OBSBasic.EnablePreview",
|
|
||||||
"OBSBasic.DisablePreview",
|
|
||||||
"OBSBasic.ShowContextBar",
|
|
||||||
"OBSBasic.HideContextBar",
|
|
||||||
"OBSBasic.TogglePreviewProgram",
|
|
||||||
"OBSBasic.Transition",
|
|
||||||
"OBSBasic.ResetStats",
|
|
||||||
"OBSBasic.Screenshot",
|
|
||||||
"OBSBasic.SelectedSourceScreenshot",
|
|
||||||
]
|
|
||||||
assert all(x in resp.hotkeys for x in obsbasic_hotkey_list)
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"name,data",
|
"name,data",
|
||||||
@@ -49,15 +24,20 @@ class TestRequests:
|
|||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_persistent_data(self, name, data):
|
def test_persistent_data(self, name, data):
|
||||||
req_cl.set_persistent_data("OBS_WEBSOCKET_DATA_REALM_PROFILE", name, data)
|
req_cl.set_persistent_data(
|
||||||
resp = req_cl.get_persistent_data("OBS_WEBSOCKET_DATA_REALM_PROFILE", name)
|
realm="OBS_WEBSOCKET_DATA_REALM_PROFILE", slot_name=name, slot_value=data
|
||||||
|
)
|
||||||
|
resp = req_cl.get_persistent_data(
|
||||||
|
realm="OBS_WEBSOCKET_DATA_REALM_PROFILE", slot_name=name
|
||||||
|
)
|
||||||
assert resp.slot_value == data
|
assert resp.slot_value == data
|
||||||
|
|
||||||
|
@pytest.mark.skip(reason="possible bug in obs-websocket, needs checking")
|
||||||
def test_profile_list(self):
|
def test_profile_list(self):
|
||||||
req_cl.create_profile("test")
|
req_cl.create_profile(profile_name="test")
|
||||||
resp = req_cl.get_profile_list()
|
resp = req_cl.get_profile_list()
|
||||||
assert "test" in resp.profiles
|
assert "test" in resp.profiles
|
||||||
req_cl.remove_profile("test")
|
req_cl.remove_profile(profile_name="test")
|
||||||
resp = req_cl.get_profile_list()
|
resp = req_cl.get_profile_list()
|
||||||
assert "test" not in resp.profiles
|
assert "test" not in resp.profiles
|
||||||
|
|
||||||
@@ -67,8 +47,8 @@ class TestRequests:
|
|||||||
"key": "live_myvery_secretkey",
|
"key": "live_myvery_secretkey",
|
||||||
}
|
}
|
||||||
req_cl.set_stream_service_settings(
|
req_cl.set_stream_service_settings(
|
||||||
"rtmp_common",
|
stream_service_type="rtmp_common",
|
||||||
settings,
|
stream_service_settings=settings,
|
||||||
)
|
)
|
||||||
resp = req_cl.get_stream_service_settings()
|
resp = req_cl.get_stream_service_settings()
|
||||||
assert resp.stream_service_type == "rtmp_common"
|
assert resp.stream_service_type == "rtmp_common"
|
||||||
@@ -86,28 +66,40 @@ class TestRequests:
|
|||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_current_program_scene(self, scene):
|
def test_current_program_scene(self, scene):
|
||||||
req_cl.set_current_program_scene(scene)
|
req_cl.set_current_program_scene(scene_name=scene)
|
||||||
resp = req_cl.get_current_program_scene()
|
resp = req_cl.get_current_program_scene()
|
||||||
assert resp.current_program_scene_name == scene
|
assert resp.current_program_scene_name == scene
|
||||||
|
|
||||||
def test_input_list(self):
|
def test_input_list(self):
|
||||||
req_cl.create_input(
|
req_cl.create_input(
|
||||||
"START_TEST", "test", "color_source_v3", {"color": 4294945535}, True
|
scene_name="START_TEST",
|
||||||
|
input_name="test",
|
||||||
|
input_kind="color_source_v3",
|
||||||
|
input_settings={"color": 4294945535},
|
||||||
|
scene_item_enabled=True,
|
||||||
)
|
)
|
||||||
resp = req_cl.get_input_list()
|
resp = req_cl.get_input_list()
|
||||||
assert {
|
for input_item in resp.inputs:
|
||||||
"inputKind": "color_source_v3",
|
if input_item["inputName"] == "test":
|
||||||
"inputName": "test",
|
assert input_item["inputKind"] == "color_source_v3"
|
||||||
"unversionedInputKind": "color_source",
|
assert input_item["unversionedInputKind"] == "color_source"
|
||||||
} in resp.inputs
|
break
|
||||||
resp = req_cl.get_input_settings("test")
|
else:
|
||||||
|
# This else block is executed if the for loop completes without finding the input_item with inputName "test"
|
||||||
|
raise AssertionError("Input with inputName 'test' not found")
|
||||||
|
|
||||||
|
resp = req_cl.get_input_settings(input_name="test")
|
||||||
assert resp.input_kind == "color_source_v3"
|
assert resp.input_kind == "color_source_v3"
|
||||||
assert resp.input_settings == {"color": 4294945535}
|
assert resp.input_settings == {"color": 4294945535}
|
||||||
req_cl.remove_input("test")
|
req_cl.remove_input(input_name="test")
|
||||||
|
|
||||||
def test_source_filter(self):
|
def test_source_filter(self):
|
||||||
req_cl.create_source_filter("START_TEST", "test", "color_key_filter_v2")
|
req_cl.create_source_filter(
|
||||||
resp = req_cl.get_source_filter_list("START_TEST")
|
source_name="START_TEST",
|
||||||
|
filter_name="test",
|
||||||
|
filter_kind="color_key_filter_v2",
|
||||||
|
)
|
||||||
|
resp = req_cl.get_source_filter_list(source_name="START_TEST")
|
||||||
assert resp.filters == [
|
assert resp.filters == [
|
||||||
{
|
{
|
||||||
"filterEnabled": True,
|
"filterEnabled": True,
|
||||||
@@ -117,7 +109,7 @@ class TestRequests:
|
|||||||
"filterSettings": {},
|
"filterSettings": {},
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
req_cl.remove_source_filter("START_TEST", "test")
|
req_cl.remove_source_filter(source_name="START_TEST", filter_name="test")
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"state",
|
"state",
|
||||||
@@ -127,6 +119,6 @@ class TestRequests:
|
|||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_studio_mode_enabled(self, state):
|
def test_studio_mode_enabled(self, state):
|
||||||
req_cl.set_studio_mode_enabled(state)
|
req_cl.set_studio_mode_enabled(studio_mode_enabled=state)
|
||||||
resp = req_cl.get_studio_mode_enabled()
|
resp = req_cl.get_studio_mode_enabled()
|
||||||
assert resp.studio_mode_enabled == state
|
assert resp.studio_mode_enabled == state
|
||||||
|
|||||||
Reference in New Issue
Block a user