-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeta_librespot-java.py
412 lines (357 loc) · 15.9 KB
/
meta_librespot-java.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# This file is part of snapcast
# Copyright (C) 2014-2022 Johannes Pohl
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Dependencies:
# - websocket-client
# - requests
from time import sleep
import websocket
import logging
import threading
import json
import sys
import getopt
import logging
import requests
__version__ = "@version@"
__git_version__ = "@gitversion@"
identity = "Snapcast"
params = {
'progname': sys.argv[0],
# Connection
'librespot-host': None,
'librespot-port': None,
'snapcast-host': None,
'snapcast-port': None,
'stream': None,
}
defaults = {
# Connection
'librespot-host': 'localhost',
'librespot-port': 24879,
'snapcast-host': 'localhost',
'snapcast-port': 1780,
'stream': 'default',
}
def send(json_msg):
sys.stdout.write(json.dumps(json_msg))
sys.stdout.write('\n')
sys.stdout.flush()
class LibrespotControl(object):
""" Librespot websocket remote control """
def __init__(self, params):
self._params = params
self._metadata = {}
self._properties = {}
self._properties['playbackStatus'] = 'playing'
self._properties['loopStatus'] = 'none'
self._properties['shuffle'] = False
self._properties['volume'] = 100
self._properties['mute'] = False
self._properties['rate'] = 1.0
self._properties['position'] = 0
self._properties['canGoNext'] = True
self._properties['canGoPrevious'] = True
self._properties['canPlay'] = True
self._properties['canPause'] = True
self._properties['canSeek'] = True
self._properties['canControl'] = True
self._req_id = 0
self._librespot_request_map = {}
self._seek_offset = 0.0
self.websocket = websocket.WebSocketApp("ws://" + self._params['librespot-host'] + ":" + str(self._params['librespot-port']) + "/events",
on_message=self.on_ws_message,
on_error=self.on_ws_error,
on_open=self.on_ws_open,
on_close=self.on_ws_close)
self.websocket_thread = threading.Thread(
target=self.websocket_loop, args=())
self.websocket_thread.name = "LibrespotControl"
self.websocket_thread.start()
def websocket_loop(self):
logger.info("Started LibrespotControl loop")
while True:
self.websocket.run_forever()
sleep(1)
logger.info("Ending LibrespotControl loop")
def getMetaData(self, data):
data = json.loads(data)
logger.debug(f'getMetaData: {data}')
metadata = {}
if data is None:
return metadata
if 'trackTime' in data:
self._properties['position'] = max(
0.0, float(data['trackTime']) / 1000.0)
if 'current' in data:
metadata['url'] = data['current']
if 'track' in data:
track = data['track']
if 'name' in track:
metadata['title'] = track['name']
if 'duration' in track:
metadata['duration'] = float(
track['duration']) / 1000
if 'artists' in track:
metadata['artist'] = []
artists = track['artists']
for artist in artists:
metadata['artist'].append(
artist['name'])
if 'album' in track:
album = track['album']
if 'name' in album:
metadata['album'] = album['name']
if 'date' in album:
date = album['date']
metadata['contentCreated'] = f"{date['year']:04d}-{date['month']:02d}-{date['day']:02d}"
if 'coverGroup' in album and 'image' in album['coverGroup']:
images = album['coverGroup']['image']
for image in images:
if 'size' in image and image['size'] == 'DEFAULT':
metadata['artUrl'] = f"http://i.scdn.co/image/{str(image['fileId']).lower()}"
if 'discNumber' in track:
metadata['discNumber'] = track['discNumber']
if 'number' in track:
metadata['trackNumber'] = track['number']
metadata['trackId'] = track['gid']
return metadata
def updateProperties(self):
res = self.send_request('player/current')
if res.status_code / 100 == 2:
self._metadata = self.getMetaData(res.text)
self._properties['metadata'] = self._metadata
def on_ws_message(self, ws, message):
# TODO: error handling
logger.debug(f'Snapcast RPC websocket message received: {message}')
jmsg = json.loads(message)
# Batch request
if isinstance(jmsg, list):
return
# Request
elif 'id' in jmsg:
return
# Notification
else:
if 'event' in jmsg:
event = jmsg['event']
logger.info(f"Event: {event}, msg: {jmsg}")
# elif event == 'contextChanged':
# elif event == 'trackChanged':
sendupdate = True
if event == 'playbackEnded':
self._properties['playbackStatus'] = 'stopped'
elif event == 'playbackPaused':
self._properties['playbackStatus'] = 'paused'
elif event == 'playbackResumed':
self._properties['playbackStatus'] = 'playing'
elif event == 'volumeChanged':
self._properties['volume'] = int(jmsg['value'] * 100.0)
elif event == 'trackSeeked':
self._properties['position'] = float(
jmsg['trackTime']) / 1000.0
elif event == 'metadataAvailable':
self.updateProperties()
# elif event == 'playbackHaltStateChanged':
# elif event == 'sessionCleared':
# elif event == 'sessionChanged':
# elif event == 'inactiveSession':
# elif event == 'connectionDropped':
# elif event == 'connectionEstablished':
# elif event == 'panic':
else:
sendupdate = False
if sendupdate:
send({"jsonrpc": "2.0", "method": "Plugin.Stream.Player.Properties",
"params": self._properties})
def on_ws_error(self, ws, error):
logger.error("Snapcast RPC websocket error")
logger.error(error)
def on_ws_open(self, ws):
logger.info("Snapcast RPC websocket opened")
send({"jsonrpc": "2.0", "method": "Plugin.Stream.Ready"})
def on_ws_close(self, ws, close_status_code, close_msg):
logger.info("Snapcast RPC websocket closed")
def send_request(self, method, params=None):
request = f"http://{self._params['librespot-host']}:{self._params['librespot-port']}/{method}"
logger.debug(f"Sending request: {request}")
r = requests.post(request, data=params)
logger.info(
f"Status: {r.status_code}, reason: {r.reason}, text: {r.text}")
return r
def stop(self):
self.websocket.keep_running = False
logger.info("Waiting for websocket thread to exit")
# self.websocket_thread.join()
def control(self, cmd):
try:
id = None
request = json.loads(cmd)
id = request['id']
[interface, cmd] = request['method'].rsplit('.', 1)
if interface == 'Plugin.Stream.Player':
if cmd == 'Control':
command = request['params']['command']
params = request['params'].get('params', {})
logger.debug(
f'Control command: {command}, params: {params}')
if command == 'next':
self.send_request("player/next")
elif command == 'previous':
self.send_request("player/prev")
elif command == 'play':
self.send_request("player/resume")
self._properties['playbackStatus'] = 'playing'
elif command == 'pause':
self.send_request("player/pause")
self._properties['playbackStatus'] = 'paused'
elif command == 'playPause':
# self.send_request("player/play-pause")
if self._properties['playbackStatus'] == 'playing':
self.send_request("player/pause")
self._properties['playbackStatus'] = 'paused'
else:
self.send_request("player/resume")
self._properties['playbackStatus'] = 'playing'
elif command == 'stop':
self.send_request("player/pause")
self._properties['playbackStatus'] = 'paused'
elif command == 'setPosition':
position = float(params['position'])
self._properties['position'] = position
self.send_request(
"player/seek", {"pos": int(position * 1000)})
elif command == 'seek':
self._seek_offset = float(params['offset'])
logger.debug("todo: not implemented")
# self.send_request(
# "core.playback.get_time_position", None, self.onGetTimePositionResponse)
# self.updateProperties()
elif cmd == 'SetProperty':
property = request['params']
logger.debug(f'SetProperty: {property}')
if 'shuffle' in property:
self._properties['shuffle'] = not self._properties['shuffle']
self.send_request("player/shuffle")
if 'loopStatus' in property:
value = property['loopStatus']
self._properties['loopStatus'] = value
if value == "playlist":
self.send_request(
"player/repeat", {"val": "context"})
elif value == "track":
self.send_request(
"player/repeat", {"val": "track"})
elif value == "none":
self.send_request("player/repeat", {"val": "none"})
if 'volume' in property:
self.send_request(
"player/set-volume", {"volume": int(float(property['volume']) / 100.0 * 65536.0)})
if 'mute' in property:
logger.debug("todo: not implemented")
# self._properties['mute'] = False
# self.send_request("core.mixer.set_mute", {
# "mute": property['mute']})
# self.updateProperties()
elif cmd == 'GetProperties':
res = self.send_request('player/current')
# if res.status_code / 100 == 2:
self._metadata = self.getMetaData(res.text)
self._properties['metadata'] = self._metadata
send({"jsonrpc": "2.0", "id": id,
"result": self._properties})
return
elif cmd == 'GetMetadata':
send({"jsonrpc": "2.0", "method": "Plugin.Stream.Log", "params": {
"severity": "Info", "message": "Logmessage"}})
return send({"jsonrpc": "2.0", "error": {"code": -32601,
"message": "TODO: GetMetadata not yet implemented"}, "id": id})
else:
return send({"jsonrpc": "2.0", "error": {"code": -32601,
"message": "Method not found"}, "id": id})
else:
return send({"jsonrpc": "2.0", "error": {"code": -32601,
"message": "Method not found"}, "id": id})
send({"jsonrpc": "2.0", "result": "ok", "id": id})
except Exception as e:
send({"jsonrpc": "2.0", "error": {
"code": -32700, "message": "Parse error", "data": str(e)}, "id": id})
def usage(params):
print("""\
Usage: %(progname)s [OPTION]...
--librespot-host=ADDR Set the librespot server address
--librespot-port=PORT Set the librespot server port
--snapcast-host=ADDR Set the snapcast server address
--snapcast-port=PORT Set the snapcast server port
--stream=ID Set the stream id
-h, --help Show this help message
-d, --debug Run in debug mode
-v, --version meta_librespot version
Report bugs to https://github.com/badaix/snapcast/issues""" % params)
if __name__ == '__main__':
log_format_stderr = '%(asctime)s %(module)s %(levelname)s: %(message)s'
log_level = logging.INFO
# Parse command line
try:
(opts, args) = getopt.getopt(sys.argv[1:], 'hdv',
['help', 'librespot-host=', 'librespot-port=', 'snapcast-host=', 'snapcast-port=', 'stream=', 'debug', 'version'])
except getopt.GetoptError as ex:
(msg, opt) = ex.args
print("%s: %s" % (sys.argv[0], msg), file=sys.stderr)
print(file=sys.stderr)
usage(params)
sys.exit(2)
for (opt, arg) in opts:
if opt in ['-h', '--help']:
usage(params)
sys.exit()
elif opt in ['--librespot-host']:
params['librespot-host'] = arg
elif opt in ['--librespot-port']:
params['librespot-port'] = int(arg)
elif opt in ['--snapcast-host']:
params['snapcast-host'] = arg
elif opt in ['--snapcast-port']:
params['snapcast-port'] = int(arg)
elif opt in ['--stream']:
params['stream'] = arg
elif opt in ['-d', '--debug']:
log_level = logging.DEBUG
elif opt in ['-v', '--version']:
v = __version__
if __git_version__:
v = __git_version__
print("meta_librespot version %s" % v)
sys.exit()
if len(args) > 2:
usage(params)
sys.exit()
logger = logging.getLogger('meta_librespot')
logger.propagate = False
logger.setLevel(log_level)
# Log to stderr
log_handler = logging.StreamHandler()
log_handler.setFormatter(logging.Formatter(log_format_stderr))
logger.addHandler(log_handler)
for p in ['librespot-host', 'librespot-port', 'snapcast-host', 'snapcast-port', 'stream']:
if not params[p]:
params[p] = defaults[p]
logger.debug(f'Parameters: {params}')
librespot_ctrl = LibrespotControl(params)
for line in sys.stdin:
librespot_ctrl.control(line)