Neo2902 commited on
Commit
4cf9a45
·
1 Parent(s): 0b1ce94

Projet complet avec chunking, extract_playlist et youtube_search

Browse files
app.py CHANGED
@@ -1,17 +1,39 @@
1
- import subprocess
2
  import sys
3
  import os
 
4
 
5
- # Installer Playwright et ses navigateurs au démarrage (avant import FastAPI)
6
  subprocess.run(["playwright", "install"], check=True)
7
 
8
  sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
9
- import hello_module
10
 
11
- from fastapi import FastAPI
 
 
 
 
 
 
12
 
13
  app = FastAPI()
14
 
15
- @app.get("/")
16
- async def root():
17
- return {"message": hello_module.say_hello()}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
 
18
  app = FastAPI()
19
 
20
+ @app.post("/extract")
21
+ async def extract(request: Request):
22
+ data = await request.json()
23
+ playlist_url = data.get("playlistUrl")
24
+ if not playlist_url:
25
+ return JSONResponse(status_code=400, content={"message": "Playlist URL manquante"})
26
+
27
+ songs = await extract_playlist_playwright.extract_playlist(playlist_url)
28
+ batches = list(chunking.chunk_list(songs, 10))
29
+
30
+ final_results = []
31
+ for batch in batches:
32
+ tasks = [youtube_search.search_youtube(s["title"], s["artist"]) for s in batch]
33
+ results = await asyncio.gather(*tasks)
34
+ final_results.extend(results)
35
+
36
+ return {
37
+ "message": f"{len(songs)} morceaux extraits et enrichis en {len(batches)} paquets.",
38
+ "playlist": final_results
39
+ }
chunking.py ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ def chunk_list(lst, n):
2
+ """Divise la liste lst en sous-listes de taille n."""
3
+ for i in range(0, len(lst), n):
4
+ yield lst[i:i + n]
5
+
extract_playlist_playwright.py ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import asyncio
2
+
3
+ async def extract_playlist(url):
4
+ # Ici, simule une extraction asynchrone d’une playlist
5
+ await asyncio.sleep(1)
6
+ return [
7
+ {"title": "Song 1", "artist": "Artist A"},
8
+ {"title": "Song 2", "artist": "Artist B"},
9
+ {"title": "Song 3", "artist": "Artist C"},
10
+ # ajoute d’autres morceaux si besoin
11
+ ]
12
+
requirements.txt CHANGED
@@ -1,3 +1,3 @@
1
- fastapi
2
- uvicorn
3
  playwright
 
1
+ fastapi[standard]
2
+ uvicorn[standard]
3
  playwright
youtube_search.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ import asyncio
2
+
3
+ async def search_youtube(title, artist):
4
+ # Simule une recherche asynchrone sur YouTube
5
+ await asyncio.sleep(0.5)
6
+ return {"title": title, "artist": artist, "youtube_link": f"https://youtube.com/search?q={title}+{artist}"}
7
+