get_rand_track()

This commit is contained in:
iou1name 2024-12-20 21:49:51 -05:00
parent a6c43fe468
commit c859c4ff9b
3 changed files with 52 additions and 57 deletions

40
database.py Normal file
View File

@ -0,0 +1,40 @@
#!/usr/bin/env python3
"""
Contains helper functions for accessing the database.
"""
import os
from urllib import parse
import config
async def get_random_track(request):
"""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"
)
track = dict(track)
fpath = track.pop('filepath')
art256 = os.path.join(os.path.dirname(fpath), 'folder-256x256.jpg')
art64 = os.path.join(os.path.dirname(fpath), 'folder-64x64.jpg')
track['source'] = convert_filepath(fpath)
track['artwork'] = [{
'src': convert_filepath(art256),
'sizes': '256x256',
'type': 'image/jpeg'
}, {
'src': convert_filepath(art64),
'sizes': '64x64',
'type': 'image/jpeg'
}
]
return track
def convert_filepath(path):
"""Convert a filepath to a URL."""
path = os.path.relpath(path, config.music_dir)
path = config.server_homepage + '/library/' + path
#path = parse.quote(path)
return path

View File

@ -4,12 +4,15 @@ A music steaming application.
""" """
import random import random
import asyncpg
from fastapi import FastAPI, Request from fastapi import FastAPI, Request
from fastapi.templating import Jinja2Templates from fastapi.templating import Jinja2Templates
from fastapi.staticfiles import StaticFiles from fastapi.staticfiles import StaticFiles
from fastapi.responses import HTMLResponse from fastapi.responses import HTMLResponse
import config
#import buckler_fastapi #import buckler_fastapi
import database as db
app = FastAPI() app = FastAPI()
#app.add_middleware(buckler_fastapi.BucklerSessionMiddleware) #app.add_middleware(buckler_fastapi.BucklerSessionMiddleware)
@ -17,68 +20,20 @@ app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static") app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="templates") templates = Jinja2Templates(directory="templates")
@app.get("/", response_class=HTMLResponse) @app.get("/", response_class=HTMLResponse)
async def index(request: Request): async def index(request: Request):
context = {"request": request} context = {"request": request}
return templates.TemplateResponse('index.html', context) return templates.TemplateResponse('index.html', context)
@app.get("/player", response_class=HTMLResponse)
async def player(request: Request):
context = {"request": request}
return templates.TemplateResponse('player.html', context)
@app.get("/rand_track/") @app.get("/rand_track/")
async def get_rand_track(): async def get_rand_track(request: Request):
"""Return a random track.""" """Return a random track."""
tracks = [{ track = await db.get_random_track(request)
'title': "Blade Runner", return track
'artist': "Beast In Black",
'album': "Dark Connection",
'source': "https://steelbea.me/scorch_test/library/Beast%20In%20Black/Beast%20In%20Black%20-%20Dark%20Connection%20%282021%29%20%5BFLAC%5D%20%7BNB62072%7D/01%20-%20Blade%20Runner.opus", @app.on_event("startup")
'artwork': [{ async def startup():
'src': "https://steelbea.me/scorch_test/library/Beast%20In%20Black/Beast%20In%20Black%20-%20Dark%20Connection%20%282021%29%20%5BFLAC%5D%20%7BNB62072%7D/folder-256x256.jpg", app.state.db_pool = await asyncpg.create_pool(**config.db)
'sizes': "256x256",
'type': "image/jpeg"
}]
}, {
'title': "Bella Donna",
'artist': "Beast In Black",
'album': "Dark Connection",
'source': "https://steelbea.me/scorch_test/library/Beast%20In%20Black/Beast%20In%20Black%20-%20Dark%20Connection%20%282021%29%20%5BFLAC%5D%20%7BNB62072%7D/02%20-%20Bella%20Donna.opus",
'artwork': [{
'src': "https://steelbea.me/scorch_test/library/Beast%20In%20Black/Beast%20In%20Black%20-%20Dark%20Connection%20%282021%29%20%5BFLAC%5D%20%7BNB62072%7D/folder-256x256.jpg",
'sizes': "256x256",
'type': "image/jpeg"
}]
}, {
'title': "Highway To Mars",
'artist': "Beast In Black",
'album': "Dark Connection",
'source': "https://steelbea.me/scorch_test/library/Beast%20In%20Black/Beast%20In%20Black%20-%20Dark%20Connection%20%282021%29%20%5BFLAC%5D%20%7BNB62072%7D/03%20-%20Highway%20To%20Mars.opus",
'artwork': [{
'src': "https://steelbea.me/scorch_test/library/Beast%20In%20Black/Beast%20In%20Black%20-%20Dark%20Connection%20%282021%29%20%5BFLAC%5D%20%7BNB62072%7D/folder-256x256.jpg",
'sizes': "256x256",
'type': "image/jpeg"
}]
}, {
'title': "Hardcore",
'artist': "Beast In Black",
'album': "Dark Connection",
'source': "https://steelbea.me/scorch_test/library/Beast%20In%20Black/Beast%20In%20Black%20-%20Dark%20Connection%20%282021%29%20%5BFLAC%5D%20%7BNB62072%7D/04%20-%20Hardcore.opus",
'artwork': [{
'src': "https://steelbea.me/scorch_test/library/Beast%20In%20Black/Beast%20In%20Black%20-%20Dark%20Connection%20%282021%29%20%5BFLAC%5D%20%7BNB62072%7D/folder-256x256.jpg",
'sizes': "256x256",
'type': "image/jpeg"
}]
}, {
'title': "One Night In Tokyo",
'artist': "Beast In Black",
'album': "Dark Connection",
'source': "https://steelbea.me/scorch_test/library/Beast%20In%20Black/Beast%20In%20Black%20-%20Dark%20Connection%20%282021%29%20%5BFLAC%5D%20%7BNB62072%7D/05%20-%20One%20Night%20In%20Tokyo.opus",
'artwork': [{
'src': "https://steelbea.me/scorch_test/library/Beast%20In%20Black/Beast%20In%20Black%20-%20Dark%20Connection%20%282021%29%20%5BFLAC%5D%20%7BNB62072%7D/folder-256x256.jpg",
'sizes': "256x256",
'type': "image/jpeg"
}]
}]
return random.choice(tracks)