-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebmpc.py
More file actions
executable file
·239 lines (194 loc) · 5.77 KB
/
webmpc.py
File metadata and controls
executable file
·239 lines (194 loc) · 5.77 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
It's quite a surprise to me that you're currently reading this.
Initially I created this for my own home MPD-based entertainment system, and
yes, I'm aware of existance of many other similar projects. Yes, this is a
perfect example of NIH syndrome (http://lmgtfy.com/?q=nih+syndrome), but I made
my own web MPD client "with blackjack and whores".
I did it because I could and wanted to, and it seemed like a good excercise
since I've never done any web development before other than simple plain HTML.
Having said that, I totally understand that many things might be done
unefficiently, not very elegant, not in Python- or JS-way, of just simply wrong.
If it bothers you you may (moreover: you're strongly encouraged to) contact me
with some details, but it's much better if you send me a pull-request with brief
explanation to the change.
"""
import sys
import os.path
import threading
import json
import cherrypy as Ch
import MPDFacade
import config
'''
Some decorators for further convenience.
'''
# Decorator for REST methods restrictions
def http_methods_allowed(methods=['GET']):
method = Ch.request.method.upper()
if method not in methods:
Ch.response.headers['Allow'] = ", ".join(methods)
raise Ch.HTTPError(405)
Ch.tools.allow = Ch.Tool('on_start_resource', http_methods_allowed)
# Decorator for not implemented functions
def not_implemented(func):
def new_f(*args, **kwargs):
print "----- Not implemented: {0}, params: '{1}'".format(func.__name__, kwargs)
return json.dumps( { 'error' : "Command '"+func.__name__+"' is not implemented yet" } )
return new_f
"""
Main CherryPy server class.
"""
class WebMPC(object):
def __init__(self) :
self.__mpd_lock = threading.Lock()
'''
Web UI main page
'''
@Ch.expose
@Ch.tools.allow(methods=['GET'])
def index(self):
f = open('webmpc.html')
html = f.read()
f.close();
return html
####################################
# Getters
@Ch.expose
@Ch.tools.allow(methods=['GET'])
def currentSong(self) :
with self.__mpd_lock :
return json.dumps( mpdFacade.currentSong() )
@Ch.expose
@Ch.tools.allow(methods=['GET'])
def status(self) :
st = None
outputs = None
st_subset = {}
# status
with self.__mpd_lock :
st = mpdFacade.status()
for key in ['volume', 'time', 'state', 'song', 'songid', 'playlist', 'random'] :
try :
st_subset[key] = st[key]
except KeyError as e:
# It it's not playing now then there's not gonna be 'time' key
# in status, so it will throw a KeyError. We don't care, so we just skip it.
pass
# outputs
with self.__mpd_lock :
outputs = mpdFacade.outputs()
st_subset['outputs'] = outputs
return json.dumps( st_subset )
####################################
# Commands
@Ch.expose
@Ch.tools.allow(methods=['POST'])
def volumeUp(self) :
with self.__mpd_lock :
mpdFacade.volumeUp()
return json.dumps( mpdFacade.status() )
@Ch.expose
@Ch.tools.allow(methods=['POST'])
def volumeDown(self) :
with self.__mpd_lock :
mpdFacade.volumeDown()
return json.dumps( mpdFacade.status() )
@Ch.expose
@Ch.tools.allow(methods=['POST'])
def next(self) :
with self.__mpd_lock :
mpdFacade.next()
return json.dumps( mpdFacade.currentSong() )
@Ch.expose
@Ch.tools.allow(methods=['POST'])
def prev(self) :
with self.__mpd_lock :
mpdFacade.prev()
return json.dumps( mpdFacade.currentSong() )
@Ch.expose
@Ch.tools.allow(methods=['POST'])
def play(self) :
with self.__mpd_lock :
mpdFacade.play()
return json.dumps( mpdFacade.status() )
@Ch.expose
@Ch.tools.allow(methods=['POST'])
def pause(self) :
with self.__mpd_lock :
mpdFacade.pause()
return json.dumps( mpdFacade.status() )
@Ch.expose
@Ch.tools.allow(methods=['POST'])
def stop(self) :
with self.__mpd_lock :
mpdFacade.stop()
return json.dumps( mpdFacade.status() )
@Ch.expose
@Ch.tools.allow(methods=['POST'])
def setPos(self, pos) :
with self.__mpd_lock :
mpdFacade.setPos(int(pos))
return json.dumps( mpdFacade.status() )
@Ch.expose
@Ch.tools.allow(methods=['POST'])
def random(self, on) :
with self.__mpd_lock :
mpdFacade.random(on)
return json.dumps( mpdFacade.status() )
####################################
# Outputs
@Ch.expose
@Ch.tools.allow(methods=['GET'])
def outputs(self) :
with self.__mpd_lock :
return json.dumps( mpdFacade.outputs() )
@Ch.expose
@Ch.tools.allow(methods=['POST'])
def enableOutput(self, index) :
with self.__mpd_lock :
mpdFacade.enableOutput(index, True)
return json.dumps( { 'outputs' : mpdFacade.outputs() } )
@Ch.expose
@Ch.tools.allow(methods=['POST'])
def disableOutput(self, index) :
with self.__mpd_lock :
mpdFacade.enableOutput(index, False)
return json.dumps( { 'outputs' : mpdFacade.outputs() } )
####################################
# Playlist
@Ch.expose
@Ch.tools.allow(methods=['GET'])
def playlist(self) :
with self.__mpd_lock :
return json.dumps({ 'playlist' : mpdFacade.playlist() })
@Ch.expose
@Ch.tools.allow(methods=['POST'])
def playSong(self, id) :
with self.__mpd_lock :
mpdFacade.playSong(id)
return json.dumps( mpdFacade.currentSong() )
mpdFacade = MPDFacade.MPDFacade(host=config.mpd_host, port=config.mpd_port)
mpdFacade.connect()
if __name__ == '__main__':
currentDir = os.path.dirname(os.path.abspath(__file__))
Ch.engine.signal_handler.handlers["SIGINT"] = sys.exit
Ch.quickstart( WebMPC(), '/', config = {
'global' : {
'server.socket_host': config.host,
'server.socket_port': config.port
},
'/js': {
'tools.staticdir.on': True,
'tools.staticdir.dir': os.path.join(currentDir, 'js')
},
'/img': {
'tools.staticdir.on': True,
'tools.staticdir.dir': os.path.join(currentDir, 'img')
},
'/css': {
'tools.staticdir.on': True,
'tools.staticdir.dir': os.path.join(currentDir, 'css')
}
})