From ec841012dd2df6de1b9c49a3774744317d9071da Mon Sep 17 00:00:00 2001 From: Michelle Date: Tue, 12 May 2026 23:26:05 +0200 Subject: [PATCH] Implement function for random endpoint, not sure if it works --- main.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index ec29ab8..36eee87 100644 --- a/main.py +++ b/main.py @@ -69,11 +69,22 @@ async def create_tables(pool): @app.get("/") async def root(): - return {"message": "yes the api works"} + return {"message": "yes the api works, maybe i will create a small landing page later here"} @app.get("/random") async def get_random_bnuy(): - return {"message": "here could be a bnuy, if I would've implemented it"} + async with app.state.pool.acquire() as conn: + async with conn.cursor() as cursor: + await cursor.execute("SELECT filename, subreddit, url FROM images ORDER BY RAND() LIMIT 1;") + result = await cursor.fetchone() + if result: + filepath = os.path.join("data/images", result[0]) + if os.path.exists(filepath): + return {"file": f"/images/{result[0]}", "source": f"https://www.reddit.com/r/{result[1]}/", "original_url": result[2]} + else: + raise HTTPException(status_code=404, detail="Image file not found") + else: + raise HTTPException(status_code=404, detail="No available images found") @app.get("/images/{filename}") async def get_image(filename: str):