-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.py
367 lines (302 loc) · 10.9 KB
/
main.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
from flask import Flask, request, jsonify, Response
from gevent.wsgi import WSGIServer
from functools import wraps
from crossdomain import crossdomain
from scheduler import Scheduler
from config import config
import song
import player
import user
import audit_log
from db import BannedUser
AUTHENTICATION_ENABLED = config.getboolean('Authentication', 'enabled')
if not AUTHENTICATION_ENABLED:
TEST_USERNAME = config.get('Authentication', 'test_username')
app = Flask(__name__)
#app.debug = True
scheduler = Scheduler()
scheduler.start()
def login_required(f):
if not AUTHENTICATION_ENABLED:
return f
@wraps(f)
def decorated_function(*args, **kwargs):
token = request.form.get('token')
if token is None:
return jsonify({'message': 'No SSO token provided'}), 401
if not user.valid_session(token):
return jsonify({'message': 'Invalid SSO token: ' + token}), 401
return f(*args, **kwargs)
return decorated_function
def get_username(token):
if not AUTHENTICATION_ENABLED:
return TEST_USERNAME
else:
session = user.get_session(token)
return session.json()['user']['name']
def check_eq_support(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if not player.equalizer_supported:
return jsonify({'message': 'Equalizer not supported'}), 400
return f(*args, **kwargs)
return decorated_function
@app.errorhandler(404)
def not_found(error):
return jsonify({'message': str(error)}), 404
@app.errorhandler(500)
def not_found(error):
return jsonify({'message': str(error)}), 500
@app.route('/v1/player/play_next', methods=['POST'])
@login_required
@crossdomain(origin='*')
def play_next():
audit_log.log(
get_username(request.form.get('token')),
'Skipped song'
)
return jsonify(scheduler.play_next(skip=True) or {})
@app.route('/v1/player/pause', methods=['POST'])
@login_required
@crossdomain(origin='*')
def pause():
audit_log.log(
get_username(request.form.get('token')),
'Paused/resumed song'
)
return jsonify(player.pause())
@app.route('/v1/player/volume', methods=['POST'])
@login_required
@crossdomain(origin='*')
def player_set_volume():
if request.form.get('volume'):
vol = int(request.form.get('volume'))
if 0 <= vol <= 100:
audit_log.log(
get_username(request.form.get('token')),
'Changed volume to %d' % vol
)
return jsonify(player.set_volume(vol))
else:
return jsonify({
'message': 'Volume must be between 0 and 100',
}), 400
return jsonify({'message': 'No volume parameter'}), 400
@app.route('/v1/player/equalizer', methods=['GET'])
@crossdomain(origin='*')
def player_get_equalizer_info():
return jsonify(player.get_static_equalizer_info())
@app.route('/v1/player/equalizer/enable', methods=['POST'])
@check_eq_support
@login_required
@crossdomain(origin='*')
def player_enable_eq():
if request.form.get('enabled'):
enabled = request.form.get('enabled') in ('True', 'true')
audit_log.log(
get_username(request.form.get('token')),
'%s equalizer' % ('Enabled' if enabled else 'Disabled')
)
return jsonify(player.set_equalizer_enabled(enabled))
return jsonify({'message': 'No equalizer enablement parameter'}), 400
@app.route('/v1/player/equalizer/adjust_preset', methods=['POST'])
@check_eq_support
@login_required
@crossdomain(origin='*')
def player_adjust_eq_preset():
if request.form.get('index'):
idx = int(request.form.get('index'))
if 0 <= idx < player.num_equalizer_presets:
return jsonify(player.set_equalizer_preset(idx))
else:
return jsonify({
'message': 'Equalizer preset index must be between 0 and %d'%(
player.num_equalizer_presets - 1)
}), 400
return jsonify({'message': 'No equalizer preset index parameter'}), 400
@app.route('/v1/player/equalizer/adjust_preamp', methods=['POST'])
@check_eq_support
@login_required
@crossdomain(origin='*')
def player_adjust_eq_preamp():
if request.form.get('level'):
lvl = float(request.form.get('level'))
if -20.0 <= lvl <= 20.0:
return jsonify(player.set_equalizer_preamp(lvl))
else:
return jsonify({'message':
'Equalizer preamp level must be between -20 dB and +20 dB'
}), 400
return jsonify({'message': 'No equalizer preamp level parameter'}), 400
@app.route('/v1/player/equalizer/adjust_band', methods=['POST'])
@check_eq_support
@login_required
@crossdomain(origin='*')
def player_adjust_eq_band():
if not request.form.get('band'):
return jsonify({'message': 'No band index parameter'}), 400
if not request.form.get('level'):
return jsonify({'message': 'No band level parameter'}), 400
idx = int(request.form.get('band'))
lvl = float(request.form.get('level'))
if idx < 0 or idx >= player.num_equalizer_bands:
return jsonify({
'message': 'Equalizer band index must be between 0 and %d'%(
player.num_equalizer_bands - 1)
}), 400
if lvl < -20.0 or lvl > 20.0:
return jsonify({
'message': 'Equalizer band level must be between -20 dB and +20 dB'
}), 400
return jsonify(player.set_equalizer_band(idx, lvl))
@app.route('/v1/songs/<song_id>', methods=['GET'])
@crossdomain(origin='*')
def show_song(song_id):
try:
return jsonify(song.Song(song_id).dictify())
except Exception, e:
return jsonify({'message': str(e)}), 404
@app.route('/v1/songs/search', methods=['GET'])
@crossdomain(origin='*')
def search():
query = request.args.get('q')
if query.startswith('album:'):
return jsonify(song.get_album(query[6:].lstrip()))
elif query.startswith('artist:'):
return jsonify(song.get_albums_for_artist(query[7:].lstrip()))
elif query.startswith('play-history'):
try:
limit = int(query[13:])
return jsonify(song.get_history(limit=limit))
except ValueError:
return jsonify(song.get_history())
elif query.startswith('top-songs'):
try:
limit = int(query[10:])
return jsonify(song.top_songs(limit=limit))
except ValueError:
return jsonify(song.top_songs())
else:
limit = request.args.get('limit')
if limit and int(limit) != 0:
return jsonify(song.search_songs(query, limit=int(limit)))
return jsonify(song.search_songs(query))
@app.route('/v1/songs/random', methods=['GET'])
@crossdomain(origin='*')
def random_songs():
limit = request.args.get('limit')
if limit and int(limit) != 0:
return jsonify(song.random_songs(limit=int(limit)))
return jsonify(song.random_songs())
@app.route('/v1/songs/history', methods=['GET'])
@crossdomain(origin='*')
def get_history():
limit = request.args.get('limit')
if limit and int(limit) != 0:
return jsonify(song.get_history(limit=int(limit)))
return jsonify(song.get_history())
@app.route('/v1/songs/top_songs', methods=['GET'])
@crossdomain(origin='*')
def top_songs():
limit = request.args.get('limit')
if limit and int(limit) != 0:
return jsonify(song.top_songs(limit=int(limit)))
return jsonify(song.top_songs())
@app.route('/v1/songs/top_artists', methods=['GET'])
@crossdomain(origin='*')
def top_artists():
limit = request.args.get('limit')
if limit and int(limit) != 0:
return jsonify(song.top_artists(limit=int(limit)))
return jsonify(song.top_artists())
@app.route('/v1/queue', methods=['GET'])
@crossdomain(origin='*')
def show_queue():
queue_user = request.args.get('user')
if queue_user:
return jsonify(scheduler.get_queue(user=queue_user))
return jsonify(scheduler.get_queue())
@app.route('/v1/queue/<int:song_id>', methods=['DELETE'])
@login_required
@crossdomain(origin='*')
def queue_remove(song_id):
audit_log.log(
get_username(request.form.get('token')),
'Removed song with id %d' % song_id
)
return jsonify(scheduler.remove_song(song_id))
@app.route('/v1/queue', methods=['DELETE'])
@login_required
@crossdomain(origin='*')
def queue_clear():
audit_log.log(
get_username(request.form.get('token')),
'Cleared queue'
)
return jsonify(scheduler.clear())
@app.route('/v1/queue/add', methods=['POST'])
@login_required
@crossdomain(origin='*')
def queue_add():
username = get_username(request.form.get('token'))
if request.form.get('id'):
try:
song_id = int(request.form.get('id'))
except ValueError:
return jsonify({'message': 'Invalid id'}), 400
audit_log.log(username, 'Added/voted for song with id %d' % song_id)
try:
return jsonify(scheduler.vote_song(username, song_id=song_id))
except Exception, e:
return jsonify({'message': str(e)}), 400
elif request.form.get('url'): # youtube and soundcloud
url = request.form.get('url')
audit_log.log(username, 'Added/voted for stream with url %s' % url)
try:
return jsonify(scheduler.vote_song(username, stream_url=url))
except Exception, e:
return jsonify({'message': str(e)}), 400
return jsonify({'message': 'No id or url parameter'}), 400
@app.route('/v1/now_playing', methods=['GET'])
@crossdomain(origin='*')
def now_playing():
return jsonify(player.get_now_playing() or {})
@app.route('/v1/session', methods=['POST'])
@crossdomain(origin='*')
def create_session():
"""Login.
For Crowd SSO support, save the token in a cookie with name
'crowd.token_key'.
"""
username = request.form.get('username')
password = request.form.get('password')
r = user.create_session(username, password)
if isinstance(r, BannedUser):
reason = r.reason or 'Not specified'
message = (
'You are banned. Please contact a Beats admin to be unbanned. '
'Reason: ' + reason
)
return jsonify({'message': message}), 403
elif r.status_code == 403:
return jsonify(
{'message': 'You must be an ACM member to use Beats.'}), 403
return jsonify(r.json()), r.status_code
@app.route('/v1/session/<token>', methods=['GET'])
@crossdomain(origin='*')
def get_session(token):
r = user.get_session(token)
return jsonify(r.json()), r.status_code
@app.route('/v1/session/<token>', methods=['DELETE'])
@crossdomain(origin='*')
def delete_session(token):
r = user.delete_session(token)
return Response(status=r.status_code)
@app.route('/', methods=['GET'])
def index():
return app.send_static_file('index.html')
if __name__ == '__main__':
print 'Beats by ACM'
print 'VLC version: ' + player.get_vlc_version()
http_server = WSGIServer(('', 5000), app)
http_server.serve_forever()