From a42165720d6215c6f496c47b2a930c0e42ecb73c Mon Sep 17 00:00:00 2001 From: iou1name Date: Tue, 5 Feb 2019 12:44:31 -0500 Subject: [PATCH] added shuffle ability --- musik.py | 42 +++++++++++++++--------------------------- static/musik.js | 24 ++++++++++-------------- 2 files changed, 25 insertions(+), 41 deletions(-) diff --git a/musik.py b/musik.py index 61401b7..423ed84 100755 --- a/musik.py +++ b/musik.py @@ -149,7 +149,22 @@ class Selection(Resource): found = list(set(t.album for t in found)) return sorted(found) + +class RandomSelection(Resource): + def get(self): + global tracks + track = random.choice(tracks) + found = dict(vars(track)) + found.pop('filepath') + found['streampath'] = url_for( + 'stream', + artist=track.artist, + album=track.album, + track=track.title) + return found + api.add_resource(Selection, '/select') +api.add_resource(RandomSelection, '/select/random') api.init_app(app) @@ -175,33 +190,6 @@ def stream(artist, album, track): return Response(generate(), mimetype="audio/ogg") -@app.route('/get_dir//') -@app.route('/get_dir/') -def get_dir(directory=""): - """Returns the contents of the requested directory.""" - directory = directory.replace("DOTDOT", "..") - directory = os.path.join(MUSIC_DIR, directory) - directory = os.path.abspath(directory) - if not directory.startswith(MUSIC_DIR): - return "False" - if not os.path.isdir(directory): - return "False" - - nav_items = os.listdir(directory) - nav_items.sort() - if directory != MUSIC_DIR: - nav_items = [".."] + nav_items - - nav_items_new = [] - for item in nav_items: - if os.path.isdir(os.path.join(directory, item)): - item += '/' - nav_items_new.append(item) - nav_items_new = [directory.replace(MUSIC_DIR, '') + '/'] + nav_items_new - - return json.dumps(nav_items_new) - - @app.route('/get_shuffle') def shuffle(): """Returns a randomly selected track from the library.""" diff --git a/static/musik.js b/static/musik.js index e58a5ad..1068754 100644 --- a/static/musik.js +++ b/static/musik.js @@ -5,13 +5,14 @@ function load() { if (document.getElementById('shuffle').checked) { var httpRequest; httpRequest = new XMLHttpRequest(); - httpRequest.onreadystatechange = function() { + httpRequest.onreadystatechange = function () { if (httpRequest.readyState !== XMLHttpRequest.DONE) { return; } if (httpRequest.status !== 200) { return; } - let track = httpRequest.responseText; + + let track = JSON.parse(httpRequest.responseText); change_track(track); }; - httpRequest.open('GET', '/musik/get_shuffle', true); + httpRequest.open('GET', api_uri + '/random', true); httpRequest.send(); } }); @@ -59,12 +60,7 @@ function select_track(select) { if (httpRequest.status !== 200) { return; } let track = JSON.parse(httpRequest.responseText); - let source = document.getElementById('stream'); - source.src = track.streampath; - let player = document.getElementById('player'); - player.load(); - player.play(); - document.getElementById('nowPlaying').innerHTML = track.title; + change_track(track); }; httpRequest.open('GET', api_uri + '?artist=' + document.getElementById('artistList').value + '&album=' + document.getElementById('albumList').value + '&track=' + select.value, true); httpRequest.send(); @@ -72,13 +68,13 @@ function select_track(select) { function change_track(track) { let source = document.getElementById('stream'); - source.src = '/musik/stream' + encodeURIComponent(track); + source.src = track.streampath; let player = document.getElementById('player'); player.load(); player.play(); - document.getElementById('nowPlaying').innerHTML = track; + 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'; + //let arr = track.split('/'); + //let art = document.getElementById('albumCover'); + //art.firstChild.src = '/musik/album_cover' + arr.slice(0, arr.length-1).join("/") + '/folder.jpg'; }