From aae1cfd34bd7998bcaa061d8e09f24e09b28187d Mon Sep 17 00:00:00 2001 From: LeoMortari Date: Tue, 23 Sep 2025 20:08:31 -0300 Subject: [PATCH] Adiciona endpoint de busca de videos --- main.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/main.py b/main.py index 33f36e8..06063b8 100644 --- a/main.py +++ b/main.py @@ -166,3 +166,36 @@ def download_video( "videoId": video_id, "filename": filename } + +@app.get("/search") +def search_youtube_yt_dlp( + q: str = Query(..., description="Termo de busca"), + max_results: int = Query(5, ge=1, le=10, description="Número de resultados (máx 10)") +): + ydl_opts = { + "quiet": True, + "extract_flat": "in_playlist", + "skip_download": True, + } + + search_query = f"ytsearch{max_results}:{q}" + + try: + with YoutubeDL(ydl_opts) as ydl: + search_result = ydl.extract_info(search_query, download=False) + entries = search_result.get("entries", [])[:max_results] + + results = [] + for item in entries: + results.append({ + "videoId": item.get("id"), + "title": item.get("title"), + "duration": item.get("duration"), + "url": item.get("webpage_url"), + "channel": item.get("uploader"), + "thumbnail": item.get("thumbnail"), + }) + return {"results": results} + + except Exception as e: + raise HTTPException(status_code=500, detail=f"Erro ao buscar vídeos: {e}") \ No newline at end of file