File size: 3,049 Bytes
ff7a5f2
cdd2ff8
 
a108d08
ff7a5f2
a108d08
ff7a5f2
 
cdd2ff8
a108d08
 
 
 
cdd2ff8
 
 
 
90d56a9
a108d08
cdd2ff8
ff7a5f2
 
 
 
 
cdd2ff8
ff7a5f2
 
 
cdd2ff8
 
ff7a5f2
cdd2ff8
 
 
 
a108d08
cdd2ff8
 
 
 
 
 
 
 
 
 
a108d08
cdd2ff8
 
a108d08
cdd2ff8
 
 
 
a108d08
 
 
 
 
 
 
 
 
 
 
 
 
 
ff7a5f2
 
 
cdd2ff8
ff7a5f2
 
 
cdd2ff8
 
ff7a5f2
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from fastapi import FastAPI, HTTPException
from deezspot.deezloader import DeeLogin
import requests
import os
from typing import Optional
from fastapi.staticfiles import StaticFiles

app = FastAPI(title="Deezer API")

# Mount a static files directory to serve downloaded files
os.makedirs("downloads", exist_ok=True)
app.mount("/downloads", StaticFiles(directory="downloads"), name="downloads")

# Deezer API base URL
DEEZER_API_URL = "https://api.deezer.com"

# Deezer ARL token (required for deezspot downloads)
ARL_TOKEN = "9850d663715d56830e6cdb4d28d19491d8c9d9a8ee31c160a0f5e06116b6d8035fb01c5323ec9690e49a32c0580c0a84e484366df2d6a8ac5786d30a95dc660771fbb372735cb2b39d4081bf30284f08319c0f73f6ad34d3d6bcb4449226877c"
#f10b97c01d087e29c71e1b1950f22
dl = DeeLogin(arl=ARL_TOKEN)

@app.get("/")
def read_root():
    return {"message": "Deezer API Endpoints - Use /track/{track_id}"}

# Fetch track metadata from Deezer API
@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))

# Download a track and return a download URL
@app.post("/download/track/{track_id}")
def download_track(track_id: str, quality: str = "MP3_320"):
    try:
        # Fetch track link from Deezer API
        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")

        # Download the track using deezspot
        output_dir = "./downloads"
        dl.download_trackdee(
            link_track=track_link,
            output_dir=output_dir,
            quality_download=quality,
            recursive_quality=False,
            recursive_download=False
        )

        # Find the downloaded file
        track_title = track_info.get("title", "track")
        artist_name = track_info.get("artist", {}).get("name", "unknown")
        filename = f"{artist_name} - {track_title}.mp3".replace("/", "_")  # Sanitize filename
        filepath = os.path.join(output_dir, filename)

        # Check if the file exists
        if not os.path.exists(filepath):
            raise HTTPException(status_code=500, detail="File download failed")

        # Return the download URL
        download_url = f"/downloads/{filename}"
        return {"download_url": download_url}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

# Search tracks using Deezer API
@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))