mirror of
				https://github.com/onyx-and-iris/obsws-python.git
				synced 2025-11-03 22:31:48 +00:00 
			
		
		
		
	Compare commits
	
		
			No commits in common. "4e45de17ea998c3eca0034f956c4ad63a1bcc16d" and "9c41f2bb593c20fa4fe558b802b6511d8585f22c" have entirely different histories.
		
	
	
		
			4e45de17ea
			...
			9c41f2bb59
		
	
		
							
								
								
									
										5
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										5
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@ -47,7 +47,4 @@ venv.bak/
 | 
			
		||||
 | 
			
		||||
# Test/config
 | 
			
		||||
quick.py
 | 
			
		||||
config.toml
 | 
			
		||||
obsws.log
 | 
			
		||||
 | 
			
		||||
.vscode/
 | 
			
		||||
config.toml
 | 
			
		||||
@ -27,7 +27,6 @@ By default the clients connect with parameters:
 | 
			
		||||
-   `host`: "localhost"
 | 
			
		||||
-   `port`: 4455
 | 
			
		||||
-   `password`: ""
 | 
			
		||||
-   `timeout`: None
 | 
			
		||||
 | 
			
		||||
You may override these parameters by storing them in a toml config file or passing them as keyword arguments.
 | 
			
		||||
 | 
			
		||||
@ -54,7 +53,7 @@ Example `__main__.py`:
 | 
			
		||||
import obsws_python as obs
 | 
			
		||||
 | 
			
		||||
# pass conn info if not in config.toml
 | 
			
		||||
cl = obs.ReqClient(host='localhost', port=4455, password='mystrongpass', timeout=3)
 | 
			
		||||
cl = obs.ReqClient(host='localhost', port=4455, password='mystrongpass')
 | 
			
		||||
 | 
			
		||||
# Toggle the mute state of your Mic input
 | 
			
		||||
cl.toggle_input_mute('Mic/Aux')
 | 
			
		||||
@ -132,8 +131,6 @@ If a request fails an `OBSSDKError` will be raised with a status code.
 | 
			
		||||
 | 
			
		||||
