diff --git a/database.py b/database.py index 76933d2..339062d 100644 --- a/database.py +++ b/database.py @@ -8,12 +8,19 @@ from urllib import parse import config -async def get_random_track(request): +async def get_random_track(request, playlist_name=""): """Selects a random track.""" async with request.app.state.db_pool.acquire() as conn: - track = await conn.fetchrow( - "SELECT * FROM track ORDER BY random() LIMIT 1" - ) + if playlist_name: + track = await conn.fetchrow( + "SELECT * FROM track " + "LEFT JOIN playlist_track ON (track.filepath = playlist_track.filepath) " + "LEFT JOIN playlist ON (playlist_track.playlist_id = playlist.id) " + "WHERE playlist.name = $1ORDER BY random() LIMIT 1", + playlist_name) + else: + track = await conn.fetchrow( + "SELECT * FROM track ORDER BY random() LIMIT 1") track = dict(track) fpath = track.pop('filepath') art256 = os.path.join(os.path.dirname(fpath), 'folder-256x256.jpg') @@ -28,25 +35,39 @@ async def get_random_track(request): return track -async def get_artists(request): +async def get_artists(request, playlist_name=""): """Returns all artists in the database.""" async with request.app.state.db_pool.acquire() as conn: - artists = await conn.fetch( - "SELECT DISTINCT albumartist FROM track ORDER BY albumartist" - ) + if playlist_name: + artists = await conn.fetch( + "SELECT DISTINCT track.albumartist FROM track " + "LEFT JOIN playlist_track ON (track.filepath = playlist_track.filepath) " + "LEFT JOIN playlist ON (playlist_track.playlist_id = playlist.id) " + "WHERE playlist.name = $1", playlist_name) + else: + artists = await conn.fetch( + "SELECT DISTINCT albumartist FROM track ORDER BY albumartist") artists = [a['albumartist'] for a in artists] return artists -async def get_albums(request, albumartist): +async def get_albums(request, albumartist, playlist_name=""): """Return all albums associated with a particular albumartist.""" async with request.app.state.db_pool.acquire() as conn: - albums = await conn.fetch( - "SELECT DISTINCT ON (album, date) filepath, album, date " - "FROM track " - "WHERE albumartist = $1 ORDER BY date", - albumartist - ) + if playlist_name: + albums = await conn.fetch( + "SELECT DISTINCT ON (track.album, track.date) track.filepath, track.album, track.date " + "FROM track " + "LEFT JOIN playlist_track ON (track.filepath = playlist_track.filepath) " + "LEFT JOIN playlist ON (playlist_track.playlist_id = playlist.id) " + "WHERE track.albumartist = $1 AND playlist.name = $2 ORDER BY date", + albumartist, playlist_name) + else: + albums = await conn.fetch( + "SELECT DISTINCT ON (album, date) filepath, album, date " + "FROM track " + "WHERE albumartist = $1 ORDER BY date", + albumartist) albums = [dict(a) for a in albums] for album in albums: fpath = album.pop('filepath') @@ -55,16 +76,25 @@ async def get_albums(request, albumartist): return albums -async def get_tracks(request, albumartist, album, date): +async def get_tracks(request, albumartist, album, date, playlist_name=""): """Select an album and return all associated tracks.""" async with request.app.state.db_pool.acquire() as conn: - tracks = await conn.fetch( - "SELECT * FROM track " - "WHERE albumartist = $1 AND " - "album = $2 AND date = $3 " - "ORDER BY discnumber ASC, tracknumber ASC", - albumartist, album, date - ) + if playlist_name: + tracks = await conn.fetch( + "SELECT * FROM track " + "LEFT JOIN playlist_track ON (track.filepath = playlist_track.filepath) " + "LEFT JOIN playlist ON (playlist_track.playlist_id = playlist.id) " + "WHERE track.albumartist = $1 AND " + "track.album = $2 AND track.date = $3 AND playlist.name = $4 " + "ORDER BY track.discnumber ASC, track.tracknumber ASC", + albumartist, album, date, playlist_name) + else: + tracks = await conn.fetch( + "SELECT * FROM track " + "WHERE albumartist = $1 AND " + "album = $2 AND date = $3 " + "ORDER BY discnumber ASC, tracknumber ASC", + albumartist, album, date) tracks = [dict(t) for t in tracks] for track in tracks: track['filepath'] = convert_filepath(track['filepath'])