30 lines
1007 B
Python
30 lines
1007 B
Python
from typing import Literal
|
|
|
|
from fastapi import APIRouter, HTTPException, Query
|
|
|
|
from app.services import streamrip_service
|
|
|
|
router = APIRouter(prefix="/api/search", tags=["search"])
|
|
|
|
MediaType = Literal["track", "album", "artist", "playlist", "podcast"]
|
|
|
|
|
|
@router.get("")
|
|
async def search(
|
|
type: MediaType = Query(...),
|
|
q: str = Query(..., min_length=0, max_length=200),
|
|
limit: int = Query(20, ge=1, le=100),
|
|
index: int = Query(0, ge=0, le=1000),
|
|
):
|
|
query = q.strip()
|
|
if not query:
|
|
raise HTTPException(status_code=400, detail="Empty query")
|
|
try:
|
|
if type == "podcast":
|
|
results, total = await streamrip_service.search_podcasts(query, limit, index)
|
|
else:
|
|
results, total = await streamrip_service.search_deezer(type, query, limit, index)
|
|
except Exception as e:
|
|
raise HTTPException(status_code=502, detail=f"Search failed: {e}")
|
|
return {"results": results, "type": type, "query": query, "total": total, "index": index}
|