mirror of
https://github.com/onyx-and-iris/obsws-python.git
synced 2026-04-05 16:39:10 +00:00
update examples to work with modified req client
examples now expect env variables
This commit is contained in:
parent
7126a2efe0
commit
fe9f305afe
@ -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,6 @@ class Observer:
|
|||||||
]
|
]
|
||||||
)
|
)
|
||||||
print(f"Registered events: {self._client.callback.get()}")
|
print(f"Registered events: {self._client.callback.get()}")
|
||||||
self.running = True
|
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
return self
|
return self
|
||||||
@ -38,10 +39,15 @@ 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("OBS closing!")
|
print("OBS closing!")
|
||||||
self.running = False
|
self._stop_event.set()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
with Observer() as observer:
|
host = os.getenv("OBSWS_HOST", "localhost")
|
||||||
while observer.running:
|
port = int(os.getenv("OBSWS_PORT", 4455))
|
||||||
time.sleep(0.1)
|
password = os.getenv("OBSWS_PASSWORD", "")
|
||||||
|
|
||||||
|
stop_event = Event()
|
||||||
|
|
||||||
|
with Observer(host, port, password, stop_event) as observer:
|
||||||
|
stop_event.wait()
|
||||||
|
|||||||
@ -1,13 +1,14 @@
|
|||||||
import inspect
|
import inspect
|
||||||
|
import os
|
||||||
|
|
||||||
import keyboard
|
import keyboard # type: ignore
|
||||||
|
|
||||||
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()}")
|
||||||
|
|
||||||
@ -34,12 +35,16 @@ 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__":
|
||||||
with obs.ReqClient() as req_client:
|
host = os.getenv("OBSWS_HOST", "localhost")
|
||||||
with Observer() as observer:
|
port = int(os.getenv("OBSWS_PORT", 4455))
|
||||||
|
password = os.getenv("OBSWS_PASSWORD", "")
|
||||||
|
|
||||||
|
with obs.ReqClient(host=host, port=port, password=password) as req_client:
|
||||||
|
with Observer(host, port, password) as observer:
|
||||||
keyboard.add_hotkey("0", version)
|
keyboard.add_hotkey("0", version)
|
||||||
keyboard.add_hotkey("1", set_scene, args=("START",))
|
keyboard.add_hotkey("1", set_scene, args=("START",))
|
||||||
keyboard.add_hotkey("2", set_scene, args=("BRB",))
|
keyboard.add_hotkey("2", set_scene, args=("BRB",))
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
import os
|
||||||
from enum import IntEnum
|
from enum import IntEnum
|
||||||
from math import log
|
from math import log
|
||||||
|
|
||||||
@ -34,8 +35,15 @@ def on_input_volume_meters(data):
|
|||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
host = os.getenv("OBSWS_HOST", "localhost")
|
||||||
|
port = int(os.getenv("OBSWS_PORT", 4455))
|
||||||
|
password = os.getenv("OBSWS_PASSWORD", "")
|
||||||
|
|
||||||
with obs.EventClient(
|
with obs.EventClient(
|
||||||
subs=(obs.Subs.LOW_VOLUME | obs.Subs.INPUTVOLUMEMETERS)
|
host=host,
|
||||||
|
port=port,
|
||||||
|
password=password,
|
||||||
|
subs=(obs.Subs.LOW_VOLUME | obs.Subs.INPUTVOLUMEMETERS),
|
||||||
) as client:
|
) as client:
|
||||||
client.callback.register([on_input_volume_meters, on_input_mute_state_changed])
|
client.callback.register([on_input_volume_meters, on_input_mute_state_changed])
|
||||||
|
|
||||||
|
|||||||
@ -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)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -24,25 +24,30 @@ include = ["/obsws_python"]
|
|||||||
[tool.hatch.env]
|
[tool.hatch.env]
|
||||||
requires = ["hatch-dotenv"]
|
requires = ["hatch-dotenv"]
|
||||||
|
|
||||||
|
[tool.hatch.env.collectors.dotenv.e]
|
||||||
|
env-files = [".env"]
|
||||||
|
fail-on-missing = true
|
||||||
|
|
||||||
|
[tool.hatch.env.collectors.dotenv.hatch-test]
|
||||||
|
env-files = [".env"]
|
||||||
|
fail-on-missing = true
|
||||||
|
|
||||||
[tool.hatch.envs.default]
|
[tool.hatch.envs.default]
|
||||||
dependencies = ["pre-commit"]
|
dependencies = ["pre-commit"]
|
||||||
|
|
||||||
[tool.hatch.envs.e]
|
[tool.hatch.envs.e]
|
||||||
dependencies = ["keyboard"]
|
dependencies = ["keyboard"]
|
||||||
|
env-include = ["OBSWS_*"]
|
||||||
|
|
||||||
[tool.hatch.envs.e.scripts]
|
[tool.hatch.envs.e.scripts]
|
||||||
events = "python {root}\\examples\\events\\."
|
events = "python -m examples.events"
|
||||||
hotkeys = "python {root}\\examples\\hotkeys\\."
|
hotkeys = "python -m examples.hotkeys"
|
||||||
levels = "python {root}\\examples\\levels\\."
|
levels = "python -m examples.levels"
|
||||||
scene_rotate = "python {root}\\examples\\scene_rotate\\."
|
scene-rotate = "python -m examples.scene_rotate"
|
||||||
|
|
||||||
[tool.hatch.envs.hatch-test]
|
[tool.hatch.envs.hatch-test]
|
||||||
randomize = true
|
randomize = true
|
||||||
env-include = ["OBSWS_TEST_*"]
|
env-include = ["OBSWS_*"]
|
||||||
|
|
||||||
[tool.hatch.env.collectors.dotenv.hatch-test]
|
|
||||||
env-files = [".env"]
|
|
||||||
fail-on-missing = true
|
|
||||||
|
|
||||||
[tool.hatch.envs.hatch-test.scripts]
|
[tool.hatch.envs.hatch-test.scripts]
|
||||||
run = "pytest{env:HATCH_TEST_ARGS:} {args}"
|
run = "pytest{env:HATCH_TEST_ARGS:} {args}"
|
||||||
|
|||||||
@ -3,9 +3,9 @@ import os
|
|||||||
import obsws_python as obs
|
import obsws_python as obs
|
||||||
|
|
||||||
req_cl = obs.ReqClient(
|
req_cl = obs.ReqClient(
|
||||||
host=os.getenv("OBSWS_TEST_HOST", "localhost"),
|
host=os.getenv("OBSWS_HOST", "localhost"),
|
||||||
port=int(os.getenv("OBSWS_TEST_PORT", 4455)),
|
port=int(os.getenv("OBSWS_PORT", 4455)),
|
||||||
password=os.getenv("OBSWS_TEST_PASSWORD", ""),
|
password=os.getenv("OBSWS_PASSWORD", ""),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -11,8 +11,8 @@ class TestErrors:
|
|||||||
|
|
||||||
def test_it_raises_an_obssdk_error_on_incorrect_password(self):
|
def test_it_raises_an_obssdk_error_on_incorrect_password(self):
|
||||||
bad_conn = {
|
bad_conn = {
|
||||||
"host": os.getenv("OBSWS_TEST_HOST", "localhost"),
|
"host": os.getenv("OBSWS_HOST", "localhost"),
|
||||||
"port": int(os.getenv("OBSWS_TEST_PORT", 4455)),
|
"port": int(os.getenv("OBSWS_PORT", 4455)),
|
||||||
"password": "incorrectpassword",
|
"password": "incorrectpassword",
|
||||||
}
|
}
|
||||||
with pytest.raises(
|
with pytest.raises(
|
||||||
@ -23,8 +23,8 @@ class TestErrors:
|
|||||||
|
|
||||||
def test_it_raises_an_obssdk_error_if_auth_enabled_but_no_password_provided(self):
|
def test_it_raises_an_obssdk_error_if_auth_enabled_but_no_password_provided(self):
|
||||||
bad_conn = {
|
bad_conn = {
|
||||||
"host": os.getenv("OBSWS_TEST_HOST", "localhost"),
|
"host": os.getenv("OBSWS_HOST", "localhost"),
|
||||||
"port": int(os.getenv("OBSWS_TEST_PORT", 4455)),
|
"port": int(os.getenv("OBSWS_PORT", 4455)),
|
||||||
"password": "",
|
"password": "",
|
||||||
}
|
}
|
||||||
with pytest.raises(
|
with pytest.raises(
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user