tecuts commited on
Commit
91ff406
·
verified ·
1 Parent(s): 6a8c623

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -14
app.py CHANGED
@@ -68,6 +68,13 @@ class DownloadRequest(BaseModel):
68
  @app.post("/spot-track/{track_id}")
69
  def download_spotify_track(track_id: str):
70
  try:
 
 
 
 
 
 
 
71
  # Download the track using spotloader
72
  logger.info(f"Downloading Spotify track: {track_id}")
73
  spo.download_track(
@@ -79,28 +86,37 @@ def download_spotify_track(track_id: str):
79
  not_interface=False,
80
  method_save=1
81
  )
 
 
 
82
 
83
  # Recursively search for audio files in the downloads directory
84
- audio_extensions = [".mp3", ".ogg", ".flac", ".wav", ".m4a"]
85
- downloaded_files = []
86
- for ext in audio_extensions:
87
- downloaded_files.extend(list(Path("downloads").rglob(f"*{ext}")))
88
- logger.info(f"Files in downloads directory: {downloaded_files}")
89
-
90
- if not downloaded_files:
91
- raise HTTPException(status_code=500, detail="File download failed")
 
 
92
 
93
- # Use the first matching file
94
- filepath = downloaded_files[0]
95
- filename = filepath.name
 
 
96
 
97
  # Return the download URL
98
- download_url = f"{BASE_URL}/downloads/{quote(str(filepath.relative_to('downloads')))}"
 
 
99
  logger.info(f"Download successful: {download_url}")
100
  return {"download_url": download_url}
101
  except Exception as e:
102
- logger.error(f"Error downloading Spotify track: {e}")
103
- raise HTTPException(status_code=500, detail="Failed to download Spotify track. Please try again later.")
104
 
105
 
106
  @app.get("/")
 
68
  @app.post("/spot-track/{track_id}")
69
  def download_spotify_track(track_id: str):
70
  try:
71
+ # Clear the downloads directory
72
+ for root, dirs, files in os.walk("downloads"):
73
+ for file in files:
74
+ os.remove(os.path.join(root, file))
75
+ for dir in dirs:
76
+ shutil.rmtree(os.path.join(root, dir))
77
+
78
  # Download the track using spotloader
79
  logger.info(f"Downloading Spotify track: {track_id}")
80
  spo.download_track(
 
86
  not_interface=False,
87
  method_save=1
88
  )
89
+ except Exception as e:
90
+ logger.error(f"Error downloading file: {e}")
91
+ raise HTTPException(status_code=500, detail="File download failed")
92
 
93
  # Recursively search for audio files in the downloads directory
94
+ file_extension = "ogg"
95
+ # Recursively search for the file in the downloads directory
96
+ filepath = None
97
+ for root, dirs, files in os.walk("downloads"):
98
+ for file in files:
99
+ if file.endswith(f'.{file_extension}'):
100
+ filepath = os.path.join(root, file)
101
+ break
102
+ if filepath:
103
+ break
104
 
105
+ if not filepath:
106
+ raise HTTPException(status_code=500, detail=f"{file_extension} file not found after download")
107
+ if filepath:
108
+ file_size = os.path.getsize(filepath)
109
+ logger.info(f"Downloaded file size: {file_size} bytes")
110
 
111
  # Return the download URL
112
+ relative_path = quote(str(os.path.relpath(filepath, "downloads")))
113
+ # Remove spaces from the relative path
114
+ download_url = f"{BASE_URL}/downloads/{relative_path}"
115
  logger.info(f"Download successful: {download_url}")
116
  return {"download_url": download_url}
117
  except Exception as e:
118
+ logger.error(f"Error downloading track: {e}")
119
+ raise HTTPException(status_code=500, detail=str(e))
120
 
121
 
122
  @app.get("/")