added coverart

This commit is contained in:
iou1name 2019-02-05 13:04:10 -05:00
parent a42165720d
commit 4adc62f499
2 changed files with 26 additions and 33 deletions

View File

@ -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/<path:cover>')
def album_cover(cover):
@app.route('/coverart/<artist>/<album>/<track>')
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__":

View File

@ -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;
}