Update app.py
Browse files
app.py
CHANGED
@@ -2,9 +2,14 @@ from fastapi import FastAPI, HTTPException
|
|
2 |
from deezspot.deezloader import DeeLogin
|
3 |
import requests
|
4 |
import os
|
|
|
5 |
from typing import Optional
|
6 |
from fastapi.staticfiles import StaticFiles
|
7 |
|
|
|
|
|
|
|
|
|
8 |
app = FastAPI(title="Deezer API")
|
9 |
|
10 |
# Mount a static files directory to serve downloaded files
|
@@ -32,6 +37,7 @@ def get_track(track_id: str):
|
|
32 |
raise HTTPException(status_code=404, detail="Track not found")
|
33 |
return response.json()
|
34 |
except Exception as e:
|
|
|
35 |
raise HTTPException(status_code=500, detail=str(e))
|
36 |
|
37 |
# Download a track and return a download URL
|
@@ -44,30 +50,33 @@ def download_track(track_id: str, quality: str = "MP3_320"):
|
|
44 |
if not track_link:
|
45 |
raise HTTPException(status_code=404, detail="Track link not found")
|
46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
# Download the track using deezspot
|
48 |
-
|
49 |
dl.download_trackdee(
|
50 |
link_track=track_link,
|
51 |
-
output_dir=
|
52 |
quality_download=quality,
|
53 |
recursive_quality=False,
|
54 |
recursive_download=False
|
55 |
)
|
56 |
|
57 |
-
# Find the downloaded file
|
58 |
-
track_title = track_info.get("title", "track")
|
59 |
-
artist_name = track_info.get("artist", {}).get("name", "unknown")
|
60 |
-
filename = f"{artist_name} - {track_title}.mp3".replace("/", "_") # Sanitize filename
|
61 |
-
filepath = os.path.join(output_dir, filename)
|
62 |
-
|
63 |
# Check if the file exists
|
64 |
if not os.path.exists(filepath):
|
|
|
65 |
raise HTTPException(status_code=500, detail="File download failed")
|
66 |
|
67 |
# Return the download URL
|
68 |
download_url = f"/downloads/{filename}"
|
|
|
69 |
return {"download_url": download_url}
|
70 |
except Exception as e:
|
|
|
71 |
raise HTTPException(status_code=500, detail=str(e))
|
72 |
|
73 |
# Search tracks using Deezer API
|
@@ -77,4 +86,5 @@ def search_tracks(query: str, limit: Optional[int] = 10):
|
|
77 |
response = requests.get(f"{DEEZER_API_URL}/search", params={"q": query, "limit": limit})
|
78 |
return response.json()
|
79 |
except Exception as e:
|
|
|
80 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
2 |
from deezspot.deezloader import DeeLogin
|
3 |
import requests
|
4 |
import os
|
5 |
+
import logging
|
6 |
from typing import Optional
|
7 |
from fastapi.staticfiles import StaticFiles
|
8 |
|
9 |
+
# Set up logging
|
10 |
+
logging.basicConfig(level=logging.INFO)
|
11 |
+
logger = logging.getLogger(__name__)
|
12 |
+
|
13 |
app = FastAPI(title="Deezer API")
|
14 |
|
15 |
# Mount a static files directory to serve downloaded files
|
|
|
37 |
raise HTTPException(status_code=404, detail="Track not found")
|
38 |
return response.json()
|
39 |
except Exception as e:
|
40 |
+
logger.error(f"Error fetching track metadata: {e}")
|
41 |
raise HTTPException(status_code=500, detail=str(e))
|
42 |
|
43 |
# Download a track and return a download URL
|
|
|
50 |
if not track_link:
|
51 |
raise HTTPException(status_code=404, detail="Track link not found")
|
52 |
|
53 |
+
# Sanitize filename
|
54 |
+
track_title = track_info.get("title", "track")
|
55 |
+
artist_name = track_info.get("artist", {}).get("name", "unknown")
|
56 |
+
filename = f"{artist_name} - {track_title}.mp3".replace("/", "_") # Sanitize filename
|
57 |
+
filepath = os.path.join("downloads", filename)
|
58 |
+
|
59 |
# Download the track using deezspot
|
60 |
+
logger.info(f"Downloading track: {filename}")
|
61 |
dl.download_trackdee(
|
62 |
link_track=track_link,
|
63 |
+
output_dir="downloads",
|
64 |
quality_download=quality,
|
65 |
recursive_quality=False,
|
66 |
recursive_download=False
|
67 |
)
|
68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
# Check if the file exists
|
70 |
if not os.path.exists(filepath):
|
71 |
+
logger.error(f"File not found: {filepath}")
|
72 |
raise HTTPException(status_code=500, detail="File download failed")
|
73 |
|
74 |
# Return the download URL
|
75 |
download_url = f"/downloads/{filename}"
|
76 |
+
logger.info(f"Download successful: {download_url}")
|
77 |
return {"download_url": download_url}
|
78 |
except Exception as e:
|
79 |
+
logger.error(f"Error downloading track: {e}")
|
80 |
raise HTTPException(status_code=500, detail=str(e))
|
81 |
|
82 |
# Search tracks using Deezer API
|
|
|
86 |
response = requests.get(f"{DEEZER_API_URL}/search", params={"q": query, "limit": limit})
|
87 |
return response.json()
|
88 |
except Exception as e:
|
89 |
+
logger.error(f"Error searching tracks: {e}")
|
90 |
raise HTTPException(status_code=500, detail=str(e))
|