diff --git a/musik.py b/musik.py index 423ed84..a702d77 100755 --- a/musik.py +++ b/musik.py @@ -25,7 +25,7 @@ FFMPEG_CMD = [ ] class Track: - def __init__(self, filepath=None, d=None): + def __init__(self, filepath=None, d=None, coverart=""): if d: for attr, value in d.items(): setattr(self, attr, value) @@ -43,6 +43,7 @@ class Track: self.length = str(int(m.info.length) // 60) + ":" self.length += str(int(m.info.length) % 60) self.filepath = filepath + self.coverart = coverart def build_library(root_dir): @@ -54,7 +55,11 @@ def build_library(root_dir): if not os.path.splitext(file)[1][1:] in MUSIC_EXT: continue filepath = os.path.join(root_dir, dir_name, file) - track = Track(filepath) + if "folder.jpg" in files: + coverart = os.path.join(root_dir, dir_name, "folder.jpg") + else: + coverart = "" + track = Track(filepath, coverart=coverart) tracks.append(track) print("Done") return tracks @@ -125,6 +130,11 @@ class Selection(Resource): artist=track.artist, album=track.album, track=track.title) + found['coverart'] = url_for( + 'coverart', + artist=track.artist, + album=track.album, + track=track.title) return found elif args.get('album'): @@ -190,36 +200,21 @@ def stream(artist, album, track): return Response(generate(), mimetype="audio/ogg") -@app.route('/get_shuffle') -def shuffle(): - """Returns a randomly selected track from the library.""" - item = random.choice(os.listdir(MUSIC_DIR)) - path = os.path.join(MUSIC_DIR, item) - n = 0 - while not item.rpartition('.')[2] in MUSIC_EXT: - n += 1 - item = random.choice(os.listdir(path)) - if os.path.isdir(os.path.join(path, item)): - path = os.path.join(path, item) - if n == 5: - item = random.choice(os.listdir(MUSIC_DIR)) - path = os.path.join(MUSIC_DIR, item) - n = 0 - path = os.path.join(path, item) - return path.replace(MUSIC_DIR, '') - - -@app.route('/album_cover/') -def album_cover(cover): +@app.route('/coverart///') +def coverart(artist, album, track): """View for the raw audio file.""" - path = os.path.join(MUSIC_DIR, cover) - path = os.path.abspath(path) - if not path.startswith(MUSIC_DIR): + for t in tracks: + if (t.artist == artist and + t.album == album and + t.title == track): + break + else: + abort(404, message="Track does not exist.") + + if t.coverart: + return send_file(t.coverart) + else: return "False" - if not os.path.isfile(path): - return "False" - - return send_file(path) if __name__ == "__main__": diff --git a/static/musik.js b/static/musik.js index 1068754..646502a 100644 --- a/static/musik.js +++ b/static/musik.js @@ -74,7 +74,5 @@ function change_track(track) { player.play(); document.getElementById('nowPlaying').innerHTML = track.title; - //let arr = track.split('/'); - //let art = document.getElementById('albumCover'); - //art.firstChild.src = '/musik/album_cover' + arr.slice(0, arr.length-1).join("/") + '/folder.jpg'; + document.getElementById('albumCover').firstChild.src = track.coverart; }