2022-11-13 10:13:52 +00:00
[![PyPI version ](https://badge.fury.io/py/streamlabsio.svg )](https://badge.fury.io/py/streamlabsio)
2022-11-13 10:39:28 +00:00
[![License: MIT ](https://img.shields.io/badge/License-MIT-yellow.svg )](https://github.com/onyx-and-iris/streamlabs-socketio-py/blob/dev/LICENSE)
2023-08-19 21:41:04 +01:00
[![Poetry ](https://img.shields.io/endpoint?url=https://python-poetry.org/badge/v0.json )](https://python-poetry.org/)
2022-11-13 10:07:08 +00:00
[![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/)
2022-11-14 19:29:08 +00:00
# A Python client for Streamlabs Socket API
2022-11-13 10:07:08 +00:00
2023-06-28 03:39:51 +01:00
For an outline of past/future changes refer to: [CHANGELOG ](CHANGELOG.md )
2022-11-13 10:07:08 +00:00
### Requirements
2022-11-14 19:29:08 +00:00
- A Streamlabs Socket API key.
2022-11-13 10:07:08 +00:00
- You can acquire this by logging into your Streamlabs.com dashboard then `Settings->Api Settings->API Tokens`
2023-06-28 03:36:38 +01:00
- Python 3.8 or greater
2022-11-13 10:07:08 +00:00
### How to install using pip
```
pip install streamlabsio
```
### How to Use
You may store your api key in a `config.toml` file, its contents should resemble:
```toml
[streamlabs]
token = "< apikey > "
```
Place it next to your `__main__.py` file.
#### Otherwise:
You may pass it as a keyword argument.
Example `__main__.py` :
```python
import streamlabsio
2023-06-28 03:36:38 +01:00
def on_twitch_event(event, data):
print(f"{event}: {data.attrs()}")
2022-11-13 10:07:08 +00:00
def main():
with streamlabsio.connect(token="< apikey > ") as client:
2022-11-13 14:41:03 +00:00
client.obs.on("streamlabs", on_twitch_event)
client.obs.on("twitch_account", on_twitch_event)
2022-11-13 10:07:08 +00:00
2022-11-13 14:41:03 +00:00
# run for 30 seconds then disconnect client from server
client.sio.sleep(30)
2022-11-13 10:07:08 +00:00
if __name__ == "__main__":
main()
```
2022-11-14 19:29:08 +00:00
#### 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.
2022-11-13 14:41:03 +00:00
2023-06-28 03:36:38 +01:00
### Client class
`streamlabsio.connect(token="<apikey>", raw=False)`
The following keyword arguments may be passed:
- `token` : str Streamlabs SocketIO api token.
2023-06-28 04:31:54 +01:00
- `raw` : boolean=False Receive raw data messages as json objects.
2023-06-28 03:36:38 +01:00
2023-08-19 21:41:04 +01:00
The following attribute is available:
- `raw` : boolean Toggle raw mode at runtime.
2022-11-13 10:07:08 +00:00
### Attributes
2023-06-28 03:38:45 +01:00
For event data you may inspect the available attributes using `attrs()` .
2022-11-13 10:07:08 +00:00
example:
```python
2023-06-28 03:38:45 +01:00
def on_twitch_event(event, data):
print(f"{event}: {data.attrs()}")
2022-11-13 10:07:08 +00:00
```
2023-06-28 03:36:38 +01:00
### Errors
- `SteamlabsSIOConnectionError` : Exception raised when connection errors occur
### Logging
To view raw incoming event data set logging level to DEBUG. Check `debug` example.
2022-11-13 10:07:08 +00:00
### Official Documentation
2022-11-14 19:29:08 +00:00
- [Streamlabs Socket API ](https://dev.streamlabs.com/docs/socket-api )