tecuts commited on
Commit
da751e9
·
verified ·
1 Parent(s): 3e5eeb6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -11
app.py CHANGED
@@ -11,6 +11,8 @@ from fastapi.staticfiles import StaticFiles
11
  from dotenv import load_dotenv
12
  from pydantic import BaseModel
13
  from urllib.parse import quote
 
 
14
 
15
  # Set up logging
16
  logging.basicConfig(level=logging.INFO)
@@ -77,24 +79,19 @@ def download_spotify_track(track_id: str):
77
  method_save=1
78
  )
79
 
80
- # Search for the downloaded file
81
- downloaded_files = os.listdir("downloads")
82
  logger.info(f"Files in downloads directory: {downloaded_files}")
83
 
84
- # Fetch track metadata from Spotify (if needed)
85
- # For now, assume the file is named like "Artist - Track Title.mp3"
86
- # Look for files that match the pattern
87
- matching_files = [file for file in downloaded_files if file.endswith(".mp3")]
88
-
89
- if not matching_files:
90
  raise HTTPException(status_code=500, detail="File download failed")
91
 
92
  # Use the first matching file
93
- filename = matching_files[0]
94
- filepath = os.path.join("downloads", filename)
95
 
96
  # Return the download URL
97
- download_url = f"/downloads/{quote(filename)}"
98
  logger.info(f"Download successful: {download_url}")
99
  return {"download_url": download_url}
100
  except Exception as e:
 
11
  from dotenv import load_dotenv
12
  from pydantic import BaseModel
13
  from urllib.parse import quote
14
+ from pathlib import Path
15
+
16
 
17
  # Set up logging
18
  logging.basicConfig(level=logging.INFO)
 
79
  method_save=1
80
  )
81
 
82
+ # Recursively search for .mp3 files in the downloads directory
83
+ downloaded_files = list(Path("downloads").rglob("*.mp3"))
84
  logger.info(f"Files in downloads directory: {downloaded_files}")
85
 
86
+ if not downloaded_files:
 
 
 
 
 
87
  raise HTTPException(status_code=500, detail="File download failed")
88
 
89
  # Use the first matching file
90
+ filepath = downloaded_files[0]
91
+ filename = filepath.name
92
 
93
  # Return the download URL
94
+ download_url = f"/downloads/{quote(str(filepath.relative_to('downloads')))}"
95
  logger.info(f"Download successful: {download_url}")
96
  return {"download_url": download_url}
97
  except Exception as e: