#!/usr/bin/env python3 """ 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) app.mount("/static", StaticFiles(directory="static"), name="static") templates = Jinja2Templates(directory="templates") @app.get("/", response_class=HTMLResponse) async def index(request: Request): 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} return templates.TemplateResponse('index.html', context) @app.get("/rand_track/") async def get_rand_track(request: Request, playlist: str = ''): """Return a random track.""" track = await db.get_random_track(request, playlist) return track @app.get("/albums/") async def get_albums(request: Request, albumartist: str, playlist: str = ''): """Return all albums associated with a particular albumartist.""" albums = await db.get_albums(request, albumartist, playlist) return albums @app.get("/tracks/") async def get_tracks(request: Request, albumartist: str, album: str, date: str, playlist: str = ''): """Select an album and return all associated tracks.""" tracks = await db.get_tracks(request, albumartist, album, date, playlist) return tracks @app.on_event("startup") async def startup(): app.state.db_pool = await asyncpg.create_pool(**config.db)