Update app.py
Browse files
app.py
CHANGED
@@ -12,6 +12,8 @@ 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
|
@@ -31,7 +33,7 @@ app.mount("/downloads", StaticFiles(directory="downloads"), name="downloads")
|
|
31 |
DEEZER_API_URL = "https://api.deezer.com"
|
32 |
|
33 |
# Base URL for the server
|
34 |
-
BASE_URL = "https://
|
35 |
|
36 |
# Deezer ARL token (required for deezspot downloads)
|
37 |
ARL_TOKEN = os.getenv('ARL')
|
@@ -63,55 +65,52 @@ class DownloadRequest(BaseModel):
|
|
63 |
quality: str
|
64 |
|
65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
|
68 |
-
|
69 |
-
# Download a Spotify track and return a download URL
|
70 |
@app.post("/spot-track/{track_id}")
|
71 |
-
def download_spotify_track(
|
|
|
|
|
|
|
72 |
try:
|
73 |
-
# Clear the downloads directory
|
74 |
downloads_dir = Path("downloads")
|
75 |
-
for
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
logger.info(f"Downloading Spotify track: {track_id}")
|
83 |
spo.download_track(
|
84 |
link_track=f"https://open.spotify.com/track/{track_id}",
|
85 |
-
output_dir=str(
|
86 |
quality_download="VERY_HIGH",
|
87 |
recursive_quality=False,
|
88 |
recursive_download=False,
|
89 |
not_interface=False,
|
90 |
method_save=1
|
91 |
)
|
92 |
-
|
93 |
-
file_extension = "ogg"
|
94 |
-
# Recursively search for the file in the downloads directory
|
95 |
-
filepath = None
|
96 |
-
for root, dirs, files in os.walk(downloads_dir):
|
97 |
-
for file in files:
|
98 |
-
if file.endswith(f'.{file_extension}'):
|
99 |
-
filepath = Path(root) / file
|
100 |
-
break
|
101 |
-
if filepath:
|
102 |
-
break
|
103 |
|
|
|
|
|
104 |
if not filepath:
|
105 |
-
raise HTTPException(status_code=500, detail=
|
106 |
-
|
107 |
-
file_size = filepath.stat().st_size
|
108 |
-
logger.info(f"Downloaded file size: {file_size} bytes")
|
109 |
|
110 |
-
#
|
|
|
|
|
|
|
111 |
relative_path = quote(str(filepath.relative_to(downloads_dir)))
|
112 |
-
download_url
|
113 |
-
|
114 |
-
return {"download_url": download_url}
|
115 |
except Exception as e:
|
116 |
logger.error(f"Error downloading track: {e}")
|
117 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
12 |
from pydantic import BaseModel
|
13 |
from urllib.parse import quote
|
14 |
from pathlib import Path
|
15 |
+
import uuid
|
16 |
+
from fastapi import BackgroundTasks
|
17 |
|
18 |
|
19 |
# Set up logging
|
|
|
33 |
DEEZER_API_URL = "https://api.deezer.com"
|
34 |
|
35 |
# Base URL for the server
|
36 |
+
BASE_URL = "https://chrunos-depot.hf.space"
|
37 |
|
38 |
# Deezer ARL token (required for deezspot downloads)
|
39 |
ARL_TOKEN = os.getenv('ARL')
|
|
|
65 |
quality: str
|
66 |
|
67 |
|
68 |
+
def cleanup_dir(dir_path: Path):
|
69 |
+
"""Background task to clean up directory"""
|
70 |
+
try:
|
71 |
+
shutil.rmtree(dir_path)
|
72 |
+
logger.info(f"Cleaned up directory: {dir_path}")
|
73 |
+
except Exception as e:
|
74 |
+
logger.error(f"Error cleaning up {dir_path}: {e}")
|
75 |
|
76 |
|
77 |
+
# Download a Spotify track and return a download URL
|
|
|
78 |
@app.post("/spot-track/{track_id}")
|
79 |
+
async def download_spotify_track(
|
80 |
+
track_id: str,
|
81 |
+
background_tasks: BackgroundTasks
|
82 |
+
):
|
83 |
try:
|
|
|
84 |
downloads_dir = Path("downloads")
|
85 |
+
# Create unique directory for this download
|
86 |
+
download_id = uuid.uuid4().hex
|
87 |
+
download_dir = downloads_dir / download_id
|
88 |
+
download_dir.mkdir(parents=True, exist_ok=True)
|
89 |
+
|
90 |
+
# Download to unique directory
|
91 |
+
logger.info(f"Downloading to {download_dir}")
|
|
|
92 |
spo.download_track(
|
93 |
link_track=f"https://open.spotify.com/track/{track_id}",
|
94 |
+
output_dir=str(download_dir),
|
95 |
quality_download="VERY_HIGH",
|
96 |
recursive_quality=False,
|
97 |
recursive_download=False,
|
98 |
not_interface=False,
|
99 |
method_save=1
|
100 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
|
102 |
+
# Find downloaded file
|
103 |
+
filepath = next(download_dir.glob(f"**/*.ogg"), None)
|
104 |
if not filepath:
|
105 |
+
raise HTTPException(status_code=500, detail="File not found after download")
|
|
|
|
|
|
|
106 |
|
107 |
+
# Schedule cleanup after response is sent
|
108 |
+
#background_tasks.add_task(cleanup_dir, download_dir)
|
109 |
+
|
110 |
+
# Return path with unique directory
|
111 |
relative_path = quote(str(filepath.relative_to(downloads_dir)))
|
112 |
+
return {"download_url": f"{BASE_URL}/downloads/{relative_path}"}
|
113 |
+
|
|
|
114 |
except Exception as e:
|
115 |
logger.error(f"Error downloading track: {e}")
|
116 |
raise HTTPException(status_code=500, detail=str(e))
|