-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio.py
86 lines (71 loc) · 2.5 KB
/
audio.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
from flask import Flask, request, jsonify, abort, Response, render_template
from flask_assets import Environment, Bundle
from webassets_browserify import Browserify
from latex import build_pdf, LatexBuildError
from latex.jinja2 import make_env
from songs import get_songs
from string import digits,letters
app = Flask(__name__, static_url_path='/static')
assets = Environment(app)
js = Bundle('js/main.jsx',
depends=('*/*.js*'),
filters=Browserify,
output='app.js')
assets.register('js_all', js)
@app.route('/')
def index(): return render_template('index.html')
song_dict = get_songs()
song_list = song_dict.values()
@app.route('/songs/')
@app.route('/songs/<int:songid>')
def songs(songid=None):
def filter_keys(item):
return {
key: item[key]
for key in [
'songid',
'songtitle',
'firstline',
'songmeta',
'songtext',
'songnotes'
]
}
if songid == None: return jsonify(songs=map(filter_keys, song_list))
if songid in song_dict: return jsonify(filter_keys(song_dict[songid]))
else: return abort(404)
def whitelist(string, alphabet):
return ''.join([x for x in string if x in alphabet])
texenv = make_env(loader=app.jinja_loader)
@app.route('/songs.pdf')
def pdf():
texonly = 'texonly' in request.args
orientation = 'landscape' if 'landscape' in request.args else 'portrait'
cols = whitelist(request.args.get('cols', ''), digits) or '2'
font = whitelist(request.args.get('font', ''), digits+letters)
fontoptions = whitelist(request.args.get('fontoptions', ''), digits+letters)
songids = request.args.get('songids')
if songids:
try:
songids = map(int, songids.split(','))
except ValueError:
return 'Invalid songid'
else:
return 'No songs'
template = texenv.get_template('songs.tex')
tex = template.render(
songs=[song_dict[x] for x in songids if x in song_dict],
cols=cols,
orientation=orientation,
font=font,
fontoptions=fontoptions)
if texonly:
return Response(tex, mimetype='text/plain')
else:
try:
pdffile = build_pdf(tex)
except LatexBuildError as e:
return Response(tex, mimetype='text/plain')
return Response(bytes(pdffile), mimetype='application/pdf')
if __name__ == '__main__':
app.run(debug=True)