File size: 2,176 Bytes
0549d36 94de8a3 173a387 0549d36 94de8a3 ba9be6f 94de8a3 ba9be6f 94de8a3 1bac1b8 ba9be6f 173a387 9674a42 4cf9a45 ba9be6f 4cf9a45 ba9be6f 4cf9a45 ba9be6f 0549d36 ba9be6f 173a387 ba9be6f 630fa47 aa6d22a ba9be6f aa6d22a ba9be6f aa6d22a ba9be6f aa6d22a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | import sys
import os
import subprocess
import logging
import asyncio
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
import chunking
import extract_playlist_playwright
import youtube_search
# Installer Playwright et les navigateurs au démarrage
try:
subprocess.run(["playwright", "install"], check=True)
print("Playwright browsers installed successfully.")
except Exception as e:
print(f"Erreur lors de l'installation des navigateurs Playwright : {e}")
# Configuration de base du logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(message)s",
handlers=[logging.StreamHandler(sys.stdout)],
)
logger = logging.getLogger(__name__)
app = FastAPI()
origins = [
"http://localhost:8000",
"https://ton-front-netlify-url",
"file://",
"*"
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.post("/extract")
async def extract(request: Request):
data = await request.json()
playlist_url = data.get("playlistUrl")
logger.info(f"Requête reçue pour URL playlist : {playlist_url}")
if not playlist_url:
logger.warning("Playlist URL manquante dans la requête")
return JSONResponse(status_code=400, content={"message": "Playlist URL manquante"})
try:
logger.info("Début extraction playlist")
songs = await extract_playlist_playwright.extract_playlist(playlist_url)
logger.info(f"{len(songs)} morceaux extraits")
batches = list(chunking.chunk_list(songs, 10))
logger.info(f"Découpage en {len(batches)} paquets")
final_results = []
for i, batch in enumerate(batches, 1):
logger.info(f"Traitement du paquet {i} / {len(batches)}")
tasks = [youtube_search.search_youtube(song['title'], song['artist']) for song in batch]
results = await asyncio.gather(*tasks)
final_results.extend(results)
logger.info("Fin enrichissement morceaux avec liens YouTube")
return {
|