Neo2902 commited on
Commit ·
5d5a3ff
1
Parent(s): c809adb
Projet complet avec chunking, extract_playlist et youtube_search
Browse files
app.py
CHANGED
|
@@ -1,27 +1,14 @@
|
|
| 1 |
-
import sys
|
| 2 |
-
import os
|
| 3 |
-
import subprocess
|
| 4 |
-
|
| 5 |
-
# Installer Playwright au démarrage (avant les imports FastAPI)
|
| 6 |
-
subprocess.run(["playwright", "install"], check=True)
|
| 7 |
-
|
| 8 |
-
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
|
| 9 |
-
|
| 10 |
-
import chunking
|
| 11 |
-
import extract_playlist_playwright
|
| 12 |
-
import youtube_search
|
| 13 |
-
|
| 14 |
from fastapi import FastAPI, Request
|
| 15 |
-
from fastapi.responses import JSONResponse
|
| 16 |
-
import asyncio
|
| 17 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 18 |
|
| 19 |
app = FastAPI()
|
| 20 |
|
| 21 |
origins = [
|
| 22 |
-
"http://localhost:8000",
|
| 23 |
-
"
|
| 24 |
-
"
|
|
|
|
| 25 |
]
|
| 26 |
|
| 27 |
app.add_middleware(
|
|
@@ -33,10 +20,8 @@ app.add_middleware(
|
|
| 33 |
)
|
| 34 |
|
| 35 |
@app.get("/")
|
| 36 |
-
async def root(
|
| 37 |
-
|
| 38 |
-
return {"message": "Logs container requested (placeholder)"}
|
| 39 |
-
return {"message": "API active et prête à recevoir des demandes sur /extract"}
|
| 40 |
|
| 41 |
@app.post("/extract")
|
| 42 |
async def extract(request: Request):
|
|
@@ -44,17 +29,10 @@ async def extract(request: Request):
|
|
| 44 |
playlist_url = data.get("playlistUrl")
|
| 45 |
if not playlist_url:
|
| 46 |
return JSONResponse(status_code=400, content={"message": "Playlist URL manquante"})
|
| 47 |
-
|
| 48 |
-
songs = await extract_playlist_playwright.extract_playlist(playlist_url)
|
| 49 |
-
batches = list(chunking.chunk_list(songs, 10))
|
| 50 |
-
|
| 51 |
-
final_results = []
|
| 52 |
-
for batch in batches:
|
| 53 |
-
tasks = [youtube_search.search_youtube(s["title"], s["artist"]) for s in batch]
|
| 54 |
-
results = await asyncio.gather(*tasks)
|
| 55 |
-
final_results.extend(results)
|
| 56 |
-
|
| 57 |
return {
|
| 58 |
-
"message":
|
| 59 |
-
"playlist":
|
|
|
|
|
|
|
| 60 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI, Request
|
|
|
|
|
|
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from fastapi.responses import JSONResponse
|
| 4 |
|
| 5 |
app = FastAPI()
|
| 6 |
|
| 7 |
origins = [
|
| 8 |
+
"http://localhost:8000",
|
| 9 |
+
"https://ton-front-netlify-url",
|
| 10 |
+
"file://",
|
| 11 |
+
"*"
|
| 12 |
]
|
| 13 |
|
| 14 |
app.add_middleware(
|
|
|
|
| 20 |
)
|
| 21 |
|
| 22 |
@app.get("/")
|
| 23 |
+
async def root():
|
| 24 |
+
return {"message": "API HF space ready"}
|
|
|
|
|
|
|
| 25 |
|
| 26 |
@app.post("/extract")
|
| 27 |
async def extract(request: Request):
|
|
|
|
| 29 |
playlist_url = data.get("playlistUrl")
|
| 30 |
if not playlist_url:
|
| 31 |
return JSONResponse(status_code=400, content={"message": "Playlist URL manquante"})
|
| 32 |
+
# Renvoie un exemple statique pour test front
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
return {
|
| 34 |
+
"message": "Test réussite - extraction simulée.",
|
| 35 |
+
"playlist": [
|
| 36 |
+
{"title": "Test Song", "artist": "Test Artist", "youtube_link": "https://youtube.com/watch?v=dQw4w9WgXcQ"}
|
| 37 |
+
]
|
| 38 |
}
|