Update app.py
Browse files
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 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
|
|
|
|
92 |
|
93 |
-
|
94 |
-
|
95 |
-
|
|
|
|
|
96 |
|
97 |
# Return the download URL
|
98 |
-
|
|
|
|
|
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
|
103 |
-
raise HTTPException(status_code=500, detail=
|
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("/")
|