guard against unwanted events

some rewording in readme

patch bump
This commit is contained in:
onyx-and-iris 2022-11-14 19:29:08 +00:00
parent 466e06f14a
commit 37ca764806
3 changed files with 21 additions and 13 deletions

View File

@ -3,11 +3,11 @@
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black) [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
[![Imports: isort](https://img.shields.io/badge/%20imports-isort-%231674b1?style=flat&labelColor=ef8336)](https://pycqa.github.io/isort/) [![Imports: isort](https://img.shields.io/badge/%20imports-isort-%231674b1?style=flat&labelColor=ef8336)](https://pycqa.github.io/isort/)
# A Python client for Streamlabs SocketIO API # A Python client for Streamlabs Socket API
### Requirements ### Requirements
- A Streamlabs SocketIO API key. - A Streamlabs Socket API key.
- You can acquire this by logging into your Streamlabs.com dashboard then `Settings->Api Settings->API Tokens` - You can acquire this by logging into your Streamlabs.com dashboard then `Settings->Api Settings->API Tokens`
### How to install using pip ### How to install using pip
@ -54,7 +54,9 @@ if __name__ == "__main__":
main() main()
``` ```
note. From the [SocketIO docs](https://python-socketio.readthedocs.io/en/latest/client.html#managing-background-tasks), `client.sio.wait()` may be used if your application has nothing to do in the main thread. #### note
From the [SocketIO docs](https://python-socketio.readthedocs.io/en/latest/client.html#managing-background-tasks), `client.sio.wait()` may be used if your application has nothing to do in the main thread.
### Attributes ### Attributes
@ -69,4 +71,4 @@ def on_twitch_event(event, msg):
### Official Documentation ### Official Documentation
- [Streamlabs SocketIO API](https://dev.streamlabs.com/docs/socket-api) - [Streamlabs Socket API](https://dev.streamlabs.com/docs/socket-api)

View File

@ -1,6 +1,6 @@
[tool.poetry] [tool.poetry]
name = "streamlabsio" name = "streamlabsio"
version = "0.1.2" version = "0.1.3"
description = "Get real time Twitch/Youtube events through Streamlabs SocketIO API" description = "Get real time Twitch/Youtube events through Streamlabs SocketIO API"
authors = ["onyx-and-iris <code@onyxandiris.online>"] authors = ["onyx-and-iris <code@onyxandiris.online>"]
license = "MIT" license = "MIT"

View File

@ -13,7 +13,7 @@ from .models import as_dataclass
class Client: class Client:
logger = logging.getLogger("socketio.socketio") logger = logging.getLogger("socketio.client")
def __init__(self, token=None): def __init__(self, token=None):
self.token = token or self._token_from_toml() self.token = token or self._token_from_toml()
@ -22,6 +22,9 @@ class Client:
self.sio.on("event", self.event_handler) self.sio.on("event", self.event_handler)
self.sio.on("disconnect", self.disconnect_handler) self.sio.on("disconnect", self.disconnect_handler)
self.obs = Observable() self.obs = Observable()
self.streamlabs = ("donation",)
self.twitch = ("follow", "subscription", "host", "bits", "raids")
self.youtube = ("follow", "subscription", "superchat")
def __enter__(self): def __enter__(self):
self.sio.connect(f"https://sockets.streamlabs.com?token={self.token}") self.sio.connect(f"https://sockets.streamlabs.com?token={self.token}")
@ -35,17 +38,20 @@ class Client:
return conn["streamlabs"].get("token") return conn["streamlabs"].get("token")
def connect_handler(self): def connect_handler(self):
self.logger.info("Connected to Twitch Socketio") self.logger.info("Connected to Streamlabs Socket API")
def event_handler(self, data): def event_handler(self, data):
self.obs.trigger( if "for" in data and data["type"] in set(
data.get("for"), self.streamlabs + self.twitch + self.youtube
data["type"], ):
as_dataclass(data["type"], *data["message"]), self.obs.trigger(
) data["for"],
data["type"],
as_dataclass(data["type"], *data["message"]),
)
def disconnect_handler(self): def disconnect_handler(self):
self.logger.info("Disconnected from Twitch Socketio") self.logger.info("Disconnected from Streamlabs Socket API")
def __exit__(self, exc_type, exc_val, exc_tb): def __exit__(self, exc_type, exc_val, exc_tb):
self.sio.disconnect() self.sio.disconnect()