-
Notifications
You must be signed in to change notification settings - Fork 1
/
gosumemory-reader.py
270 lines (204 loc) · 8.43 KB
/
gosumemory-reader.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import obspython as obs
import websocket as websockets
import json
import threading
import time
import traceback
api_endpoint = ""
jsonpath = ""
text_source = ""
format_rule = ""
format_args = []
cached_json = ""
websocket_thread = None
old_text = ""
thread_ws_stop = False
new_state = {}
DEBUG = False
# ------------------------------------------------------------
# Script by @KotRikD
# Written in 2020 for gosumemory
# ------------------------------------------------------------
def on_message(ws, message):
if thread_ws_stop:
ws.close()
global jsonpath
global format_args
try:
jsoned_message = json.loads(message)
# probably needs a better way of doing this
if type(jsonpath) == str:
lol = eval('jsoned_message["'+jsonpath.replace(".", '"]["')+'"]')
format_args[0] = str(lol)
elif type(jsonpath) in [list, tuple]:
for (ind, key) in enumerate(jsonpath):
value = eval('jsoned_message["'+key.replace(".", '"]["')+'"]')
format_args[ind] = value
update_text(format_rule.format(*format_args))
except Exception:
traceback.print_exc()
def on_any_trouble(ws, error=None):
if DEBUG:
obs.script_log(
obs.LOG_WARNING, f"Oops, looks like ws is crashed by N-reason: {error}")
def run_ws():
global api_endpoint
if DEBUG:
obs.script_log(
obs.LOG_WARNING, f"This is endpoint: {api_endpoint}")
websocket = websockets.WebSocketApp(api_endpoint,
on_message=on_message,
on_error=on_any_trouble,
on_close=on_any_trouble)
websocket.run_forever()
def update_text(text):
global text_source
global old_text
if old_text == text:
return
source = obs.obs_get_source_by_name(text_source)
if source is not None:
settings = obs.obs_data_create()
obs.obs_data_set_string(settings, "text", text)
obs.obs_source_update(source, settings)
obs.obs_data_release(settings)
obs.obs_source_release(source)
old_text = text
else:
if DEBUG:
obs.script_log(
obs.LOG_WARNING, f"Source is empty: {source}/{text_source}")
def check_thread_is_alive():
global websocket_thread
global thread_ws_stop
if websocket_thread is not None:
if not websocket_thread.is_alive():
websocket_thread = threading.Thread(target=run_ws)
websocket_thread.daemon = True
websocket_thread.start()
thread_ws_stop = False
def refresh_pressed(props, prop):
global websocket_thread
global api_endpoint
global thread_ws_stop
obs.timer_remove(check_thread_is_alive)
if websocket_thread is not None:
if websocket_thread.is_alive():
thread_ws_stop = True
while websocket_thread.is_alive():
pass
thread_ws_stop = False
websocket_thread = threading.Thread(target=run_ws)
websocket_thread.daemon = True
websocket_thread.start()
obs.timer_add(check_thread_is_alive, 1000)
def script_description():
return '''This is script for gathering data from gosumemory!
Requirements:
Python 3.6.x!(THIS IS VERY IMPORTANT)
websocket-client (python -m pip install websocket-client)
Documentation: https://vk.cc/az3XWj
If you have any questions: https://discord.gg/8enr4qD'''
def script_unload():
global thread_ws_stop
if websocket_thread is not None:
if websocket_thread.is_alive():
thread_ws_stop = True
def save_pressed(props, prop):
global api_endpoint
global jsonpath
global text_source
global format_rule
global format_args
global thread_ws_stop
global websocket_thread
api_endpoint = new_state["api_endpoint"]
jsonpath = new_state["jsonpath"]
text_source = new_state["text_source"]
format_rule = new_state["format_rule"]
format_args = new_state["format_args"]
if DEBUG:
obs.script_log(
obs.LOG_WARNING, f"{new_state}")
if api_endpoint != "" and text_source != "" and jsonpath != "" and format_rule != "" and format_args:
obs.timer_remove(check_thread_is_alive)
if websocket_thread is not None:
if websocket_thread.is_alive():
thread_ws_stop = True
while websocket_thread.is_alive():
pass
thread_ws_stop = False
websocket_thread = threading.Thread(target=run_ws)
websocket_thread.daemon = True
websocket_thread.start()
obs.timer_add(check_thread_is_alive, 1000)
def script_update(settings):
global new_state
new_state["api_endpoint"] = obs.obs_data_get_string(
settings, "api_endpoint")
new_state["jsonpath"] = obs.obs_data_get_string(settings, "jsonpath").replace(" ", "").strip()
if "," in new_state["jsonpath"]:
new_state["jsonpath"] = new_state["jsonpath"].split(",")
new_state["text_source"] = obs.obs_data_get_string(settings, "text_source")
new_state["format_rule"] = obs.obs_data_get_string(settings, "format_rule")
if type(new_state["jsonpath"]) == str:
new_state["format_args"] = [""]
elif type(new_state["jsonpath"]) in [list, tuple]:
new_state["format_args"] = []
for _ in new_state["jsonpath"]:
new_state["format_args"].append("")
def script_load(settings):
global new_state
if DEBUG:
obs.script_log(
obs.LOG_WARNING, "I'm joined to the party")
new_state["api_endpoint"] = obs.obs_data_get_string(
settings, "api_endpoint")
new_state["jsonpath"] = obs.obs_data_get_string(settings, "jsonpath").replace(" ", "").strip()
if "," in new_state["jsonpath"]:
new_state["jsonpath"] = new_state["jsonpath"].split(",")
new_state["text_source"] = obs.obs_data_get_string(settings, "text_source")
new_state["format_rule"] = obs.obs_data_get_string(settings, "format_rule")
if type(new_state["jsonpath"]) == str:
new_state["format_args"] = [""]
elif type(new_state["jsonpath"]) in [list, tuple]:
new_state["format_args"] = []
for _ in new_state["jsonpath"]:
new_state["format_args"].append("")
if DEBUG:
obs.script_log(
obs.LOG_WARNING, f"{new_state}")
save_pressed(None, None)
def script_defaults(settings):
obs.obs_data_set_default_string(
settings, "api_endpoint", "ws://localhost:24050/ws")
obs.obs_data_set_default_string(settings, "jsonpath", "menu.state")
obs.obs_data_set_default_string(
settings, "format_rule", "State: {0}")
new_state["jsonpath"] = "menu.state"
new_state["format_rule"] = "State: {0}"
new_state["api_endpoint"] = "ws://localhost:24050/ws"
def script_properties():
props = obs.obs_properties_create()
# obs_property_t *obs_properties_add_text(obs_properties_t *props, const char *name, const char *description, enum obs_text_type type)
obs.obs_properties_add_text(
props, "api_endpoint", "API Endpoint", obs.OBS_TEXT_DEFAULT)
obs.obs_properties_add_text(
props, "jsonpath", "Path to json string (ex menu.state)", obs.OBS_TEXT_DEFAULT)
obs.obs_properties_add_text(
props, "format_rule", "Formatting rule (python format)", obs.OBS_TEXT_MULTILINE)
# obs_property_t *obs_properties_add_int(obs_properties_t *props, const char *name, const char *description, int min, int max, int step)
p = obs.obs_properties_add_list(
props, "text_source", "Text Source (old FreeType 2)", obs.OBS_COMBO_TYPE_EDITABLE, obs.OBS_COMBO_FORMAT_STRING)
sources = obs.obs_enum_sources()
if sources is not None:
for source in sources:
source_id = obs.obs_source_get_unversioned_id(source)
if source_id == "text_ft2_source":
# gdiplus lags!!!!!! if source_id == "text_gdiplus" or source_id == "text_ft2_source":
name = obs.obs_source_get_name(source)
obs.obs_property_list_add_string(p, name, name)
obs.source_list_release(sources)
obs.obs_properties_add_button(
props, "button", "Save/Refresh", save_pressed)
return props