mirror of
https://github.com/onyx-and-iris/streamlabs-socketio-py
synced 2026-04-07 13:43:30 +00:00
v2 proposal. See CHANGELOG unreleased.
This commit is contained in:
@@ -1,9 +0,0 @@
|
||||
## About
|
||||
|
||||
The underlying socketio and engineio packages emit a lot of logs so it may be useful to filter out streamlabsio logs.
|
||||
|
||||
This example prints raw log messages whenever Client.event_handler() receives data.
|
||||
|
||||
## Use
|
||||
|
||||
Run the script and trigger any of the events with `Test Widgets` in the Streamlabs GUI.
|
||||
@@ -1,56 +0,0 @@
|
||||
from logging import config
|
||||
|
||||
import streamlabsio
|
||||
|
||||
config.dictConfig(
|
||||
{
|
||||
'version': 1,
|
||||
'disable_existing_loggers': False,
|
||||
'loggers': {
|
||||
'streamlabsio.client': {
|
||||
'level': 'DEBUG',
|
||||
'handlers': ['console'],
|
||||
'propagate': False,
|
||||
}
|
||||
},
|
||||
'handlers': {
|
||||
'console': {
|
||||
'class': 'logging.StreamHandler',
|
||||
'level': 'DEBUG',
|
||||
'formatter': 'simple',
|
||||
'stream': 'ext://sys.stdout',
|
||||
}
|
||||
},
|
||||
'formatters': {
|
||||
'simple': {'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s'}
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def on_youtube_event(event, data):
|
||||
print(f'{event}: {data.attrs()}')
|
||||
|
||||
|
||||
def on_twitch_event(event, data):
|
||||
if event == 'follow':
|
||||
print(f'Received follow from {data.name}')
|
||||
elif event == 'bits':
|
||||
print(f'{data.name} donated {data.amount} bits! With message: {data.message}')
|
||||
elif event == 'donation':
|
||||
print(
|
||||
f'{data.name} donated {data.formatted_amount}! With message: {data.message}'
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
with streamlabsio.connect() as client:
|
||||
client.obs.on('streamlabs', on_twitch_event)
|
||||
client.obs.on('twitch_account', on_twitch_event)
|
||||
client.obs.on('youtube_account', on_youtube_event)
|
||||
|
||||
client.sio.sleep(30)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
19
examples/events/README.md
Normal file
19
examples/events/README.md
Normal file
@@ -0,0 +1,19 @@
|
||||
## About
|
||||
|
||||
To view the logs emitted by the streamlabsio library simply add the following to your code:
|
||||
|
||||
```python
|
||||
from loguru import logger
|
||||
|
||||
logger.enable('streamlabsio')
|
||||
```
|
||||
|
||||
## Configure
|
||||
|
||||
The script expects the Streamlabs token to be loaded into the environment with key `STREAMLABS_TOKEN`.
|
||||
|
||||
If you're running the script with `poetry poe` then poe is configured to load a `.env` file in the root of the repository.
|
||||
|
||||
## Use
|
||||
|
||||
Run the script and trigger any of the events with `Test Widgets` in the Streamlabs GUI.
|
||||
51
examples/events/__main__.py
Normal file
51
examples/events/__main__.py
Normal file
@@ -0,0 +1,51 @@
|
||||
import os
|
||||
from dataclasses import asdict
|
||||
|
||||
from loguru import logger
|
||||
|
||||
import streamlabsio
|
||||
|
||||
logger.enable('streamlabsio')
|
||||
|
||||
|
||||
def on_streamlabs_event(event, data):
|
||||
match event:
|
||||
case 'donation':
|
||||
print(f'{data.name} donated {data.amount}! With message: {data.message}')
|
||||
|
||||
|
||||
def on_twitch_event(event, data):
|
||||
event_message = {
|
||||
'follow': 'Received follow from {name}',
|
||||
'bits': '{name} donated {amount} bits! With message: {message}',
|
||||
'subscription': '{name} just subscribed for {months} months!',
|
||||
'raid': '{name} just raided with {raiders} raiders!',
|
||||
'host': '{name} just hosted with {viewers} viewers!',
|
||||
}
|
||||
|
||||
if event in event_message:
|
||||
print(event_message[event].format(**asdict(data)))
|
||||
|
||||
|
||||
def on_youtube_event(event, data):
|
||||
event_message = {
|
||||
'follow': 'Received follow from {name}',
|
||||
'superchat': '{name} donated {display_string} with a superchat! With comment: {comment}',
|
||||
'subscription': '{name} just subscribed for {months} months!',
|
||||
}
|
||||
|
||||
if event in event_message:
|
||||
print(event_message[event].format(**asdict(data)))
|
||||
|
||||
|
||||
def main():
|
||||
with streamlabsio.connect(token=os.getenv('STREAMLABS_TOKEN')) as client:
|
||||
client.obs.on('streamlabs', on_streamlabs_event)
|
||||
client.obs.on('twitch_account', on_twitch_event)
|
||||
client.obs.on('youtube_account', on_youtube_event)
|
||||
|
||||
client.sio.sleep(30)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user