add playlist support to database functions

This commit is contained in:
iou1name 2025-02-11 15:29:48 -05:00
parent 8dd2f0830d
commit 3b7c40c115

View File

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