Websocket & Python Help

dunnand0

New Member
I have been using ChatGPT to help me script some specific needs of mine. The AI wants me to use websocket to pull off the script, GPT has not been updated since websocket has been integrated into OBS and I keep getting errors. Can anyone help me fix up my code?

import time
import xml.etree.ElementTree as ET
import websocket
import json

def check_xml_for_string(file_path, search_string):
try:
tree = ET.parse(file_path)
root = tree.getroot()
# Traverse the XML tree to find the string
for elem in root.iter():
if search_string in elem.text:
return True
except Exception as e:
print(f"Error reading XML file: {e}")
return False

def send_command_to_obs(command, data):
ws = websocket.create_connection("ws://localhost:4444") # OBS WebSocket URL
message = json.dumps({"request-type": command, "message-id": "1", **data})
ws.send(message)
response = ws.recv()
ws.close()
return response

# Path to your XML file
xml_file_path = "path/to/your/file.xml"
# String you're searching for
search_string = "YourStringHere"

while True:
if check_xml_for_string(xml_file_path, search_string):
# Command to play media source in OBS
response = send_command_to_obs("SetSceneItemProperties", {"item": "YourMediaSourceName", "visible": True})
print("Media source played in OBS:", response)
time.sleep(10) # Check every 10 seconds
 
Top