added shuffle ability
This commit is contained in:
parent
6c27ea933b
commit
a42165720d
42
musik.py
42
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/<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')
|
||||
def shuffle():
|
||||
"""Returns a randomly selected track from the library."""
|
||||
|
|
|
@ -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';
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user