2022-11-13 10:07:08 +00:00
|
|
|
import logging
|
|
|
|
from pathlib import Path
|
2025-01-20 13:32:11 +00:00
|
|
|
from typing import Any, Union
|
2022-11-13 10:07:08 +00:00
|
|
|
|
|
|
|
import socketio
|
|
|
|
from observable import Observable
|
|
|
|
|
2025-01-20 13:32:11 +00:00
|
|
|
from .error import SteamlabsSIOConnectionError, SteamlabsSIOError
|
2022-11-13 10:07:08 +00:00
|
|
|
from .models import as_dataclass
|
|
|
|
|
2023-06-28 02:45:04 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
2022-11-13 10:07:08 +00:00
|
|
|
|
|
|
|
|
2023-06-28 02:45:04 +01:00
|
|
|
class Client:
|
|
|
|
def __init__(self, token=None, raw=False):
|
|
|
|
self.logger = logger.getChild(self.__class__.__name__)
|
2022-11-13 10:07:08 +00:00
|
|
|
self.token = token or self._token_from_toml()
|
2023-06-28 02:45:04 +01:00
|
|
|
self._raw = raw
|
2022-11-13 10:07:08 +00:00
|
|
|
self.sio = socketio.Client()
|
2025-01-20 13:32:11 +00:00
|
|
|
self.sio.on('connect', self.connect_handler)
|
|
|
|
self.sio.on('event', self.event_handler)
|
|
|
|
self.sio.on('disconnect', self.disconnect_handler)
|
2022-11-13 10:07:08 +00:00
|
|
|
self.obs = Observable()
|
2025-01-20 13:32:11 +00:00
|
|
|
self.event_types: set[str] = (
|
|
|
|
{'donation'} # streamlabs
|
|
|
|
| {'follow', 'subscription', 'host', 'bits', 'raid'} # twitch
|
|
|
|
| {'follow', 'subscription', 'superchat'} # youtube
|
|
|
|
)
|
2022-11-13 10:07:08 +00:00
|
|
|
|
|
|
|
def __enter__(self):
|
2023-06-28 02:45:04 +01:00
|
|
|
try:
|
2025-01-20 13:32:11 +00:00
|
|
|
self.sio.connect(f'https://sockets.streamlabs.com?token={self.token}')
|
2023-06-28 02:45:04 +01:00
|
|
|
except socketio.exceptions.ConnectionError as e:
|
2025-01-20 13:32:11 +00:00
|
|
|
self.logger.exception(f'{type(e).__name__}: {e}')
|
2023-06-28 02:45:04 +01:00
|
|
|
raise SteamlabsSIOConnectionError(
|
2025-01-20 13:32:11 +00:00
|
|
|
'no connection could be established to the Streamlabs SIO server'
|
2023-06-28 02:45:04 +01:00
|
|
|
) from e
|
2023-08-19 21:38:32 +01:00
|
|
|
self.log_mode()
|
2022-11-13 10:07:08 +00:00
|
|
|
return self
|
|
|
|
|
2025-01-20 13:32:11 +00:00
|
|
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
|
|
self.sio.disconnect()
|
|
|
|
|
2023-08-19 21:38:32 +01:00
|
|
|
@property
|
2025-01-20 13:32:11 +00:00
|
|
|
def raw(self) -> bool:
|
2023-08-19 21:38:32 +01:00
|
|
|
return self._raw
|
|
|
|
|
|
|
|
@raw.setter
|
2025-01-20 13:32:11 +00:00
|
|
|
def raw(self, val: bool) -> None:
|
2023-08-19 21:38:32 +01:00
|
|
|
self._raw = val
|
|
|
|
self.log_mode()
|
|
|
|
|
|
|
|
def log_mode(self):
|
2025-01-20 13:32:11 +00:00
|
|
|
info = (
|
|
|
|
'Raw mode' if self.raw else 'Normal mode',
|
|
|
|
'activated.',
|
|
|
|
'JSON messages' if self.raw else 'Event objects',
|
|
|
|
'will be passed to callbacks.',
|
|
|
|
)
|
|
|
|
self.logger.info(' '.join(info))
|
2023-08-19 21:38:32 +01:00
|
|
|
|
2022-11-13 10:07:08 +00:00
|
|
|
def _token_from_toml(self) -> str:
|
2025-01-20 13:32:11 +00:00
|
|
|
"""
|
|
|
|
Retrieves the Streamlabs token from a TOML configuration file.
|
|
|
|
This method attempts to load the token from a 'config.toml' file located
|
|
|
|
either in the current working directory or in the user's home configuration
|
|
|
|
directory under '.config/streamlabsio/'.
|
|
|
|
Returns:
|
|
|
|
str: The Streamlabs token retrieved from the TOML configuration file.
|
|
|
|
Raises:
|
|
|
|
SteamlabsSIOError: If no configuration file is found, if the file cannot
|
|
|
|
be decoded, or if the required 'streamlabs' section or 'token' key is
|
|
|
|
missing from the configuration file.
|
|
|
|
"""
|
|
|
|
|
2023-06-28 02:45:04 +01:00
|
|
|
try:
|
|
|
|
import tomllib
|
|
|
|
except ModuleNotFoundError:
|
|
|
|
import tomli as tomllib
|
|
|
|
|
2025-01-20 13:32:11 +00:00
|
|
|
def get_filepath() -> Union[Path, None]:
|
2023-06-28 02:45:04 +01:00
|
|
|
filepaths = (
|
2025-01-20 13:32:11 +00:00
|
|
|
Path.cwd() / 'config.toml',
|
|
|
|
Path.home() / '.config' / 'streamlabsio' / 'config.toml',
|
2023-06-28 02:45:04 +01:00
|
|
|
)
|
|
|
|
for filepath in filepaths:
|
|
|
|
if filepath.exists():
|
|
|
|
return filepath
|
2025-01-20 13:32:11 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
filepath = get_filepath()
|
|
|
|
if not filepath:
|
|
|
|
raise SteamlabsSIOError('no token provided and no config.toml file found')
|
2023-06-28 02:45:04 +01:00
|
|
|
|
|
|
|
try:
|
2025-01-20 13:32:11 +00:00
|
|
|
with open(filepath, 'rb') as f:
|
2023-06-28 02:45:04 +01:00
|
|
|
conn = tomllib.load(f)
|
2025-01-20 13:32:11 +00:00
|
|
|
except tomllib.TOMLDecodeError as e:
|
|
|
|
ERR_MSG = f'Error decoding {filepath}: {e}'
|
|
|
|
self.logger.exception(ERR_MSG)
|
|
|
|
raise SteamlabsSIOError(ERR_MSG) from e
|
|
|
|
|
|
|
|
if 'streamlabs' not in conn or 'token' not in conn['streamlabs']:
|
|
|
|
ERR_MSG = (
|
|
|
|
'config.toml does not contain a "streamlabs" section '
|
|
|
|
'or the "streamlabs" section does not contain a "token" key'
|
|
|
|
)
|
|
|
|
self.logger.exception(ERR_MSG)
|
|
|
|
raise SteamlabsSIOError(ERR_MSG)
|
|
|
|
|
|
|
|
return conn['streamlabs']['token']
|
|
|
|
|
|
|
|
def connect_handler(self) -> None:
|
|
|
|
self.logger.info('Connected to Streamlabs Socket API')
|
|
|
|
|
|
|
|
def event_handler(self, data: Any) -> None:
|
|
|
|
"""
|
|
|
|
Handles incoming events and triggers corresponding OBS actions.
|
|
|
|
Args:
|
|
|
|
data (dict): The event data containing information about the event.
|
|
|
|
Expected keys:
|
|
|
|
- 'for': The target of the event.
|
|
|
|
- 'type': The type of the event.
|
|
|
|
- 'message': A list containing the event message.
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
"""
|
|
|
|
|
|
|
|
if 'for' in data and data['type'] in self.event_types:
|
|
|
|
message = data['message'][0]
|
2022-11-14 19:29:08 +00:00
|
|
|
self.obs.trigger(
|
2025-01-20 13:32:11 +00:00
|
|
|
data['for'],
|
|
|
|
data['type'],
|
|
|
|
message if self.raw else as_dataclass(data['type'], message),
|
2022-11-14 19:29:08 +00:00
|
|
|
)
|
2023-06-28 02:45:04 +01:00
|
|
|
self.logger.debug(data)
|
2022-11-13 10:07:08 +00:00
|
|
|
|
2025-01-20 13:32:11 +00:00
|
|
|
def disconnect_handler(self) -> None:
|
|
|
|
self.logger.info('Disconnected from Streamlabs Socket API')
|
2022-11-13 10:07:08 +00:00
|
|
|
|
|
|
|
|
2025-01-20 13:32:11 +00:00
|
|
|
def connect(**kwargs) -> Client:
|
2022-11-13 10:07:08 +00:00
|
|
|
SIO_cls = Client
|
|
|
|
return SIO_cls(**kwargs)
|