tecuts commited on
Commit
363ed4f
·
verified ·
1 Parent(s): 0a1f269

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -12
app.py CHANGED
@@ -82,21 +82,22 @@ def download_track(track_id: str, quality: str = "MP3_320"):
82
  recursive_download=False
83
  )
84
 
85
- # Get the downloaded file
86
- downloaded_files = os.listdir("downloads")
87
- if len(downloaded_files) == 0:
88
- raise HTTPException(status_code=500, detail="File download failed")
89
-
90
- # Find the MP3 file
91
- mp3_files = [file for file in downloaded_files if file.endswith('.mp3')]
92
- if len(mp3_files) == 0:
93
- raise HTTPException(status_code=500, detail="MP3 file not found after download")
94
 
95
- found_file = mp3_files[0]
96
- found_filepath = os.path.join("downloads", found_file)
97
 
98
  # Return the download URL
99
- download_url = f"/downloads/{os.path.basename(found_file)}"
 
100
  logger.info(f"Download successful: {download_url}")
101
  return {"download_url": download_url}
102
  except Exception as e:
 
82
  recursive_download=False
83
  )
84
 
85
+ # Recursively search for the MP3 file in the downloads directory
86
+ mp3_filepath = None
87
+ for root, dirs, files in os.walk("downloads"):
88
+ for file in files:
89
+ if file.endswith('.mp3'):
90
+ mp3_filepath = os.path.join(root, file)
91
+ break
92
+ if mp3_filepath:
93
+ break
94
 
95
+ if not mp3_filepath:
96
+ raise HTTPException(status_code=500, detail="MP3 file not found after download")
97
 
98
  # Return the download URL
99
+ relative_path = os.path.relpath(mp3_filepath, "downloads")
100
+ download_url = f"/downloads/{relative_path}"
101
  logger.info(f"Download successful: {download_url}")
102
  return {"download_url": download_url}
103
  except Exception as e: