Update app.py
Browse files
app.py
CHANGED
@@ -27,6 +27,7 @@ from io import BytesIO
|
|
27 |
from pathlib import Path
|
28 |
from fastapi.staticfiles import StaticFiles
|
29 |
from collections import defaultdict
|
|
|
30 |
|
31 |
tmp_dir = tempfile.gettempdir()
|
32 |
BASE_URL = "https://chrunos-multi.hf.space"
|
@@ -311,6 +312,68 @@ async def download_audio(request: Request):
|
|
311 |
encoded_filename = urllib.parse.quote(downloaded_file.name)
|
312 |
download_url = f"{BASE_URL}/file/{encoded_filename}"
|
313 |
return {"url": download_url}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
314 |
# Mount the static files directory
|
315 |
app.mount("/file", StaticFiles(directory=global_download_dir), name="downloads")
|
316 |
|
|
|
27 |
from pathlib import Path
|
28 |
from fastapi.staticfiles import StaticFiles
|
29 |
from collections import defaultdict
|
30 |
+
import logging
|
31 |
|
32 |
tmp_dir = tempfile.gettempdir()
|
33 |
BASE_URL = "https://chrunos-multi.hf.space"
|
|
|
312 |
encoded_filename = urllib.parse.quote(downloaded_file.name)
|
313 |
download_url = f"{BASE_URL}/file/{encoded_filename}"
|
314 |
return {"url": download_url}
|
315 |
+
|
316 |
+
# Configure logging
|
317 |
+
logging.basicConfig(level=logging.INFO)
|
318 |
+
|
319 |
+
@app.post("/search")
|
320 |
+
async def search_and_download_song(request: Request):
|
321 |
+
data = await request.json()
|
322 |
+
song_name = data.get('songname')
|
323 |
+
artist_name = data.get('artist')
|
324 |
+
if artist_name:
|
325 |
+
search_query = f"ytsearch:{song_name} {artist_name}"
|
326 |
+
else:
|
327 |
+
search_query = f"ytsearch:{song_name}"
|
328 |
+
|
329 |
+
logging.info(f"Search query: {search_query}")
|
330 |
+
cookiefile = "firefox-cookies.txt"
|
331 |
+
env_to_cookies_from_env("firefox-cookies.txt")
|
332 |
+
|
333 |
+
timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
|
334 |
+
output_template = str(Path(global_download_dir) / f'%(title)s_{timestamp}.%(ext)s')
|
335 |
+
|
336 |
+
ydl_opts = {
|
337 |
+
'format': 'bestaudio/best',
|
338 |
+
'outtmpl': output_template,
|
339 |
+
'quiet': True,
|
340 |
+
'no_warnings': True,
|
341 |
+
'noprogress': True,
|
342 |
+
'postprocessors': [{
|
343 |
+
'key': 'FFmpegExtractAudio',
|
344 |
+
'preferredcodec': 'mp3',
|
345 |
+
'preferredquality': '192'
|
346 |
+
}],
|
347 |
+
'cookiefile': cookiefile
|
348 |
+
}
|
349 |
+
|
350 |
+
try:
|
351 |
+
logging.info("Starting yt-dlp search and download...")
|
352 |
+
await run_in_threadpool(lambda: yt_dlp.YoutubeDL(ydl_opts).download([search_query]))
|
353 |
+
logging.info("yt-dlp search and download completed")
|
354 |
+
except yt_dlp.utils.DownloadError as e:
|
355 |
+
error_message = str(e)
|
356 |
+
logging.error(f"yt-dlp error: {error_message}")
|
357 |
+
return JSONResponse(content={"error": error_message}, status_code=500)
|
358 |
+
except Exception as e:
|
359 |
+
error_message = str(e)
|
360 |
+
logging.error(f"General error: {error_message}")
|
361 |
+
return JSONResponse(content={"error": error_message}, status_code=500)
|
362 |
+
|
363 |
+
downloaded_files = list(Path(global_download_dir).glob(f"*_{timestamp}.mp3"))
|
364 |
+
if not downloaded_files:
|
365 |
+
logging.error("Download failed: No MP3 files found")
|
366 |
+
return JSONResponse(content={"error": "Download failed"}, status_code=500)
|
367 |
+
|
368 |
+
downloaded_file = downloaded_files[0]
|
369 |
+
encoded_filename = urllib.parse.quote(downloaded_file.name)
|
370 |
+
download_url = f"{BASE_URL}/file/{encoded_filename}"
|
371 |
+
logging.info(f"Download URL: {download_url}")
|
372 |
+
|
373 |
+
# Log just before returning the response
|
374 |
+
logging.info("Preparing to send response back to the client")
|
375 |
+
return JSONResponse(content={"url": download_url}, status_code=200)
|
376 |
+
|
377 |
# Mount the static files directory
|
378 |
app.mount("/file", StaticFiles(directory=global_download_dir), name="downloads")
|
379 |
|