tecuts commited on
Commit
6e4b6a1
·
verified ·
1 Parent(s): fce4c15

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -12
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  from fastapi import FastAPI, HTTPException
2
  from deezspot.deezloader import DeeLogin
3
  import requests
@@ -64,6 +65,13 @@ def download_track(track_id: str, quality: str = "MP3_320"):
64
  artist_name = track_info.get("artist", {}).get("name", "unknown")
65
  expected_filename = f"{artist_name} - {track_title}.mp3".replace("/", "_") # Sanitize filename
66
 
 
 
 
 
 
 
 
67
  # Download the track using deezspot
68
  logger.info(f"Downloading track: {expected_filename}")
69
  dl.download_trackdee(
@@ -74,21 +82,13 @@ def download_track(track_id: str, quality: str = "MP3_320"):
74
  recursive_download=False
75
  )
76
 
77
- # Search for the downloaded file in the downloads directory
78
  downloaded_files = os.listdir("downloads")
79
- logger.warning(f"Files in downloads directory: {downloaded_files}")
80
-
81
- # Try to find the file with a more flexible approach
82
- found_filepath = None
83
- for file in downloaded_files:
84
- if (track_title.lower() in file.lower() or artist_name.lower() in file.lower()) and file.lower().endswith('.mp3'):
85
- found_filepath = os.path.join("downloads", file)
86
- logger.info(f"Found matching file: {found_filepath}")
87
- break
88
-
89
- if not found_filepath:
90
  raise HTTPException(status_code=500, detail="File download failed")
91
 
 
 
92
  # Return the download URL
93
  download_url = f"/downloads/{os.path.basename(found_filepath)}"
94
  logger.info(f"Download successful: {download_url}")
 
1
+ import shutil
2
  from fastapi import FastAPI, HTTPException
3
  from deezspot.deezloader import DeeLogin
4
  import requests
 
65
  artist_name = track_info.get("artist", {}).get("name", "unknown")
66
  expected_filename = f"{artist_name} - {track_title}.mp3".replace("/", "_") # Sanitize filename
67
 
68
+ # Clear the downloads directory
69
+ for root, dirs, files in os.walk("downloads"):
70
+ for file in files:
71
+ os.remove(os.path.join(root, file))
72
+ for dir in dirs:
73
+ shutil.rmtree(os.path.join(root, dir))
74
+
75
  # Download the track using deezspot
76
  logger.info(f"Downloading track: {expected_filename}")
77
  dl.download_trackdee(
 
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
+ found_filepath = os.path.join("downloads", downloaded_files[0])
91
+
92
  # Return the download URL
93
  download_url = f"/downloads/{os.path.basename(found_filepath)}"
94
  logger.info(f"Download successful: {download_url}")