Update app.py
Browse files
app.py
CHANGED
|
@@ -8,9 +8,33 @@ import uuid
|
|
| 8 |
import json
|
| 9 |
from datetime import datetime
|
| 10 |
import mimetypes
|
|
|
|
| 11 |
|
| 12 |
app = FastAPI()
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
# In-memory storage for albums
|
| 15 |
albums = {}
|
| 16 |
|
|
@@ -302,9 +326,12 @@ async def index():
|
|
| 302 |
@app.get("/search", response_class=HTMLResponse)
|
| 303 |
async def search_albums(query: str = ""):
|
| 304 |
query = query.lower()
|
|
|
|
|
|
|
| 305 |
matching_albums = {
|
| 306 |
-
|
| 307 |
-
|
|
|
|
| 308 |
}
|
| 309 |
|
| 310 |
results_html = ""
|
|
@@ -324,28 +351,6 @@ async def search_albums(query: str = ""):
|
|
| 324 |
results=results_html
|
| 325 |
)
|
| 326 |
|
| 327 |
-
@app.post("/upload")
|
| 328 |
-
async def handle_upload(request: Request, file: UploadFile = File(...)):
|
| 329 |
-
if not file:
|
| 330 |
-
return {"error": "No file uploaded"}, 400
|
| 331 |
-
|
| 332 |
-
cookies = await get_cookies()
|
| 333 |
-
if 'csrftoken' not in cookies or 'sessionid' not in cookies:
|
| 334 |
-
return {"error": "Failed to get cookies"}, 500
|
| 335 |
-
|
| 336 |
-
upload_result = await initiate_upload(cookies, file.filename, file.content_type)
|
| 337 |
-
if not upload_result or 'upload_url' not in upload_result:
|
| 338 |
-
return {"error": "Failed to initiate upload"}, 500
|
| 339 |
-
|
| 340 |
-
file_content = await file.read()
|
| 341 |
-
upload_success = await retry_upload(upload_result['upload_url'], file_content, file.content_type)
|
| 342 |
-
if not upload_success:
|
| 343 |
-
return {"error": "Upload failed"}, 500
|
| 344 |
-
|
| 345 |
-
base_url = str(request.base_url).rstrip('/')
|
| 346 |
-
redirect_url = f"{base_url}/upload/{upload_result['serving_url'].split('/pbxt/')[1]}"
|
| 347 |
-
return RedirectResponse(url=redirect_url, status_code=303)
|
| 348 |
-
|
| 349 |
@app.post("/album/create")
|
| 350 |
async def create_album(
|
| 351 |
request: Request,
|
|
@@ -378,6 +383,9 @@ async def create_album(
|
|
| 378 |
'files': album_files
|
| 379 |
}
|
| 380 |
|
|
|
|
|
|
|
|
|
|
| 381 |
base_url = str(request.base_url).rstrip('/')
|
| 382 |
return RedirectResponse(url=f"{base_url}/album/{album_id}", status_code=303)
|
| 383 |
|
|
@@ -506,4 +514,4 @@ async def retry_upload(upload_url, file_content, content_type, max_retries=5):
|
|
| 506 |
|
| 507 |
if __name__ == "__main__":
|
| 508 |
import uvicorn
|
| 509 |
-
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
|
| 8 |
import json
|
| 9 |
from datetime import datetime
|
| 10 |
import mimetypes
|
| 11 |
+
import httpx
|
| 12 |
|
| 13 |
app = FastAPI()
|
| 14 |
|
| 15 |
+
DENO_API_URL = "https://dataerrr99.deno.dev"
|
| 16 |
+
|
| 17 |
+
async def save_album_to_db(album_name: str, album_id: str):
|
| 18 |
+
async with httpx.AsyncClient() as client:
|
| 19 |
+
try:
|
| 20 |
+
response = await client.post(DENO_API_URL, json={
|
| 21 |
+
"albumName": album_name,
|
| 22 |
+
"albumLink": album_id
|
| 23 |
+
})
|
| 24 |
+
return response.json()
|
| 25 |
+
except Exception as e:
|
| 26 |
+
print(f"Error saving to DB: {str(e)}")
|
| 27 |
+
return None
|
| 28 |
+
|
| 29 |
+
async def get_albums_from_db():
|
| 30 |
+
async with httpx.AsyncClient() as client:
|
| 31 |
+
try:
|
| 32 |
+
response = await client.get(DENO_API_URL)
|
| 33 |
+
return response.json()
|
| 34 |
+
except Exception as e:
|
| 35 |
+
print(f"Error fetching from DB: {str(e)}")
|
| 36 |
+
return {"data": []}
|
| 37 |
+
|
| 38 |
# In-memory storage for albums
|
| 39 |
albums = {}
|
| 40 |
|
|
|
|
| 326 |
@app.get("/search", response_class=HTMLResponse)
|
| 327 |
async def search_albums(query: str = ""):
|
| 328 |
query = query.lower()
|
| 329 |
+
db_albums = await get_albums_from_db()
|
| 330 |
+
|
| 331 |
matching_albums = {
|
| 332 |
+
album['link']: albums[album['link']]
|
| 333 |
+
for album in db_albums['data']
|
| 334 |
+
if album['link'] in albums and query in album['name'].lower()
|
| 335 |
}
|
| 336 |
|
| 337 |
results_html = ""
|
|
|
|
| 351 |
results=results_html
|
| 352 |
)
|
| 353 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 354 |
@app.post("/album/create")
|
| 355 |
async def create_album(
|
| 356 |
request: Request,
|
|
|
|
| 383 |
'files': album_files
|
| 384 |
}
|
| 385 |
|
| 386 |
+
# Save to Deno KV database
|
| 387 |
+
await save_album_to_db(album_name, album_id)
|
| 388 |
+
|
| 389 |
base_url = str(request.base_url).rstrip('/')
|
| 390 |
return RedirectResponse(url=f"{base_url}/album/{album_id}", status_code=303)
|
| 391 |
|
|
|
|
| 514 |
|
| 515 |
if __name__ == "__main__":
|
| 516 |
import uvicorn
|
| 517 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|