|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +''' |
| 4 | +Moo.play |
| 5 | +~~~~~~~~ |
| 6 | +
|
| 7 | +Flask app for playing music files |
| 8 | +
|
| 9 | +$ workon moo |
| 10 | +$ export FLASK_APP=Moo/play/app.py |
| 11 | +$ export FLASK_DEBUG=1 |
| 12 | +$ export FLASK_ENV=development |
| 13 | +$ flask run |
| 14 | +''' |
| 15 | + |
| 16 | +import os |
| 17 | +import random |
| 18 | + |
| 19 | +from urllib.parse import quote |
| 20 | + |
| 21 | +from flask import Flask, Response, render_template, request, abort |
| 22 | +from flask import redirect, url_for |
| 23 | +from Moo.play import config, lib |
| 24 | + |
| 25 | + |
| 26 | +app = Flask(__name__) |
| 27 | +app.config.from_object(config) |
| 28 | +base = app.config['BASE'] |
| 29 | +index = lib.index(app.config) |
| 30 | + |
| 31 | + |
| 32 | +@app.route('/') |
| 33 | +def root(): |
| 34 | + return render_template('index.html', base=base, index=index) |
| 35 | + |
| 36 | + |
| 37 | +@app.route('/album/<path:alkey>') |
| 38 | +def album_route(alkey): |
| 39 | + alb, metadata = album_data(alkey, index) |
| 40 | + return serve_album(alkey, alb, metadata) |
| 41 | + |
| 42 | + |
| 43 | +@app.route('/index/<letter>') |
| 44 | +def alpha_index(letter): |
| 45 | + alpha = list() |
| 46 | + |
| 47 | + for path in index: |
| 48 | + alname = str(path).replace(base, '') |
| 49 | + if alname.upper().startswith('/' + letter): |
| 50 | + alpha.append(path) |
| 51 | + |
| 52 | + return render_template('index.html', |
| 53 | + alpha=letter, base=base, |
| 54 | + index=sorted(alpha)) |
| 55 | + |
| 56 | + |
| 57 | +@app.route('/covers/<letter>') |
| 58 | +def alpha_covers(letter): |
| 59 | + alpha = list() |
| 60 | + |
| 61 | + for path in index: |
| 62 | + alname = str(path).replace(base, '') |
| 63 | + if alname.upper().startswith('/' + letter): |
| 64 | + alpha.append(path) |
| 65 | + |
| 66 | + return render_template('covers.html', alpha=letter, base=base, |
| 67 | + index=sorted(alpha)) |
| 68 | + |
| 69 | + |
| 70 | +@app.route('/covers') |
| 71 | +def covers(): |
| 72 | + return render_template('covers.html', index=index, base=base) |
| 73 | + |
| 74 | + |
| 75 | +@app.route('/debug') |
| 76 | +def debug(): |
| 77 | + return render_template('debug.html', data=index) |
| 78 | + |
| 79 | + |
| 80 | +@app.route('/img/track/<tnum>/<path:alkey>') |
| 81 | +def cover_track(tnum, alkey): |
| 82 | + try: |
| 83 | + album, metadata = album_data(alkey, index) |
| 84 | + mkey = sorted(metadata.keys())[int(tnum) - 1] |
| 85 | + tpath = metadata[mkey]['fpath'] |
| 86 | + return lib.cover(os.path.join(base, alkey), tpath) |
| 87 | + except TypeError: |
| 88 | + return app.send_static_file('cover.png') |
| 89 | + |
| 90 | + |
| 91 | +@app.route('/img/<path:alkey>') |
| 92 | +def cover(alkey): |
| 93 | + try: |
| 94 | + return lib.cover(os.path.join(base, alkey)) |
| 95 | + except TypeError: |
| 96 | + return app.send_static_file('cover.png') |
| 97 | + |
| 98 | + |
| 99 | +@app.route('/meta/<path:alkey>') |
| 100 | +def meta_route(alkey): |
| 101 | + album, metadata = album_data(alkey, index) |
| 102 | + return metadata |
| 103 | + |
| 104 | + |
| 105 | +@app.route('/random') |
| 106 | +def rando(): |
| 107 | + ind = random.choice(range(len(index))) |
| 108 | + alkey = str(index[ind]) |
| 109 | + album, metadata = album_data(alkey, index) |
| 110 | + return redirect('/album' + quote(alkey.replace(base, ''))) |
| 111 | + |
| 112 | + |
| 113 | +@app.route('/track/<tnum>/<path:alkey>') |
| 114 | +def track(tnum, alkey): |
| 115 | + alb, metadata = album_data(alkey, index) |
| 116 | + return serve_album(alkey, alb, metadata, int(tnum) - 1) |
| 117 | + |
| 118 | + |
| 119 | +def album_data(alkey, index): |
| 120 | + ''' |
| 121 | + returns album, metadata tuple or aborts |
| 122 | + ''' |
| 123 | + alb = lib.album(alkey, index) |
| 124 | + |
| 125 | + if not alb: |
| 126 | + abort(404) |
| 127 | + |
| 128 | + try: |
| 129 | + metadata = lib.metadata(base, alb) |
| 130 | + except FileNotFoundError: |
| 131 | + abort(404) |
| 132 | + |
| 133 | + if not metadata: |
| 134 | + raise ValueError('{} has no METADATA'.format(quote(alkey))) |
| 135 | + |
| 136 | + return alb, metadata |
| 137 | + |
| 138 | + |
| 139 | +def serve_album(alkey, album, metadata, track_ind=None): |
| 140 | + |
| 141 | + if track_ind is not None: |
| 142 | + autoplay = True |
| 143 | + lib.update_history(alkey) |
| 144 | + else: |
| 145 | + autoplay = False |
| 146 | + track_ind = 0 |
| 147 | + |
| 148 | + mkey = sorted(metadata.keys())[track_ind] |
| 149 | + tnum = track_ind + 1 |
| 150 | + track = metadata[mkey] |
| 151 | + |
| 152 | + try: |
| 153 | + tags = lib.tags(base, alkey, track) |
| 154 | + except AttributeError: |
| 155 | + raise ValueError("NO TAGS: album/{}".format(quote(alkey))) |
| 156 | + |
| 157 | + terms = lib.terms(metadata, track) |
| 158 | + control = lib.control(base, index, metadata, track_ind) |
| 159 | + |
| 160 | + return render_template( |
| 161 | + 'album.html', |
| 162 | + album=album, |
| 163 | + alkey=alkey, |
| 164 | + autoplay=autoplay, |
| 165 | + base=base, |
| 166 | + control=control, |
| 167 | + index=index, |
| 168 | + info=lib.info(track, metadata), |
| 169 | + metadata=metadata, |
| 170 | + mkey=mkey, |
| 171 | + personnel=lib.personnel(metadata), |
| 172 | + related=lib.related(index, terms), |
| 173 | + tags=tags, |
| 174 | + terms=terms, |
| 175 | + tnum=tnum, |
| 176 | + track=track) |
0 commit comments