-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwnp-obs.py
183 lines (165 loc) · 8.01 KB
/
wnp-obs.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
from threading import Thread
import urllib.request, json
from pywnp import WNPRedux
import obspython as obs
import time
# WNP Variables
player_name = 'N/A'
title = 'N/A'
artist = 'N/A'
album = 'N/A'
duration = '0:00'
position = '0:00'
position_percent = '0'
cover_url = ''
# Script Settings
selected_widget = 'None'
widgets_manifest = None
default_cover_url = ''
custom_format = '{title} - {artist} ({position}/{duration}) '
fallback_default_cover_url = 'https://raw.githubusercontent.com/keifufu/WebNowPlaying-Redux-OBS/main/widgets/images/nocover.png'
custom_css = r'body { background-color: rgba(0, 0, 0, 0); margin: 0px auto; overflow: hidden; } '
custom_css += r':root { --default-cover-url: url("{default_cover_url}"); }'
def script_description():
description = '<b>WebNowPlaying for OBS</b>'
description += '<br>'
description += 'Available placeholders:'
description += '<br>'
description += '{player_name}, {title}, {artist}, {album}, {duration}, {position}, {position_percent}'
return description
def script_defaults(settings):
obs.obs_data_set_default_string(settings, 'custom_format', custom_format)
obs.obs_data_set_default_string(settings, 'default_cover_url', default_cover_url)
def script_properties():
props = obs.obs_properties_create()
list = obs.obs_properties_add_list(props, 'selected_widget', 'Widget', obs.OBS_COMBO_TYPE_LIST, obs.OBS_COMBO_FORMAT_STRING)
obs.obs_property_list_add_string(list, 'None', 'None')
req = urllib.request.Request('https://raw.githubusercontent.com/keifufu/WebNowPlaying-Redux-OBS/main/widgets/manifest.json', headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/111.0'})
with urllib.request.urlopen(req) as url:
data = json.loads(url.read().decode())
global widgets_manifest
widgets_manifest = data
for widget in data:
obs.obs_property_list_add_string(list, widget['name'], widget['name'])
obs.obs_properties_add_text(props, 'custom_format', 'Format', obs.OBS_TEXT_DEFAULT)
obs.obs_properties_add_button(props, 'cs', 'Create Sources', create_sources)
obs.obs_properties_add_text(props, 'default_cover_url', 'Default Cover URL', obs.OBS_TEXT_DEFAULT)
return props
def script_update(settings):
global custom_format, default_cover_url, selected_widget
selected_widget = obs.obs_data_get_string(settings, 'selected_widget')
custom_format = obs.obs_data_get_string(settings, 'custom_format')
default_cover_url = obs.obs_data_get_string(settings, 'default_cover_url')
update_widget()
def script_load(settings):
def logger(type, message):
print(f'WNP - {type}: {message}')
print('load')
WNPRedux.start(6534, '2.0.0', logger)
obs.timer_add(update, 250)
def script_unload():
Thread(target=WNPRedux.stop).start()
time.sleep(1) # sorry but this at least prevents reloading the script breaking it
obs.timer_remove(update)
def update():
if WNPRedux.is_started:
global player_name, title, artist, album, duration, position, position_percent, cover_url
player_name = WNPRedux.media_info.player_name or 'N/A'
title = WNPRedux.media_info.title or 'N/A'
artist = WNPRedux.media_info.artist or 'N/A'
album = WNPRedux.media_info.album or 'N/A'
duration = WNPRedux.media_info.duration
position = WNPRedux.media_info.position
position_percent = str(int(WNPRedux.media_info.position_percent))
cover_url = WNPRedux.media_info.cover_url
update_source('Player', 'text', player_name)
update_source('Title', 'text', title)
update_source('Artist', 'text', artist)
update_source('Album', 'text', album)
update_source('Duration', 'text', duration)
update_source('Position', 'text', position)
update_source('Cover', 'url', cover_url or default_cover_url or fallback_default_cover_url)
try:
update_source('Formatted', 'text', custom_format.format(player_name=player_name, title=title, artist=artist, album=album, duration=duration, position=position, positionPercent=position_percent))
except:
pass
def create_sources(props, prop):
create_text_source('WNP-PlayerName', 'N/A')
create_text_source('WNP-Title', 'N/A')
create_text_source('WNP-Artist', 'N/A')
create_text_source('WNP-Album', 'N/A')
create_text_source('WNP-Duration', '0:00')
create_text_source('WNP-Position', '0:00')
create_cover_source('WNP-Cover', default_cover_url or fallback_default_cover_url)
try:
create_text_source('WNP-Formatted', custom_format.format(player_name=player_name, title=title, artist=artist, album=album, duration=duration, position=position, positionPercent=position_percent))
except:
pass
def update_source(type, key, value):
source = obs.obs_get_source_by_name(f'WNP-{type}')
if source is not None:
settings = obs.obs_data_create()
obs.obs_data_set_string(settings, key, value)
obs.obs_source_update(source, settings)
obs.obs_data_release(settings)
obs.obs_source_release(source)
def create_text_source(name, placeholder):
source = obs.obs_get_source_by_name(name)
if source is None:
current_scene = obs.obs_frontend_get_current_scene()
scene = obs.obs_scene_from_source(current_scene)
settings = obs.obs_data_create()
obs.obs_data_set_string(settings, 'text', placeholder)
source = obs.obs_source_create('text_gdiplus', name, settings, None)
obs.obs_scene_add(scene, source)
obs.obs_scene_release(scene)
obs.obs_data_release(settings)
obs.obs_source_release(source)
return source
def create_cover_source(name, url):
source = obs.obs_get_source_by_name(name)
if source is None:
current_scene = obs.obs_frontend_get_current_scene()
scene = obs.obs_scene_from_source(current_scene)
settings = obs.obs_data_create()
obs.obs_data_set_string(settings, 'url', url)
obs.obs_data_set_int(settings, 'width', 300)
obs.obs_data_set_int(settings, 'height', 300)
obs.obs_data_set_string(settings, 'css', 'img { width: auto; height: 100%; aspect-ratio: 1; object-fit:cover; }')
source = obs.obs_source_create('browser_source', name, settings, None)
obs.obs_scene_add(scene, source)
obs.obs_scene_release(scene)
obs.obs_data_release(settings)
obs.obs_source_release(source)
return source
def update_widget():
if widgets_manifest == None: return
if selected_widget == 'None':
source = obs.obs_get_source_by_name('WNP-Widget')
if source is not None:
obs.obs_source_remove(source)
obs.obs_source_release(source)
else:
source = obs.obs_get_source_by_name('WNP-Widget')
if source is None:
current_scene = obs.obs_frontend_get_current_scene()
scene = obs.obs_scene_from_source(current_scene)
settings = obs.obs_data_create()
obs.obs_data_set_string(settings, 'url', f'https://raw.githack.com/keifufu/WebNowPlaying-Redux-OBS/main/widgets/{selected_widget}.html')
obs.obs_data_set_int(settings, 'height', next((t['height'] for t in widgets_manifest if t['name'] == selected_widget), 0))
obs.obs_data_set_int(settings, 'width', next((t['width'] for t in widgets_manifest if t['name'] == selected_widget), 0))
obs.obs_data_set_string(settings, 'css', custom_css.replace(r'{default_cover_url}', default_cover_url or fallback_default_cover_url))
source = obs.obs_source_create('browser_source', 'WNP-Widget', settings, None)
obs.obs_scene_add(scene, source)
obs.obs_scene_release(scene)
obs.obs_data_release(settings)
else:
settings = obs.obs_data_create()
obs.obs_data_set_string(settings, 'url', f'https://raw.githack.com/keifufu/WebNowPlaying-Redux-OBS/main/widgets/{selected_widget}.html')
obs.obs_data_set_int(settings, 'height', next((t['height'] for t in widgets_manifest if t['name'] == selected_widget), 0))
obs.obs_data_set_int(settings, 'width', next((t['width'] for t in widgets_manifest if t['name'] == selected_widget), 0))
obs.obs_data_set_string(settings, 'css', custom_css.replace(r'{default_cover_url}', default_cover_url or fallback_default_cover_url))
obs.obs_source_update(source, settings)
obs.obs_data_release(settings)
obs.obs_source_release(source)
return source