Neo2902 commited on
Commit
2987eba
·
1 Parent(s): fd8f3e8

Ajout de tous les fichiers pour mise à jour complète

Browse files
Files changed (6) hide show
  1. Dockerfile +19 -0
  2. app.py +40 -0
  3. chunking.py +5 -0
  4. extract_playlist_playwright.py +23 -0
  5. requirements.txt +6 -0
  6. youtube_search.py +18 -0
Dockerfile ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Utilise une image python officielle
2
+ FROM python:3.10-slim
3
+
4
+ # Crée un dossier de travail
5
+ WORKDIR /app
6
+
7
+ # Copie requirements.txt et installe dépendances
8
+ COPY requirements.txt .
9
+ RUN pip install --no-cache-dir -r requirements.txt
10
+
11
+ # Copie le code source
12
+ COPY app.py .
13
+
14
+ # Expose le port (doit correspondre au port de uvicorn)
15
+ EXPOSE 7860
16
+
17
+ # Commande pour lancer l'application uvicorn
18
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
19
+
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ print("Current working dir:", os.getcwd())
3
+ print("Files here:", os.listdir())
4
+
5
+ import os
6
+ import sys
7
+ sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
8
+
9
+
10
+ from fastapi import FastAPI, Request
11
+ from fastapi.responses import JSONResponse
12
+ import asyncio
13
+
14
+ # Import des modules placés à la racine du dépôt
15
+ import extract_playlist_playwright
16
+ import chunking
17
+ import youtube_search
18
+
19
+ app = FastAPI()
20
+
21
+ @app.post("/extract")
22
+ async def extract(request: Request):
23
+ data = await request.json()
24
+ playlist_url = data.get("playlistUrl")
25
+ if not playlist_url:
26
+ return JSONResponse(status_code=400, content={"message": "Playlist URL manquante"})
27
+
28
+ songs = await extract_playlist_playwright.extract_playlist(playlist_url)
29
+ batches = list(chunking.chunk_list(songs, 10))
30
+
31
+ final_results = []
32
+ for batch in batches:
33
+ tasks = [youtube_search.search_youtube(s["title"], s["artist"]) for s in batch]
34
+ results = await asyncio.gather(*tasks)
35
+ final_results.extend(results)
36
+
37
+ return {
38
+ "message": f"{len(songs)} morceaux extraits et enrichis en {len(batches)} paquets.",
39
+ "playlist": final_results
40
+ }
chunking.py ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ def chunk_list(lst, n):
2
+ """Découpe une liste en morceaux 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,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from playwright.async_api import async_playwright
2
+
3
+ async def extract_playlist(url: str):
4
+ async with async_playwright() as p:
5
+ browser = await p.chromium.launch(headless=True)
6
+ page = await browser.new_page()
7
+ await page.goto(url)
8
+
9
+ # Attente de l’élément clé pour la liste des titres
10
+ await page.wait_for_selector('div.songs-list-row__song-name')
11
+
12
+ titles = await page.query_selector_all('div.songs-list-row__song-name')
13
+ artists = await page.query_selector_all('div.songs-list-row__by-line')
14
+
15
+ songs = []
16
+ for t, a in zip(titles, artists):
17
+ title = await t.inner_text()
18
+ artist = await a.inner_text()
19
+ songs.append({"title": title.strip(), "artist": artist.strip()})
20
+
21
+ await browser.close()
22
+ return songs
23
+
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ fastapi[standard]
2
+ uvicorn[standard]
3
+ playwright
4
+ requests==2.31.0
5
+ youtube-search-python
6
+
youtube_search.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from youtubesearchpython import VideosSearch
2
+
3
+ async def search_youtube(title: str, artist: str):
4
+ query = f"{title} {artist}"
5
+ videosSearch = VideosSearch(query, limit=1)
6
+ result = await videosSearch.next()
7
+
8
+ if result and "result" in result and len(result["result"]) > 0:
9
+ video = result["result"][0]
10
+ return {
11
+ "title": title,
12
+ "artist": artist,
13
+ "youtube_title": video.get("title", ""),
14
+ "youtube_url": video.get("link", None)
15
+ }
16
+ else:
17
+ return {"title": title, "artist": artist, "youtube_url": None}
18
+