added shuffle ability

This commit is contained in:
iou1name 2019-02-05 12:44:31 -05:00
parent 6c27ea933b
commit a42165720d
2 changed files with 25 additions and 41 deletions

View File

@ -149,7 +149,22 @@ class Selection(Resource):
found = list(set(t.album for t in found)) found = list(set(t.album for t in found))
return sorted(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(Selection, '/select')
api.add_resource(RandomSelection, '/select/random')
api.init_app(app) api.init_app(app)
@ -175,33 +190,6 @@ def stream(artist, album, track):
return Response(generate(), mimetype="audio/ogg") return Response(generate(), mimetype="audio/ogg")
@app.route('/get_dir/<path:directory>/')
@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') @app.route('/get_shuffle')
def shuffle(): def shuffle():
"""Returns a randomly selected track from the library.""" """Returns a randomly selected track from the library."""

View File

@ -5,13 +5,14 @@ function load() {
if (document.getElementById('shuffle').checked) { if (document.getElementById('shuffle').checked) {
var httpRequest; var httpRequest;
httpRequest = new XMLHttpRequest(); httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function() { httpRequest.onreadystatechange = function () {
if (httpRequest.readyState !== XMLHttpRequest.DONE) { return; } if (httpRequest.readyState !== XMLHttpRequest.DONE) { return; }
if (httpRequest.status !== 200) { return; } if (httpRequest.status !== 200) { return; }
let track = httpRequest.responseText;
let track = JSON.parse(httpRequest.responseText);
change_track(track); change_track(track);
}; };
httpRequest.open('GET', '/musik/get_shuffle', true); httpRequest.open('GET', api_uri + '/random', true);
httpRequest.send(); httpRequest.send();
} }
}); });
@ -59,12 +60,7 @@ function select_track(select) {
if (httpRequest.status !== 200) { return; } if (httpRequest.status !== 200) { return; }
let track = JSON.parse(httpRequest.responseText); let track = JSON.parse(httpRequest.responseText);
let source = document.getElementById('stream'); change_track(track);
source.src = track.streampath;
let player = document.getElementById('player');
player.load();
player.play();
document.getElementById('nowPlaying').innerHTML = track.title;
}; };
httpRequest.open('GET', api_uri + '?artist=' + document.getElementById('artistList').value + '&album=' + document.getElementById('albumList').value + '&track=' + select.value, true); httpRequest.open('GET', api_uri + '?artist=' + document.getElementById('artistList').value + '&album=' + document.getElementById('albumList').value + '&track=' + select.value, true);
httpRequest.send(); httpRequest.send();
@ -72,13 +68,13 @@ function select_track(select) {
function change_track(track) { function change_track(track) {
let source = document.getElementById('stream'); let source = document.getElementById('stream');
source.src = '/musik/stream' + encodeURIComponent(track); source.src = track.streampath;
let player = document.getElementById('player'); let player = document.getElementById('player');
player.load(); player.load();
player.play(); player.play();
document.getElementById('nowPlaying').innerHTML = track; document.getElementById('nowPlaying').innerHTML = track.title;
let arr = track.split('/'); //let arr = track.split('/');
let art = document.getElementById('albumCover'); //let art = document.getElementById('albumCover');
art.firstChild.src = '/musik/album_cover' + arr.slice(0, arr.length-1).join("/") + '/folder.jpg'; //art.firstChild.src = '/musik/album_cover' + arr.slice(0, arr.length-1).join("/") + '/folder.jpg';
} }