Skip to content

Instantly share code, notes, and snippets.

@Specnr
Created August 18, 2022 20:02
Show Gist options
  • Select an option

  • Save Specnr/321a9ef59a66fb040ff9deb45d3654b6 to your computer and use it in GitHub Desktop.

Select an option

Save Specnr/321a9ef59a66fb040ff9deb45d3654b6 to your computer and use it in GitHub Desktop.
An OBS Websocket script used to easily set and swap players for tournaments
from obswebsocket import obsws, requests
import time
host = "localhost"
port = 4444
password = ""
player_format = "p"
ws = obsws(host, port, password)
try:
ws.connect()
except:
quit()
def getUserFromSource(number):
url = ws.call(requests.GetBrowserSourceProperties(
f"{player_format}{number}")).getUrl()
return url.split("channel=")[1].split("&enable")[0]
def refreshPlayer(number):
ws.call(requests.SetSceneItemProperties(
f"{player_format}{number}", visible=False))
ws.call(requests.SetSceneItemProperties(
f"{player_format}{number}", visible=True))
def updatePlayer(number, name):
ws.call(requests.SetBrowserSourceProperties(
f"{player_format}{number}",
is_local_file=False,
url=f"https://player.twitch.tv/?channel={name}&enableExtensions=true&muted=true&parent=twitch.tv&player=popout&quality=chunked&volume=0.01")
)
refreshPlayer(number)
while(True):
user_cmd = input("What action would you like to perform? ")
# Exit
if user_cmd == "x":
break
# Who
elif user_cmd == "w":
i = 1
while(True):
try:
print(f"Player {i}: {getUserFromSource(i)}")
except:
break
i += 1
# Reconnect
if user_cmd == "c":
ws.disconnect()
time.sleep(1)
ws.connect()
print("Reconnected.")
# Refresh
elif user_cmd.startswith("r"):
if user_cmd[1].isnumeric():
refreshPlayer(user_cmd[1])
# Swap
elif user_cmd.startswith("sw"):
if user_cmd[2].isnumeric() and user_cmd[3].isnumeric():
a, b = getUserFromSource(
user_cmd[2]), getUserFromSource(user_cmd[3])
updatePlayer(user_cmd[2], b)
updatePlayer(user_cmd[3], a)
# Set then swap main
elif user_cmd.startswith("sm"):
if user_cmd[2].isnumeric():
number, name = user_cmd[2], user_cmd[3:]
updatePlayer(number, name.strip())
updatePlayer(number.strip(), getUserFromSource(1))
updatePlayer(1, name.strip())
# Set
elif user_cmd.startswith("s"):
if user_cmd[1].isnumeric():
number, name = user_cmd[1], user_cmd[2:]
updatePlayer(number.strip(), name.strip())
else:
print("Didn't recognize your command, try again.")
ws.disconnect()
@Specnr
Copy link
Author

Specnr commented Aug 18, 2022

supports up to 9 players on screen, more could be added but a lack of need makes it unnecessary for now.
might add gui at some point to simplify more

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment