Update app.py
Browse files
app.py
CHANGED
@@ -23,31 +23,38 @@ app.mount("/downloads", StaticFiles(directory="downloads"), name="downloads")
|
|
23 |
DEEZER_API_URL = "https://api.deezer.com"
|
24 |
|
25 |
# Deezer ARL token (required for deezspot downloads)
|
26 |
-
ARL_TOKEN =
|
27 |
dl = DeeLogin(arl=ARL_TOKEN)
|
28 |
|
29 |
@app.get("/")
|
30 |
def read_root():
|
31 |
-
return {"message": "running
|
32 |
|
33 |
-
#
|
34 |
-
|
35 |
-
def get_track(track_id: str):
|
36 |
try:
|
37 |
response = requests.get(f"{DEEZER_API_URL}/track/{track_id}")
|
38 |
if response.status_code != 200:
|
39 |
raise HTTPException(status_code=404, detail="Track not found")
|
40 |
return response.json()
|
|
|
|
|
|
|
41 |
except Exception as e:
|
42 |
logger.error(f"Error fetching track metadata: {e}")
|
43 |
raise HTTPException(status_code=500, detail=str(e))
|
44 |
|
|
|
|
|
|
|
|
|
|
|
45 |
# Download a track and return a download URL
|
46 |
@app.post("/download/track/{track_id}")
|
47 |
def download_track(track_id: str, quality: str = "MP3_320"):
|
48 |
try:
|
49 |
-
# Fetch track
|
50 |
-
track_info =
|
51 |
track_link = track_info.get("link")
|
52 |
if not track_link:
|
53 |
raise HTTPException(status_code=404, detail="Track link not found")
|
@@ -55,11 +62,10 @@ def download_track(track_id: str, quality: str = "MP3_320"):
|
|
55 |
# Sanitize filename
|
56 |
track_title = track_info.get("title", "track")
|
57 |
artist_name = track_info.get("artist", {}).get("name", "unknown")
|
58 |
-
|
59 |
-
filepath = os.path.join("downloads", filename)
|
60 |
|
61 |
# Download the track using deezspot
|
62 |
-
logger.info(f"Downloading track: {
|
63 |
dl.download_trackdee(
|
64 |
link_track=track_link,
|
65 |
output_dir="downloads",
|
@@ -68,25 +74,23 @@ def download_track(track_id: str, quality: str = "MP3_320"):
|
|
68 |
recursive_download=False
|
69 |
)
|
70 |
|
71 |
-
#
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
else:
|
86 |
-
raise HTTPException(status_code=500, detail="File download failed")
|
87 |
|
88 |
# Return the download URL
|
89 |
-
download_url = f"/downloads/{os.path.basename(
|
90 |
logger.info(f"Download successful: {download_url}")
|
91 |
return {"download_url": download_url}
|
92 |
except Exception as e:
|
@@ -99,6 +103,9 @@ def search_tracks(query: str, limit: Optional[int] = 10):
|
|
99 |
try:
|
100 |
response = requests.get(f"{DEEZER_API_URL}/search", params={"q": query, "limit": limit})
|
101 |
return response.json()
|
|
|
|
|
|
|
102 |
except Exception as e:
|
103 |
logger.error(f"Error searching tracks: {e}")
|
104 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
23 |
DEEZER_API_URL = "https://api.deezer.com"
|
24 |
|
25 |
# Deezer ARL token (required for deezspot downloads)
|
26 |
+
ARL_TOKEN = os.getenv('ARL')
|
27 |
dl = DeeLogin(arl=ARL_TOKEN)
|
28 |
|
29 |
@app.get("/")
|
30 |
def read_root():
|
31 |
+
return {"message": "running"}
|
32 |
|
33 |
+
# Helper function to get track info
|
34 |
+
def get_track_info(track_id: str):
|
|
|
35 |
try:
|
36 |
response = requests.get(f"{DEEZER_API_URL}/track/{track_id}")
|
37 |
if response.status_code != 200:
|
38 |
raise HTTPException(status_code=404, detail="Track not found")
|
39 |
return response.json()
|
40 |
+
except requests.exceptions.RequestException as e:
|
41 |
+
logger.error(f"Network error fetching track metadata: {e}")
|
42 |
+
raise HTTPException(status_code=500, detail=str(e))
|
43 |
except Exception as e:
|
44 |
logger.error(f"Error fetching track metadata: {e}")
|
45 |
raise HTTPException(status_code=500, detail=str(e))
|
46 |
|
47 |
+
# Fetch track metadata from Deezer API
|
48 |
+
@app.get("/track/{track_id}")
|
49 |
+
def get_track(track_id: str):
|
50 |
+
return get_track_info(track_id)
|
51 |
+
|
52 |
# Download a track and return a download URL
|
53 |
@app.post("/download/track/{track_id}")
|
54 |
def download_track(track_id: str, quality: str = "MP3_320"):
|
55 |
try:
|
56 |
+
# Fetch track info
|
57 |
+
track_info = get_track_info(track_id)
|
58 |
track_link = track_info.get("link")
|
59 |
if not track_link:
|
60 |
raise HTTPException(status_code=404, detail="Track link not found")
|
|
|
62 |
# Sanitize filename
|
63 |
track_title = track_info.get("title", "track")
|
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(
|
70 |
link_track=track_link,
|
71 |
output_dir="downloads",
|
|
|
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 similar name
|
82 |
+
found_filepath = None
|
83 |
+
for file in downloaded_files:
|
84 |
+
if track_title.lower() in file.lower() and artist_name.lower() in file.lower():
|
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}")
|
95 |
return {"download_url": download_url}
|
96 |
except Exception as e:
|
|
|
103 |
try:
|
104 |
response = requests.get(f"{DEEZER_API_URL}/search", params={"q": query, "limit": limit})
|
105 |
return response.json()
|
106 |
+
except requests.exceptions.RequestException as e:
|
107 |
+
logger.error(f"Network error searching tracks: {e}")
|
108 |
+
raise HTTPException(status_code=500, detail=str(e))
|
109 |
except Exception as e:
|
110 |
logger.error(f"Error searching tracks: {e}")
|
111 |
raise HTTPException(status_code=500, detail=str(e))
|