-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvlcwebclient.py
419 lines (321 loc) · 10 KB
/
vlcwebclient.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
413
414
415
416
417
418
419
# -*- coding: UTF-8 -*-
# Information taken from
# http://git.videolan.org/?p=vlc.git;a=blob_plain;f=share/lua/http/requests/+
# +README.txt;hb=HEAD
#
# This module tries to mostly provide a similar interface to
# https://github.com/DerMitch/py-vlcclient
#
__author__ = 'Florian Jung <[email protected]>'
__version__ = "0.1.0"
__license__ = "MIT License"
import requests
import urlparse
class WrongPasswordError(Exception):
"""
Invalid password sent to the server
"""
pass
class VLCWebClient(object):
def __init__(self, settings):
self.address = settings.get("address", "127.0.0.1")
self.port = settings.get("port", 8080)
self.username = settings.get("username", "")
self.password = settings.get("password", "")
self.timeout = settings.get("timeout", 5)
self._session = requests.Session()
if self.username or self.password:
self._session.auth = (self.username, self.password)
def _send(self, apiUri, format='json', **kwargs):
scheme, netloc, path, params, query, fragment = urlparse.urlparse(
self.address)
if not netloc:
netloc = self.address
if scheme == "":
scheme = "http"
if self.port != 80:
netloc += ":{}".format(self.port)
path = apiUri
url = urlparse.urlunparse(
(scheme, netloc, path, params, query, fragment))
res = self._session.get(url, params=kwargs, timeout=self.timeout)
if res.status_code == 401:
raise WrongPasswordError('Incorrect username/password')
res.raise_for_status()
if format == 'json':
try:
return res.json()
except:
if res.text.startswith("<html"):
# got error
raise ValueError("Error sending")
raise
else:
return res.text
def _send_command(self, **kwargs):
return self._send("/requests/status.json", **kwargs)
def status(self):
return self._send_command()
def info(self):
"""
information about current stream
"""
status = self.status()
return status['information']
def play(self, item=None, option=None):
"""
if item is a uri:
add <uri> to playlist and start playback:
?command=in_play&input=<uri>&option=<option>
if item is int or None:
play playlist item <id>. If <id> is omitted, play last active item:
?command=pl_play&id=<id>
:param item: item to play (playlist id or uri)
:param option: the option field is optional, and can have the values:
noaudio
novideo
(only if item is uri)
:return:
"""
id = None
try:
if item is not None:
id = int(item)
except:
if option:
return self._send_command(command="in_play", input=item,
option=option)
else:
return self._send_command(command="in_play", input=item)
if id is not None:
return self._send_command(command="pl_play", id=id)
else:
return self._send_command(command="pl_play")
def enqueue(self, uri):
"""
add <uri> to playlist:
?command=in_enqueue&input=<uri>
:param uri:
:return:
"""
return self._send_command(command="in_enqueue", input=uri)
def togglePlay(self, id=None):
"""
toggle pause.
If current state was 'stop', play item <id>, if no <id> specified,
play current item.
If no current item, play 1st item in the playlist:
?command=pl_pause&id=<id>
:param id: item id to play
:return:
"""
if id is not None:
return self._send_command(command="pl_pause", id=id)
else:
return self._send_command(command="pl_pause")
def pause(self):
"""
pause playback, do nothing if already paused
?command=pl_forcepause
:return:
"""
return self._send_command(command="pl_forcepause")
def resume(self):
"""
resume playback if paused, else do nothing
?command=pl_forceresume
:return:
"""
return self._send_command(command="pl_forceresume")
def stop(self):
"""
stop playback:
?command=pl_stop
:return:
"""
return self._send_command(command="pl_stop")
def rewind(self):
"""
Rewind stream
:return:
"""
return self.seek("0%")
def next(self):
"""
jump to next item:
?command=pl_next
:return:
"""
return self._send_command(command="pl_next")
def previous(self):
"""
jump to previous item:
?command=pl_previous
:return:
"""
return self._send_command(command="pl_previous")
def prev(self):
"""
jump to previous item:
?command=pl_previous
:return:
"""
return self.previous()
def delete(self, id):
"""
delete item <id> from playlist:
?command=pl_delete&id=<id>
NOTA BENE: pl_delete is completly UNSUPPORTED
:param id: item
:return:
"""
return self._send_command(command="pl_delete", id=id)
def empty(self):
"""
empty playlist:
?command=pl_empty
:return:
"""
return self._send_command(command="pl_empty")
def clear(self):
"""
Clear all items in playlist
:return:
"""
return self.empty()
def sort(self, sortMode, reversed=False):
"""
sort playlist using sort mode <val> and order <id>:
?command=pl_sort&id=<id>&val=<val>
If id=0 then items will be sorted in normal order, if id=1
they will be sorted in reverse order
:param sortMode: A non exhaustive list of sort modes:
0 Id
1 Name
3 Author
5 Random
7 Track number
:param reversed: sort in reverse order
:return:
"""
val = sortMode
id = 1 if reversed else 0
return self._send_command(command="pl_sort", id=id, val=val)
def random(self):
"""
toggle random playback:
?command=pl_random
:return:
"""
return self._send_command(command="pl_random")
def loop(self):
"""
toggle loop:
?command=pl_loop
:return:
"""
return self._send_command(command="pl_loop")
def repeat(self):
"""
toggle repeat:
?command=pl_repeat
:return:
"""
return self._send_command(command="pl_repeat")
def fullscreen(self):
"""
toggle fullscreen:
?command=fullscreen
:return:
"""
return self._send_command(command="fullscreen")
def volume(self, val=None):
"""
if val:
set volume level to <val> (can be absolute integer,
percent or +/- relative value):
?command=volume&val=<val>
Allowed values are of the form:
+<int>, -<int>, <int> or <int>%
else:
return volume
:param uri:
:return:
"""
if val is None:
res = self.status()
return res['volume']
return self._send_command(command="volume", val=val)
def volup(self, steps=1):
"""
increase colume by steps
:param steps: number of values to increase by (default: 1)
:return:
"""
self.volume("+{}".format(steps))
def voldown(self, steps=1):
"""
decrease colume by steps
:param steps: number of values to decrease by (default: 1)
:return:
"""
self.volume("-{}".format(steps))
def mute(self):
return self.volume(0)
def seek(self, val):
"""
seek to <val>:
?command=seek&val=<val>
Allowed values are of the form:
[+ or -][<int><H or h>:][<int><M or m or '>:]
[<int><nothing or S or s or ">]
or [+ or -]<int>%
(value between [ ] are optional, value between < > are mandatory)
examples:
1000 -> seek to the 1000th second
+1H:2M -> seek 1 hour and 2 minutes forward
-10% -> seek 10% back
:param val:
:return:
"""
return self._send_command(command="seek", val=val)
def playlist(self):
"""
get the full playlist tree
NB: playlist_jstree.xml is used for the internal web client.
It should not be relied upon by external remotes.
It may be removed without notice.
:return:
"""
return self._send("/requests/playlist.json")
def browse(self, uri):
"""
get file list from uri. At the moment, only local file uris are
supported
?dir=<uri>
NB: uri is the preferred parameter. Dir is deprecated and may be
removed in a future release.
:param uri:
:return:
"""
return self._send("/requests/browse.json", dir=uri)
def vlm(self, command=None):
"""
if command:
execute VLM command <cmd>
?command=<cmd>
get the error message from <cmd>
else:
get the full list of VLM elements
:param command:
:return:
"""
if command is None:
return self._send("/requests/vlm.xml", format="xml")
else:
return self._send("/requests/vlm_cmd.xml", format="xml",
command=command)
if __name__ == "__main__":
vlc = VLCWebClient({"password": ""})
import pprint
pprint.pprint(vlc.status())