Neo2902 commited on
Commit
630fa47
·
1 Parent(s): aa6d22a

Projet complet avec chunking, extract_playlist et youtube_search

Browse files
Files changed (2) hide show
  1. app.py +6 -3
  2. extract_playlist_playwright.py +16 -10
app.py CHANGED
@@ -7,8 +7,8 @@ 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
@@ -44,11 +44,15 @@ async def extract(request: Request):
44
  if not playlist_url:
45
  return JSONResponse(status_code=400, content={"message": "Playlist URL manquante"})
46
  try:
47
- songs = await extract_playlist_playwright.extract_playlist(playlist_url)
 
 
 
48
  batches = list(chunking.chunk_list(songs, 10))
49
 
50
  final_results = []
51
  for batch in batches:
 
52
  tasks = [youtube_search.search_youtube(song['title'], song['artist']) for song in batch]
53
  results = await asyncio.gather(*tasks)
54
  final_results.extend(results)
@@ -59,4 +63,3 @@ async def extract(request: Request):
59
  }
60
  except Exception as e:
61
  return JSONResponse(status_code=500, content={"message": f"Erreur extraction : {str(e)}"})
62
-
 
7
 
8
  sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
9
 
10
+ from extract_playlist_playwright import extract_playlist # Import asynchrone extraction
11
  import chunking
 
12
  import youtube_search
13
 
14
  from fastapi import FastAPI, Request
 
44
  if not playlist_url:
45
  return JSONResponse(status_code=400, content={"message": "Playlist URL manquante"})
46
  try:
47
+ # Extraction réelle des morceaux via Playwright
48
+ songs = await extract_playlist(playlist_url)
49
+
50
+ # Découpage par paquet de 10
51
  batches = list(chunking.chunk_list(songs, 10))
52
 
53
  final_results = []
54
  for batch in batches:
55
+ # Recherche YouTube pour chaque morceau
56
  tasks = [youtube_search.search_youtube(song['title'], song['artist']) for song in batch]
57
  results = await asyncio.gather(*tasks)
58
  final_results.extend(results)
 
63
  }
64
  except Exception as e:
65
  return JSONResponse(status_code=500, content={"message": f"Erreur extraction : {str(e)}"})
 
extract_playlist_playwright.py CHANGED
@@ -1,12 +1,18 @@
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
-
 
 
 
 
 
 
 
1
+ from playwright.async_api import async_playwright
2
 
3
  async def extract_playlist(url):
4
+ songs = []
5
+ async with async_playwright() as p:
6
+ browser = await p.chromium.launch(headless=True)
7
+ page = await browser.new_page()
8
+ await page.goto(url)
9
+ await page.wait_for_selector('div.songs-list-row__song-name-wrapper')
10
+ items = await page.query_selector_all('div.songs-list-row__song-name-wrapper')
11
+ for item in items:
12
+ title_el = await item.query_selector('div.songs-list-row__song-name')
13
+ artist_el = await item.query_selector('div.songs-list-row__by-line')
14
+ title = await title_el.inner_text() if title_el else "Unknown Title"
15
+ artist = await artist_el.inner_text() if artist_el else "Unknown Artist"
16
+ songs.append({'title': title.strip(), 'artist': artist.strip()})
17
+ await browser.close()
18
+ return songs