|
from fastapi import FastAPI, HTTPException |
|
from deezspot.deezloader import DeeLogin |
|
import requests |
|
from typing import Optional |
|
|
|
app = FastAPI(title="Deezer API") |
|
|
|
|
|
DEEZER_API_URL = "https://api.deezer.com" |
|
|
|
|
|
ARL_TOKEN = "your_arl_token" |
|
dl = DeeLogin(arl=ARL_TOKEN) |
|
|
|
@app.get("/") |
|
def read_root(): |
|
return {"message": "Deezer API Endpoints - Use /track/{track_id}"} |
|
|
|
|
|
@app.get("/track/{track_id}") |
|
def get_track(track_id: str): |
|
try: |
|
response = requests.get(f"{DEEZER_API_URL}/track/{track_id}") |
|
if response.status_code != 200: |
|
raise HTTPException(status_code=404, detail="Track not found") |
|
return response.json() |
|
except Exception as e: |
|
raise HTTPException(status_code=500, detail=str(e)) |
|
|
|
|
|
@app.post("/download/track/{track_id}") |
|
def download_track(track_id: str, quality: str = "MP3_320"): |
|
try: |
|
|
|
track_info = requests.get(f"{DEEZER_API_URL}/track/{track_id}").json() |
|
track_link = track_info.get("link") |
|
if not track_link: |
|
raise HTTPException(status_code=404, detail="Track link not found") |
|
|
|
|
|
dl.download_trackdee( |
|
link_track=track_link, |
|
output_dir="./downloads", |
|
quality_download=quality, |
|
recursive_quality=False, |
|
recursive_download=False |
|
) |
|
return {"message": f"Track {track_id} downloaded successfully"} |
|
except Exception as e: |
|
raise HTTPException(status_code=500, detail=str(e)) |
|
|
|
|
|
@app.get("/search") |
|
def search_tracks(query: str, limit: Optional[int] = 10): |
|
try: |
|
response = requests.get(f"{DEEZER_API_URL}/search", params={"q": query, "limit": limit}) |
|
return response.json() |
|
except Exception as e: |
|
raise HTTPException(status_code=500, detail=str(e)) |