|
import shutil |
|
from fastapi import FastAPI, HTTPException |
|
from deezspot.deezloader import DeeLogin |
|
from deezspot.spotloader import SpoLogin |
|
import requests |
|
import os |
|
import logging |
|
import json |
|
from typing import Optional |
|
from fastapi.staticfiles import StaticFiles |
|
from dotenv import load_dotenv |
|
from pydantic import BaseModel |
|
from urllib.parse import quote |
|
from pathlib import Path |
|
|
|
|
|
|
|
logging.basicConfig(level=logging.INFO) |
|
logger = logging.getLogger(__name__) |
|
|
|
app = FastAPI(title="Deezer API") |
|
|
|
load_dotenv() |
|
|
|
|
|
os.makedirs("downloads", exist_ok=True) |
|
app.mount("/downloads", StaticFiles(directory="downloads"), name="downloads") |
|
|
|
|
|
DEEZER_API_URL = "https://api.deezer.com" |
|
|
|
|
|
BASE_URL = "https://tecuts-depot.hf.space" |
|
|
|
|
|
ARL_TOKEN = os.getenv('ARL') |
|
dl = DeeLogin(arl=ARL_TOKEN) |
|
|
|
|
|
SPOTIFY_USERNAME = os.getenv("SPOTIFY_USERNAME") |
|
SPOTIFY_CREDENTIALS = os.getenv("SPOTIFY_CREDENTIALS") |
|
|
|
if not SPOTIFY_USERNAME or not SPOTIFY_CREDENTIALS: |
|
raise RuntimeError("Spotify credentials not found in environment variables") |
|
|
|
|
|
CREDENTIALS_PATH = "/tmp/credentials.json" |
|
with open(CREDENTIALS_PATH, "w") as f: |
|
json.dump({ |
|
"username": SPOTIFY_USERNAME, |
|
"credentials": SPOTIFY_CREDENTIALS, |
|
"type": "AUTHENTICATION_STORED_SPOTIFY_CREDENTIALS" |
|
}, f) |
|
|
|
|
|
spo = SpoLogin(credentials_path=CREDENTIALS_PATH) |
|
|
|
|
|
|
|
class DownloadRequest(BaseModel): |
|
url: str |
|
quality: str |
|
|
|
|
|
|
|
|
|
|
|
@app.post("/spot-track/{track_id}") |
|
def download_spotify_track(track_id: str): |
|
try: |
|
|
|
for root, dirs, files in os.walk("downloads"): |
|
for file in files: |
|
os.remove(os.path.join(root, file)) |
|
for dir in dirs: |
|
shutil.rmtree(os.path.join(root, dir)) |
|
|
|
|
|
logger.info(f"Downloading Spotify track: {track_id}") |
|
spo.download_track( |
|
link_track=f"https://open.spotify.com/track/{track_id}", |
|
output_dir="downloads", |
|
quality_download="VERY_HIGH", |
|
recursive_quality=False, |
|
recursive_download=False, |
|
not_interface=False, |
|
method_save=1 |
|
) |
|
except Exception as e: |
|
logger.error(f"Error downloading file: {e}") |
|
raise HTTPException(status_code=500, detail="File download failed") |
|
|
|
|
|
file_extension = "ogg" |
|
|
|
filepath = None |
|
for root, dirs, files in os.walk("downloads"): |
|
for file in files: |
|
if file.endswith(f'.{file_extension}'): |
|
filepath = os.path.join(root, file) |
|
break |
|
if filepath: |
|
break |
|
|
|
if not filepath: |
|
raise HTTPException(status_code=500, detail=f"{file_extension} file not found after download") |
|
if filepath: |
|
file_size = os.path.getsize(filepath) |
|
logger.info(f"Downloaded file size: {file_size} bytes") |
|
|
|
|
|
relative_path = quote(str(os.path.relpath(filepath, "downloads"))) |
|
|
|
download_url = f"{BASE_URL}/downloads/{relative_path}" |
|
logger.info(f"Download successful: {download_url}") |
|
return {"download_url": download_url} |
|
except Exception as e: |
|
logger.error(f"Error downloading track: {e}") |
|
raise HTTPException(status_code=500, detail=str(e)) |
|
|
|
|
|
@app.get("/") |
|
def read_root(): |
|
return {"message": "running"} |
|
|
|
|
|
|
|
def get_track_info(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 requests.exceptions.RequestException as e: |
|
logger.error(f"Network error fetching track metadata: {e}") |
|
raise HTTPException(status_code=500, detail=str(e)) |
|
except Exception as e: |
|
logger.error(f"Error fetching track metadata: {e}") |
|
raise HTTPException(status_code=500, detail=str(e)) |
|
|
|
|
|
|
|
@app.get("/track/{track_id}") |
|
def get_track(track_id: str): |
|
return get_track_info(track_id) |
|
|
|
|
|
|
|
@app.post("/download/track") |
|
def download_track(request: DownloadRequest): |
|
try: |
|
url = request.url |
|
quality = request.quality |
|
|
|
if quality not in ["MP3_320", "MP3_128", "FLAC"]: |
|
raise HTTPException(status_code=400, detail="Invalid quality specified") |
|
|
|
|
|
track_id = url.split("/")[-1] |
|
|
|
|
|
track_info = get_track_info(track_id) |
|
track_link = track_info.get("link") |
|
if not track_link: |
|
raise HTTPException(status_code=404, detail="Track link not found") |
|
|
|
|
|
track_title = track_info.get("title", "track") |
|
artist_name = track_info.get("artist", {}).get("name", "unknown") |
|
file_extension = "flac" if quality == "FLAC" else "mp3" |
|
expected_filename = f"{artist_name} - {track_title}.{file_extension}".replace("/", "_") |
|
|
|
|
|
for root, dirs, files in os.walk("downloads"): |
|
for file in files: |
|
os.remove(os.path.join(root, file)) |
|
for dir in dirs: |
|
shutil.rmtree(os.path.join(root, dir)) |
|
|
|
|
|
logger.info(f"Downloading track: {expected_filename}") |
|
try: |
|
|
|
dl.download_trackdee( |
|
link_track=track_link, |
|
output_dir="downloads", |
|
quality_download=quality, |
|
recursive_quality=False, |
|
recursive_download=False |
|
) |
|
except Exception as e: |
|
logger.error(f"Error downloading file: {e}") |
|
raise HTTPException(status_code=500, detail="File download failed") |
|
|
|
|
|
filepath = None |
|
for root, dirs, files in os.walk("downloads"): |
|
for file in files: |
|
if file.endswith(f'.{file_extension}'): |
|
filepath = os.path.join(root, file) |
|
break |
|
if filepath: |
|
break |
|
|
|
if not filepath: |
|
raise HTTPException(status_code=500, detail=f"{file_extension} file not found after download") |
|
if filepath: |
|
file_size = os.path.getsize(filepath) |
|
logger.info(f"Downloaded file size: {file_size} bytes") |
|
|
|
|
|
relative_path = quote(str(os.path.relpath(filepath, "downloads"))) |
|
|
|
download_url = f"{BASE_URL}/downloads/{relative_path}" |
|
logger.info(f"Download successful: {download_url}") |
|
return {"download_url": download_url} |
|
except Exception as e: |
|
logger.error(f"Error downloading track: {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 requests.exceptions.RequestException as e: |
|
logger.error(f"Network error searching tracks: {e}") |
|
raise HTTPException(status_code=500, detail=str(e)) |
|
except Exception as e: |
|
logger.error(f"Error searching tracks: {e}") |
|
raise HTTPException(status_code=500, detail=str(e)) |
|
|