-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebserver.py
More file actions
52 lines (45 loc) · 1.42 KB
/
webserver.py
File metadata and controls
52 lines (45 loc) · 1.42 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
import subprocess
import flask
app = flask.Flask(__name__)
def run(cmd):
if isinstance(cmd, basestring):
cmd = cmd.split()
return (line.strip() for line in subprocess.check_output(cmd).decode('utf-8').strip().split('\n'))
@app.route('/')
def index():
return flask.render_template('index.html', albums=[])
@app.route('/albums')
def albums():
albums = []
for line in run('beet list -a -f $id|$album|$albumartist'):
(albumid, album, artist) = line.split('|')
albums.append({
'id': albumid,
'title': album,
'artist': artist,
})
return flask.jsonify({'albums': albums})
@app.route('/album/<albumid>')
def album(albumid):
tracks = []
for line in run('beet list album_id:%s -f $id|$track|$title' % albumid):
(itemid, tracknum, title) = line.split('|')
tracks.append({
'itemid': itemid,
'tracknum': tracknum,
'title': title,
})
tracks.sort(key=lambda t: t['tracknum'])
return flask.jsonify({'tracks': tracks})
@app.route('/file/<itemid>')
def get_file(itemid):
lines = list(run('beet list id:%s -f $path' % itemid))
print lines
if not lines:
flask.abort(404)
path = lines[0]
assert path.startswith('/home/yang/music/')
return flask.redirect(path.replace('/home/yang', ''))
if __name__ == '__main__':
app.debug = True
app.run(host='0.0.0.0', port=8338)