tecuts commited on
Commit
a108d08
·
verified ·
1 Parent(s): 90d56a9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -4
app.py CHANGED
@@ -1,16 +1,22 @@
1
  from fastapi import FastAPI, HTTPException
2
  from deezspot.deezloader import DeeLogin
3
  import requests
 
4
  from typing import Optional
 
5
 
6
  app = FastAPI(title="Deezer API")
7
 
 
 
 
 
8
  # Deezer API base URL
9
  DEEZER_API_URL = "https://api.deezer.com"
10
 
11
  # Deezer ARL token (required for deezspot downloads)
12
  ARL_TOKEN = "9850d663715d56830e6cdb4d28d19491d8c9d9a8ee31c160a0f5e06116b6d8035fb01c5323ec9690e49a32c0580c0a84e484366df2d6a8ac5786d30a95dc660771fbb372735cb2b39d4081bf30284f08319c0f73f6ad34d3d6bcb4449226877c"
13
- #f10b97c01d087e29c71e1b1950f220244f57e199e923e5fc975b45533a497daac4831349242ea9dbc9ee56b6c27970c28314dfe7777be10cc516a1e91125f2b322521b83644e1a7c533709f29453ab44414466cc8a1bdebdd37240c8c84a2a"
14
  dl = DeeLogin(arl=ARL_TOKEN)
15
 
16
  @app.get("/")
@@ -28,7 +34,7 @@ def get_track(track_id: str):
28
  except Exception as e:
29
  raise HTTPException(status_code=500, detail=str(e))
30
 
31
- # Download a track using deezspot
32
  @app.post("/download/track/{track_id}")
33
  def download_track(track_id: str, quality: str = "MP3_320"):
34
  try:
@@ -39,14 +45,28 @@ def download_track(track_id: str, quality: str = "MP3_320"):
39
  raise HTTPException(status_code=404, detail="Track link not found")
40
 
41
  # Download the track using deezspot
 
42
  dl.download_trackdee(
43
  link_track=track_link,
44
- output_dir="./downloads",
45
  quality_download=quality,
46
  recursive_quality=False,
47
  recursive_download=False
48
  )
49
- return {"message": f"Track {track_id} downloaded successfully"}
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  except Exception as e:
51
  raise HTTPException(status_code=500, detail=str(e))
52
 
 
1
  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
11
+ os.makedirs("downloads", exist_ok=True)
12
+ app.mount("/downloads", StaticFiles(directory="downloads"), name="downloads")
13
+
14
  # Deezer API base URL
15
  DEEZER_API_URL = "https://api.deezer.com"
16
 
17
  # Deezer ARL token (required for deezspot downloads)
18
  ARL_TOKEN = "9850d663715d56830e6cdb4d28d19491d8c9d9a8ee31c160a0f5e06116b6d8035fb01c5323ec9690e49a32c0580c0a84e484366df2d6a8ac5786d30a95dc660771fbb372735cb2b39d4081bf30284f08319c0f73f6ad34d3d6bcb4449226877c"
19
+ #f10b97c01d087e29c71e1b1950f22
20
  dl = DeeLogin(arl=ARL_TOKEN)
21
 
22
  @app.get("/")
 
34
  except Exception as e:
35
  raise HTTPException(status_code=500, detail=str(e))
36
 
37
+ # Download a track and return a download URL
38
  @app.post("/download/track/{track_id}")
39
  def download_track(track_id: str, quality: str = "MP3_320"):
40
  try:
 
45
  raise HTTPException(status_code=404, detail="Track link not found")
46
 
47
  # Download the track using deezspot
48
+ output_dir = "./downloads"
49
  dl.download_trackdee(
50
  link_track=track_link,
51
+ output_dir=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