get_rand_track()
This commit is contained in:
parent
a6c43fe468
commit
c859c4ff9b
40
database.py
Normal file
40
database.py
Normal 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
|
69
pyrite.py
69
pyrite.py
|
@ -4,12 +4,15 @@ A music steaming application.
|
|||
"""
|
||||
import random
|
||||
|
||||
import asyncpg
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.templating import Jinja2Templates
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from fastapi.responses import HTMLResponse
|
||||
|
||||
import config
|
||||
#import buckler_fastapi
|
||||
import database as db
|
||||
|
||||
app = FastAPI()
|
||||
#app.add_middleware(buckler_fastapi.BucklerSessionMiddleware)
|
||||
|
@ -17,68 +20,20 @@ app = FastAPI()
|
|||
app.mount("/static", StaticFiles(directory="static"), name="static")
|
||||
templates = Jinja2Templates(directory="templates")
|
||||
|
||||
|
||||
@app.get("/", response_class=HTMLResponse)
|
||||
async def index(request: Request):
|
||||
context = {"request": request}
|
||||
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/")
|
||||
async def get_rand_track():
|
||||
async def get_rand_track(request: Request):
|
||||
"""Return a random track."""
|
||||
tracks = [{
|
||||
'title': "Blade Runner",
|
||||
'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",
|
||||
'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': "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)
|
||||
track = await db.get_random_track(request)
|
||||
return track
|
||||
|
||||
|
||||
@app.on_event("startup")
|
||||
async def startup():
|
||||
app.state.db_pool = await asyncpg.create_pool(**config.db)
|
||||
|
|
Loading…
Reference in New Issue
Block a user