This commit is contained in:
Chris kerr 2024-06-24 16:45:25 -04:00
parent cbc87ca3c8
commit 51a9639e48

View File

@ -1,7 +1,7 @@
import json import json
import asyncio import asyncio
import websockets import websockets
from obswebsocket import obsws, requests from simpleobsws import obsws, requests
def load_config(file_path): def load_config(file_path):
try: try:
@ -12,13 +12,11 @@ def load_config(file_path):
print(f"Config file not found at {file_path}.") print(f"Config file not found at {file_path}.")
return None return None
async def listen_to_relay(your_pubkey, your_relay_url, obs_host, obs_port, obs_password): async def listen_to_relay_and_control_obs(config):
uri = f"{your_relay_url}/ws" obs = obsws(config["obs_host"], config["obs_port"], config["obs_password"])
await obs.connect()
# Create OBS connection
obs = obsws(obs_host, obs_port, obs_password)
obs.connect()
uri = f"{config['your_relay_url']}/ws"
async with websockets.connect(uri) as websocket: async with websockets.connect(uri) as websocket:
# Subscribe to all kind 3 events # Subscribe to all kind 3 events
subscription_id = "kind_3_events" # Replace with a unique identifier subscription_id = "kind_3_events" # Replace with a unique identifier
@ -26,53 +24,37 @@ async def listen_to_relay(your_pubkey, your_relay_url, obs_host, obs_port, obs_p
while True: while True:
response = await websocket.recv() response = await websocket.recv()
await handle_event(response, your_pubkey, obs) # Pass the OBS connection to handle_event await handle_event(response, config, obs) # Await the handle_event coroutine
async def handle_event(event_json, your_pubkey, obs): async def handle_event(event_json, config, obs):
event = json.loads(event_json) event = json.loads(event_json)
if event[0] == "EVENT": if event[0] == "EVENT":
event_data = event[2] event_data = event[2]
if event_data["kind"] == 3: # Follow list event if event_data["kind"] == 3: # Follow list event
await handle_follow_list(event_data, your_pubkey, obs) # Pass the OBS connection to handle_follow_list await handle_follow_list(event_data, config, obs) # Await the handle_follow_list coroutine
async def handle_follow_list(follow_list_event, config, obs):
async def handle_follow_list(follow_list_event, your_pubkey, obs):
if "tags" in follow_list_event: if "tags" in follow_list_event:
# Extract the list of "p" tags from the 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"] 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 # Check if your pubkey is the last entry in the "p" tags
if p_tags and p_tags[-1][1] == your_pubkey: if p_tags and p_tags[-1][1] == config["your_pubkey"]:
print(f"You have a new follower: {follow_list_event.get('pubkey', '')}") print(f"You have a new follower: {follow_list_event.get('pubkey', '')}")
# Toggle the specified source group in OBS # Now, you can control OBS source group as per your requirement.
source_group_name = "heyguys" # Replace with the name of your source group await control_obs_source_group(obs)
scene_name = "Escape From Tarkov" # Replace with the name of your scene
await toggle_source_group(obs, scene_name, source_group_name)
async def toggle_source_group(obs, scene_name, source_group_name): async def control_obs_source_group(obs):
# Get the list of scene items in the specified scene # Replace "YourSourceGroup" with the actual name of your source group in OBS
scene_items = obs.call(requests.GetSceneItemList(sceneName=scene_name)) source_group_name = "YourSourceGroup"
# Find the source group item in the list # Toggle the visibility of the source group
source_group_item = next((item for item in scene_items.getSceneItems() if item["name"] == source_group_name), None) await obs.call(requests.SetSourceRender(sourceGroup=source_group_name, render=not obs.is_rendering_source(source_group_name)))
if source_group_item:
# Enable or disable the visibility of the source group item
obs_response = obs.call(
requests.SetSceneItemProperties(item=source_group_item["name"], scene=scene_name, visible=not source_group_item["visible"])
)
print(f"Toggle Response: {obs_response}")
print(f"Toggled source group '{source_group_name}' in scene '{scene_name}'.")
else:
print(f"Source group '{source_group_name}' not found in scene '{scene_name}'.")
if __name__ == "__main__": if __name__ == "__main__":
config_path = 'config.json' config_path = 'config.json'
config = load_config(config_path) config = load_config(config_path)
if config: if config:
asyncio.get_event_loop().run_until_complete(listen_to_relay( asyncio.get_event_loop().run_until_complete(listen_to_relay_and_control_obs(config))
config["your_pubkey"], config["your_relay_url"],
config["obs_host"], config["obs_port"], config["obs_password"]
))