api and webpage are working

This commit is contained in:
iou1name 2019-02-05 11:23:41 -05:00
parent a4af2a1fd5
commit 9cd963f910
4 changed files with 104 additions and 109 deletions

View File

@ -8,7 +8,7 @@ import random
import subprocess
from urllib import parse
from flask import Flask, Response, render_template, send_file
from flask import Flask, Response, render_template, send_file, url_for
from flask_restful import reqparse, abort, Api, Resource
import mutagen
@ -78,8 +78,8 @@ tracks = init_library()
@app.route('/')
def index():
"""Main index page."""
artist = list(set(t.artist for t in tracks))
artist.sort()
artists = list(set(t.artist for t in tracks))
artists.sort()
return render_template('index.html', **locals())
@ -106,50 +106,61 @@ class Selection(Resource):
def get(self):
global tracks
args = parser.parse_args()
print(args)
validate_select_args(args)
if args.get('track'):
track = [t for t in tracks
if (t.title == args.get('track') and
t.album == args.get('album') and
t.artist == args.get('artist'))]
if not track:
abort(404, message="Track does not exist.")
for track in tracks:
if (track.artist == args.get('artist') and
track.album == args.get('album') and
track.title == args.get('track')):
break
else:
return vars(track[0])
abort(404, message="Track does not exist.")
found = dict(vars(track))
found.pop('filepath')
found['streampath'] = url_for(
'stream',
artist=track.artist,
album=track.album,
track=track.title)
return found
elif args.get('album'):
tracks = [t for t in tracks
if (t.album == args.get('album') and
t.artist == args.get('artist'))]
if not tracks:
found = []
for track in tracks:
if (track.artist == args.get('artist') and
track.album == args.get('album')):
found.append(track)
if not found:
abort(404, message="Album does not exist.")
else:
return [t.title for t in tracks]
return [t.title for t in found]
elif args.get('artist'):
albums = list(set(t.album for t in tracks
if t.artist == args.get('artist')))
if not albums:
found = []
for track in tracks:
if track.artist == args.get('artist'):
found.append(track)
if not found:
abort(404, message="Artist does not exist.")
else:
return sorted(albums)
found = list(set(t.album for t in found))
return sorted(found)
api.add_resource(Selection, '/select')
api.init_app(app)
@app.route('/stream/<path:track>')
def stream(track):
@app.route('/stream/<artist>/<album>/<track>')
def stream(artist, album, track):
"""View for the raw audio file."""
track = parse.unquote(track)
path = os.path.join(MUSIC_DIR, track)
path = os.path.abspath(path)
if not path.startswith(MUSIC_DIR):
return "False"
if not os.path.isfile(path):
return "False"
FFMPEG_CMD[5] = path
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.")
FFMPEG_CMD[5] = t.filepath
def generate():
with subprocess.Popen(FFMPEG_CMD, stdout=subprocess.PIPE) as proc:

View File

@ -28,32 +28,7 @@ img {
border-bottom: 2px solid #ccc;
}
#selectArtistContainer {
height: 100%;
width: 100%;
}
#selectArtist {
height: 100%;
width: 100%;
}
#selectAlbumContainer {
height: 100%;
width: 100%;
}
#selectAlbum {
height: 100%;
width: 100%;
}
#selectTrackContainer {
height: 100%;
width: 100%;
}
#selectTrack {
.list {
height: 100%;
width: 100%;
}

View File

@ -17,20 +17,58 @@ function load() {
});
}
function navigate(item) {
if (/\..{3,5}$/.test(item)) {
select_track(item);
}
else {
get_dir(item);
}
function select_artist(select) {
var httpRequest;
httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState !== XMLHttpRequest.DONE) { return; }
if (httpRequest.status !== 200) { return; }
nav_items = JSON.parse(httpRequest.responseText);
let html_str = '';
for (let i = 0; i < nav_items.length; i++) {
html_str += '<option value="' + nav_items[i] + '">' + nav_items[i] + '</option>';
}
document.getElementById('albumList').innerHTML = html_str;
};
httpRequest.open('GET', api_uri + '?artist=' + select.value, true);
httpRequest.send();
}
function select_track(item) {
let cd = document.getElementById('currentDirectory').innerText;
let track = cd + item;
change_track(track);
function select_album(select) {
var httpRequest;
httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState !== XMLHttpRequest.DONE) { return; }
if (httpRequest.status !== 200) { return; }
nav_items = JSON.parse(httpRequest.responseText);
let html_str = '';
for (let i = 0; i < nav_items.length; i++) {
html_str += '<option value="' + nav_items[i] + '">' + nav_items[i] + '</option>';
}
document.getElementById('trackList').innerHTML = html_str;
};
httpRequest.open('GET', api_uri + '?artist=' + document.getElementById('artistList').value + '&album=' + select.value, true);
httpRequest.send();
}
function select_track(select) {
var httpRequest;
httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState !== XMLHttpRequest.DONE) { return; }
if (httpRequest.status !== 200) { return; }
let track = JSON.parse(httpRequest.responseText);
console.log(track);
let source = document.getElementById('stream');
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.send();
}
function change_track(track) {
@ -45,34 +83,3 @@ function change_track(track) {
let art = document.getElementById('albumCover');
art.firstChild.src = '/musik/album_cover' + arr.slice(0, arr.length-1).join("/") + '/folder.jpg';
}
function get_dir(item) {
let cd = document.getElementById('currentDirectory').innerText;
if (item === "../") {
if (cd !== "/") {
item = cd.slice(0, cd.slice(0, -1).lastIndexOf("/")+1);
}
else {
return;
}
}
else {
item = cd + item;
}
var httpRequest;
httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState !== XMLHttpRequest.DONE) { return; }
if (httpRequest.status !== 200) { return; }
nav_items = JSON.parse(httpRequest.responseText);
document.getElementById('currentDirectory').innerText = nav_items.shift();
let html_str = '';
for (let i = 0; i < nav_items.length; i++) {
html_str += `<li><a href="javascript:void(0);" onclick="navigate(\`${nav_items[i]}\`)">${nav_items[i]}</a></li>`;
}
document.getElementById('navItems').innerHTML = html_str;
};
httpRequest.open('GET', '/musik/get_dir/' + item, true);
httpRequest.send();
}

View File

@ -4,26 +4,28 @@
<title>Musik</title>
<link rel="stylesheet" type="text/css" href="/static/musik.css">
<script type="text/javascript" src="/static/musik.js"></script>
<script>
const api_uri = "{{ url_for('selection') }}";
</script>
<script>window.onload = load;</script>
</head>
<body>
<div id="globalContainer">
<div id="titleContainer"><h1>Musik</h1></div>
<div id="navigationContainer">
<div id="selectArtistContainer">
<select id="selectArtist" size="2">
<div id="artistListContainer" class="list">
<select id="artistList" size="2" class="list" onchange="select_artist(this)">
{% for artist in artists %}
<!--<li><a href="javascript:void(0);" onclick="navigate('{{ item }}')">{{ item }}</a></li>-->
<option>{{ artist }}</option>
<option value="{{ artist }}">{{ artist }}</option>
{% endfor %}
</select>
</div>
<div id="selectAlbumContainer">
<select id="selectAlbum" size="2">
<div id="albumListContainer" class="list">
<select id="albumList" size="2" class="list" onchange="select_album(this)">
</select>
</div>
<div id="selectTrackContainer">
<select id="selectTrack" size="2">
<div id="trackListContainer" class="list">
<select id="trackList" size="2" class="list" onchange="select_track(this)">
</select>
</div>
</div>