pystr/obs_action.py

61 lines
2.3 KiB
Python
Raw Normal View History

2024-03-03 23:53:00 +00:00
import json
import asyncio
import websockets
2024-06-24 20:45:25 +00:00
from simpleobsws import obsws, requests
2024-03-03 23:53:00 +00:00
def load_config(file_path):
try:
with open(file_path, 'r') as config_file:
config = json.load(config_file)
return config
except FileNotFoundError:
print(f"Config file not found at {file_path}.")
return None
2024-06-24 20:45:25 +00:00
async def listen_to_relay_and_control_obs(config):
obs = obsws(config["obs_host"], config["obs_port"], config["obs_password"])
await obs.connect()
2024-03-03 23:53:00 +00:00
2024-06-24 20:45:25 +00:00
uri = f"{config['your_relay_url']}/ws"
2024-03-03 23:53:00 +00:00
async with websockets.connect(uri) as websocket:
# Subscribe to all kind 3 events
subscription_id = "kind_3_events" # Replace with a unique identifier
await websocket.send(json.dumps(["REQ", subscription_id, {"kinds": [3]}]))
while True:
response = await websocket.recv()
2024-06-24 20:45:25 +00:00
await handle_event(response, config, obs) # Await the handle_event coroutine
2024-03-03 23:53:00 +00:00
2024-06-24 20:45:25 +00:00
async def handle_event(event_json, config, obs):
2024-03-03 23:53:00 +00:00
event = json.loads(event_json)
if event[0] == "EVENT":
event_data = event[2]
if event_data["kind"] == 3: # Follow list event
2024-06-24 20:45:25 +00:00
await handle_follow_list(event_data, config, obs) # Await the handle_follow_list coroutine
2024-03-03 23:53:00 +00:00
2024-06-24 20:45:25 +00:00
async def handle_follow_list(follow_list_event, config, obs):
2024-03-03 23:53:00 +00:00
if "tags" in follow_list_event:
# Extract the list of "p" tags from the follow list event
p_tags = [tag for tag in follow_list_event["tags"] if tag[0] == "p"]
# Check if your pubkey is the last entry in the "p" tags
2024-06-24 20:45:25 +00:00
if p_tags and p_tags[-1][1] == config["your_pubkey"]:
2024-03-03 23:53:00 +00:00
print(f"You have a new follower: {follow_list_event.get('pubkey', '')}")
2024-06-24 20:45:25 +00:00
# Now, you can control OBS source group as per your requirement.
await control_obs_source_group(obs)
2024-03-04 19:27:58 +00:00
2024-06-24 20:45:25 +00:00
async def control_obs_source_group(obs):
# Replace "YourSourceGroup" with the actual name of your source group in OBS
source_group_name = "YourSourceGroup"
2024-03-04 19:27:58 +00:00
2024-06-24 20:45:25 +00:00
# Toggle the visibility of the source group
await obs.call(requests.SetSourceRender(sourceGroup=source_group_name, render=not obs.is_rendering_source(source_group_name)))
2024-03-03 23:53:00 +00:00
if __name__ == "__main__":
config_path = 'config.json'
config = load_config(config_path)
if config:
2024-06-24 20:45:25 +00:00
asyncio.get_event_loop().run_until_complete(listen_to_relay_and_control_obs(config))