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: class Track:
def __init__(self, filepath=None, d=None): def __init__(self, filepath=None, d=None, coverart=""):
if d: if d:
for attr, value in d.items(): for attr, value in d.items():
setattr(self, attr, value) 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.length += str(int(m.info.length) % 60) self.length += str(int(m.info.length) % 60)
self.filepath = filepath self.filepath = filepath
self.coverart = coverart
def build_library(root_dir): 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: if not os.path.splitext(file)[1][1:] in MUSIC_EXT:
continue continue
filepath = os.path.join(root_dir, dir_name, file) 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) tracks.append(track)
print("Done") print("Done")
return tracks return tracks
@ -125,6 +130,11 @@ class Selection(Resource):
artist=track.artist, artist=track.artist,
album=track.album, album=track.album,
track=track.title) track=track.title)
found['coverart'] = url_for(
'coverart',
artist=track.artist,
album=track.album,
track=track.title)
return found return found
elif args.get('album'): elif args.get('album'):
@ -190,36 +200,21 @@ def stream(artist, album, track):
return Response(generate(), mimetype="audio/ogg") return Response(generate(), mimetype="audio/ogg")
@app.route('/get_shuffle') @app.route('/coverart/<artist>/<album>/<track>')
def shuffle(): def coverart(artist, album, track):
"""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):
"""View for the raw audio file.""" """View for the raw audio file."""
path = os.path.join(MUSIC_DIR, cover) for t in tracks:
path = os.path.abspath(path) if (t.artist == artist and
if not path.startswith(MUSIC_DIR): 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" return "False"
if not os.path.isfile(path):
return "False"
return send_file(path)
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -74,7 +74,5 @@ function change_track(track) {
player.play(); player.play();
document.getElementById('nowPlaying').innerHTML = track.title; document.getElementById('nowPlaying').innerHTML = track.title;
//let arr = track.split('/'); document.getElementById('albumCover').firstChild.src = track.coverart;
//let art = document.getElementById('albumCover');
//art.firstChild.src = '/musik/album_cover' + arr.slice(0, arr.length-1).join("/") + '/folder.jpg';
} }