tecuts commited on
Commit
cdd2ff8
·
verified ·
1 Parent(s): 2e4555c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -8
app.py CHANGED
@@ -1,29 +1,59 @@
1
  from fastapi import FastAPI, HTTPException
2
- from deezspot import Deezer
 
3
  from typing import Optional
4
 
5
  app = FastAPI(title="Deezer API")
6
- dz = Deezer()
 
 
 
 
 
 
7
 
8
  @app.get("/")
9
  def read_root():
10
  return {"message": "Deezer API Endpoints - Use /track/{track_id}"}
11
 
 
12
  @app.get("/track/{track_id}")
13
  def get_track(track_id: str):
14
  try:
15
- track = dz.get_track(track_id)
16
- if not track:
17
  raise HTTPException(status_code=404, detail="Track not found")
18
- return track
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  except Exception as e:
20
  raise HTTPException(status_code=500, detail=str(e))
21
 
22
- # Additional endpoint: Search tracks
23
  @app.get("/search")
24
  def search_tracks(query: str, limit: Optional[int] = 10):
25
  try:
26
- results = dz.search(query, limit=limit)
27
- return {"results": results}
28
  except Exception as e:
29
  raise HTTPException(status_code=500, detail=str(e))
 
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 = "your_arl_token"
13
+ dl = DeeLogin(arl=ARL_TOKEN)
14
 
15
  @app.get("/")
16
  def read_root():
17
  return {"message": "Deezer API Endpoints - Use /track/{track_id}"}
18
 
19
+ # Fetch track metadata from Deezer API
20
  @app.get("/track/{track_id}")
21
  def get_track(track_id: str):
22
  try:
23
+ response = requests.get(f"{DEEZER_API_URL}/track/{track_id}")
24
+ if response.status_code != 200:
25
  raise HTTPException(status_code=404, detail="Track not found")
26
+ return response.json()
27
+ except Exception as e:
28
+ raise HTTPException(status_code=500, detail=str(e))
29
+
30
+ # Download a track using deezspot
31
+ @app.post("/download/track/{track_id}")
32
+ def download_track(track_id: str, quality: str = "MP3_320"):
33
+ try:
34
+ # Fetch track link from Deezer API
35
+ track_info = requests.get(f"{DEEZER_API_URL}/track/{track_id}").json()
36
+ track_link = track_info.get("link")
37
+ if not track_link:
38
+ raise HTTPException(status_code=404, detail="Track link not found")
39
+
40
+ # Download the track using deezspot
41
+ dl.download_trackdee(
42
+ link_track=track_link,
43
+ output_dir="./downloads",
44
+ quality_download=quality,
45
+ recursive_quality=False,
46
+ recursive_download=False
47
+ )
48
+ return {"message": f"Track {track_id} downloaded successfully"}
49
  except Exception as e:
50
  raise HTTPException(status_code=500, detail=str(e))
51
 
52
+ # Search tracks using Deezer API
53
  @app.get("/search")
54
  def search_tracks(query: str, limit: Optional[int] = 10):
55
  try:
56
+ response = requests.get(f"{DEEZER_API_URL}/search", params={"q": query, "limit": limit})
57
+ return response.json()
58
  except Exception as e:
59
  raise HTTPException(status_code=500, detail=str(e))