Pyrite/pyrite.py

57 lines
1.7 KiB
Python
Raw Normal View History

2024-10-29 10:53:46 -04:00
#!/usr/bin/env python3
"""
A music steaming application.
"""
import random
2024-12-20 21:49:51 -05:00
import asyncpg
2024-10-29 10:53:46 -04:00
from fastapi import FastAPI, Request
from fastapi.templating import Jinja2Templates
from fastapi.staticfiles import StaticFiles
from fastapi.responses import HTMLResponse
2024-12-20 21:49:51 -05:00
import config
2024-10-29 10:53:46 -04:00
#import buckler_fastapi
2024-12-20 21:49:51 -05:00
import database as db
2024-10-29 10:53:46 -04:00
app = FastAPI()
#app.add_middleware(buckler_fastapi.BucklerSessionMiddleware)
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="templates")
2024-12-20 21:49:51 -05:00
2024-10-29 10:53:46 -04:00
@app.get("/", response_class=HTMLResponse)
async def index(request: Request):
2025-02-11 16:13:46 -05:00
playlist_name = request.cookies.get('playlist', '')
artists = await db.get_artists(request, playlist_name)
playlists = await db.get_playlists(request)
context = {'request': request, 'artists': artists, 'playlists': playlists}
2024-10-29 10:53:46 -04:00
return templates.TemplateResponse('index.html', context)
2024-11-12 13:49:35 -05:00
2024-10-29 10:53:46 -04:00
@app.get("/rand_track/")
2025-02-11 16:13:46 -05:00
async def get_rand_track(request: Request, playlist: str = ''):
2024-10-29 10:53:46 -04:00
"""Return a random track."""
2025-02-11 16:13:46 -05:00
track = await db.get_random_track(request, playlist)
2024-12-20 21:49:51 -05:00
return track
2025-01-30 09:14:31 -05:00
@app.get("/albums/")
2025-02-11 16:13:46 -05:00
async def get_albums(request: Request, albumartist: str, playlist: str = ''):
2025-01-30 09:14:31 -05:00
"""Return all albums associated with a particular albumartist."""
2025-02-11 16:13:46 -05:00
albums = await db.get_albums(request, albumartist, playlist)
2025-01-30 09:14:31 -05:00
return albums
2025-01-30 16:19:40 -05:00
@app.get("/tracks/")
2025-02-11 16:13:46 -05:00
async def get_tracks(request: Request, albumartist: str, album: str, date: str, playlist: str = ''):
2025-01-30 16:19:40 -05:00
"""Select an album and return all associated tracks."""
2025-02-11 16:13:46 -05:00
tracks = await db.get_tracks(request, albumartist, album, date, playlist)
2025-01-30 16:19:40 -05:00
return tracks
2024-12-20 21:49:51 -05:00
@app.on_event("startup")
async def startup():
app.state.db_pool = await asyncpg.create_pool(**config.db)