-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtube_studio_api.py
58 lines (50 loc) · 1.72 KB
/
vtube_studio_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import json
import threading
import time
import websocket
class VTubeStudioAPI:
def __init__(self):
self.ws = websocket.WebSocketApp(
"ws://localhost:8001",
on_open=self.on_open,
on_message=self.on_message,
on_close=self.on_close,
on_error=self.on_error,
)
self.ws_thread = threading.Thread(target=self.ws.run_forever, kwargs={"ping_interval": 20})
self.ws_thread.start()
time.sleep(1) # Give the connection some time to establish
def on_open(self, ws):
print("WebSocket connection opened")
self.authenticate()
def on_message(self, ws, message):
print("Received message:", message)
def on_close(self, ws):
print("Connection closed")
def close(self):
self.ws.close()
self.ws_thread.join()
def on_error(self, ws, error):
print("Error:", error)
def authenticate(self):
self.ws.send(json.dumps({
"apiName": "VTubeStudioPublicAPI",
"apiVersion": "1.0",
"requestID": "SomeID",
"messageType": "AuthenticationRequest",
"data": {
"pluginName": "Shiro chan",
"pluginDeveloper": "Madrus",
"authenticationToken": "c7c5296f2450ca697726e8e05b257586ee7f550671c4b0b5ee2ed7c7ad4c6cbc" #this need to be changed to variable
}
}))
def play_animation(self, hotkeyID):
self.ws.send(json.dumps({
"apiName": "VTubeStudioPublicAPI",
"apiVersion": "1.0",
"requestID": "SomeID",
"messageType": "HotkeyTriggerRequest",
"data": {
"hotkeyID": hotkeyID
}
}))