Resource icon

OBS Python Mastodon Integration 2025-02-08

Sends automatic messages, when stream starts (and ends as well). Can be used for automatic stream announcements. Based on this plugin: https://obsproject.com/forum/resources/obs-studio-twitter-integration.1870/

Requieres:
Python 3.11.4 (download and install it)
mastodon.py (after installation of Python, open your cmd/Powershell and type 'pip install mastodon')
pytz (for good looking timestamps, 'pip install pytz' - mastodon messages has already a timestamps on them, this is just required to bypass a spam protection)

Next you need to go to your Mastodon account -> Preferences -> Development and click "New application", name it whatever you like - doesn't really matter.
Then you need to set the "scope" to "write" (or just "write:statuses").

It will give you three types of tokens next, client key, secret key and "your access token" - last one is the exactly what you need to copy.

Copy and paste it into your MASTODON_TOKEN.txt file.

TEXT_STREAM_START.txt and TEXT_STREAM_END.txt is the actual messages that you are about to send.
Note that all three files should be placed in one folder with the script (i.e. four files should be in total including the script itself)!

Also don't forget to change your timezone in pytz.timezone("Europe/Moscow"), the full list of timezones is available here: https://gist.github.com/heyalexej/8bf688fd67d7199be4a1682b3eec7568

Full code:
Python:
import obspython as obs
import subprocess
import datetime
import pytz
import os
from mastodon import Mastodon

path = os.path.dirname(__file__)
 
with open(f"{path}/MASTODON_TOKEN.txt", 'r') as file:
    MASTODON_TOKEN = file.read()

with open(f"{path}/TEXT_STREAM_START.txt", 'rt', encoding="utf-8") as file:
    TEXT_STREAM_START = file.read()
 
with open(f"{path}/TEXT_STREAM_END.txt", 'rt', encoding="utf-8") as file:
    TEXT_STREAM_END = file.read()

mastodon = Mastodon(api_base_url='https://mastodon.social/')
mastodon.access_token = MASTODON_TOKEN

def start_streaming():
    time = datetime.datetime.now(pytz.timezone("Europe/Moscow")).replace(second=0, microsecond=0)
    text = f"{TEXT_STREAM_START}\n{time}"
    mastodon.toot(text)

def stop_streaming():
    time = datetime.datetime.now(pytz.timezone("Europe/Moscow")).replace(second=0, microsecond=0)
    text = f"{TEXT_STREAM_END}\n{time}"
    mastodon.toot(text)

def on_event(event):
    if event == obs.OBS_FRONTEND_EVENT_STREAMING_STARTED:
        obs.script_log(obs.LOG_INFO, "Stream started, sending messages to platforms...")
        start_streaming()
        return
    elif event == obs.OBS_FRONTEND_EVENT_STREAMING_STOPPED:
        obs.script_log(obs.LOG_INFO, "Stream stopped, sending messages to platforms...")
        stop_streaming()
        return

def script_load(settings):
    obs.script_log(obs.LOG_INFO, "Script loaded, waiting for streaming events...")
    obs.obs_frontend_add_event_callback(on_event)
Author
aaa123
Downloads
44
Views
94
First release
Last update
Rating
0.00 star(s) 0 ratings
Top