Implement function for random endpoint, not sure if it works
Build and publish bnuy api / build (push) Successful in 13m13s
Build and publish bnuy api / build (push) Successful in 13m13s
This commit is contained in:
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user