For a full list of status codes refer to [Codes](https://github.com/obsproject/obs-websocket/blob/master/docs/generated/protocol.md#requeststatus)
 | 
			
		||||
 | 
			
		||||
If a timeout occurs during sending/receiving a request or receiving an event an `OBSSDKTimeoutError` will be raised.
 | 
			
		||||
 | 
			
		||||
### Logging
 | 
			
		||||
 | 
			
		||||
If you want to see the raw messages simply set log level to DEBUG
 | 
			
		||||
 | 
			
		||||
@ -7,22 +7,15 @@ from random import randint
 | 
			
		||||
from typing import Optional
 | 
			
		||||
 | 
			
		||||
import websocket
 | 
			
		||||
from websocket import WebSocketTimeoutException
 | 
			
		||||
 | 
			
		||||
from .error import OBSSDKError, OBSSDKTimeoutError
 | 
			
		||||
from .error import OBSSDKError
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class ObsClient:
 | 
			
		||||
    logger = logging.getLogger("baseclient.obsclient")
 | 
			
		||||
 | 
			
		||||
    def __init__(self, **kwargs):
 | 
			
		||||
        defaultkwargs = {
 | 
			
		||||
            "host": "localhost",
 | 
			
		||||
            "port": 4455,
 | 
			
		||||
            "password": "",
 | 
			
		||||
            "subs": 0,
 | 
			
		||||
            "timeout": None,
 | 
			
		||||
        }
 | 
			
		||||
        defaultkwargs = {"host": "localhost", "port": 4455, "password": "", "subs": 0}
 | 
			
		||||
        if not any(key in kwargs for key in ("host", "port", "password")):
 | 
			
		||||
            kwargs |= self._conn_from_toml()
 | 
			
		||||
        kwargs = defaultkwargs | kwargs
 | 
			
		||||
@ -30,21 +23,14 @@ class ObsClient:
 | 
			
		||||
            setattr(self, attr, val)
 | 
			
		||||
 | 
			
		||||
        self.logger.info(
 | 
			
		||||
            "Connecting with parameters: host='{host}' port={port} password='{password}' subs={subs} timeout={timeout}".format(
 | 
			
		||||
            "Connecting with parameters: host='{host}' port={port} password='{password}' subs={subs}".format(
 | 
			
		||||
                **self.__dict__
 | 
			
		||||
            )
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
        try:
 | 
			
		||||
            self.ws = websocket.WebSocket()
 | 
			
		||||
            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, WebSocketTimeoutException) as e:
 | 
			
		||||
            self.logger.exception(f"{type(e).__name__}: {e}")
 | 
			
		||||
            raise
 | 
			
		||||
        self.ws = websocket.WebSocket()
 | 
			
		||||
        self.ws.connect(f"ws://{self.host}:{self.port}")
 | 
			
		||||
        self.server_hello = json.loads(self.ws.recv())
 | 
			
		||||
 | 
			
		||||
    def _conn_from_toml(self) -> dict:
 | 
			
		||||
        try:
 | 
			
		||||
@ -119,11 +105,7 @@ class ObsClient:
 | 
			
		||||
        if req_data:
 | 
			
		||||
            payload["d"]["requestData"] = req_data
 | 
			
		||||
        self.logger.debug(f"Sending request {payload}")
 | 
			
		||||
        try:
 | 
			
		||||
            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.ws.send(json.dumps(payload))
 | 
			
		||||
        response = json.loads(self.ws.recv())
 | 
			
		||||
        self.logger.debug(f"Response received {response}")
 | 
			
		||||
        return response["d"]
 | 
			
		||||
 | 
			
		||||
@ -1,6 +1,4 @@
 | 
			
		||||
class OBSSDKError(Exception):
 | 
			
		||||
    """Exception raised when general errors occur"""
 | 
			
		||||
    """general errors"""
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class OBSSDKTimeoutError(Exception):
 | 
			
		||||
    """Exception raised when a connection times out"""
 | 
			
		||||
    pass
 | 
			
		||||
 | 
			
		||||
@ -3,11 +3,8 @@ import logging
 | 
			
		||||
import time
 | 
			
		||||
from threading import Thread
 | 
			
		||||
 | 
			
		||||
from websocket import WebSocketTimeoutException
 | 
			
		||||
 | 
			
		||||
from .baseclient import ObsClient
 | 
			
		||||
from .callback import Callback
 | 
			
		||||
from .error import OBSSDKTimeoutError
 | 
			
		||||
from .subs import Subs
 | 
			
		||||
 | 
			
		||||
"""
 | 
			
		||||
@ -33,7 +30,7 @@ class EventClient:
 | 
			
		||||
    def __repr__(self):
 | 
			
		||||
        return type(
 | 
			
		||||
            self
 | 
			
		||||
        ).__name__ + "(host='{host}', port={port}, password='{password}', subs={subs}, timeout={timeout})".format(
 | 
			
		||||
        ).__name__ + "(host='{host}', port={port}, password='{password}', subs={subs})".format(
 | 
			
		||||
            **self.base_client.__dict__,
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
@ -52,11 +49,7 @@ class EventClient:
 | 
			
		||||
        """
 | 
			
		||||
        self.running = True
 | 
			
		||||
        while self.running:
 | 
			
		||||
            try:
 | 
			
		||||
                event = json.loads(self.base_client.ws.recv())
 | 
			
		||||
            except WebSocketTimeoutException as e:
 | 
			
		||||
                self.logger.exception(f"{type(e).__name__}: {e}")
 | 
			
		||||
                raise OBSSDKTimeoutError("Timeout while waiting for event") from e
 | 
			
		||||
            event = json.loads(self.base_client.ws.recv())
 | 
			
		||||
            self.logger.debug(f"Event received {event}")
 | 
			
		||||
            type_, data = (
 | 
			
		||||
                event["d"].get("eventType"),
 | 
			
		||||
 | 
			
		||||
@ -28,7 +28,7 @@ class ReqClient:
 | 
			
		||||
    def __repr__(self):
 | 
			
		||||
        return type(
 | 
			
		||||
            self
 | 
			
		||||
        ).__name__ + "(host='{host}', port={port}, password='{password}', timeout={timeout})".format(
 | 
			
		||||
        ).__name__ + "(host='{host}', port={port}, password='{password}')".format(
 | 
			
		||||
            **self.base_client.__dict__,
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -1 +1 @@
 | 
			
		||||
version = "1.5.0"
 | 
			
		||||
version = "1.4.2"
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user