Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import shutil
|
2 |
from fastapi import FastAPI, HTTPException
|
3 |
from deezspot.deezloader import DeeLogin
|
|
|
4 |
import requests
|
5 |
import os
|
6 |
import logging
|
@@ -8,6 +9,7 @@ from typing import Optional
|
|
8 |
from fastapi.staticfiles import StaticFiles
|
9 |
from dotenv import load_dotenv
|
10 |
from pydantic import BaseModel
|
|
|
11 |
|
12 |
# Set up logging
|
13 |
logging.basicConfig(level=logging.INFO)
|
@@ -31,6 +33,18 @@ BASE_URL = "https://tecuts-depot.hf.space"
|
|
31 |
ARL_TOKEN = os.getenv('ARL')
|
32 |
dl = DeeLogin(arl=ARL_TOKEN)
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
# 定义请求体模型
|
36 |
class DownloadRequest(BaseModel):
|
@@ -38,6 +52,46 @@ class DownloadRequest(BaseModel):
|
|
38 |
quality: str
|
39 |
|
40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
@app.get("/")
|
42 |
def read_root():
|
43 |
return {"message": "running"}
|
|
|
1 |
import shutil
|
2 |
from fastapi import FastAPI, HTTPException
|
3 |
from deezspot.deezloader import DeeLogin
|
4 |
+
from deezspot.spotloader import SpoLogin
|
5 |
import requests
|
6 |
import os
|
7 |
import logging
|
|
|
9 |
from fastapi.staticfiles import StaticFiles
|
10 |
from dotenv import load_dotenv
|
11 |
from pydantic import BaseModel
|
12 |
+
from urllib.parse import quote
|
13 |
|
14 |
# Set up logging
|
15 |
logging.basicConfig(level=logging.INFO)
|
|
|
33 |
ARL_TOKEN = os.getenv('ARL')
|
34 |
dl = DeeLogin(arl=ARL_TOKEN)
|
35 |
|
36 |
+
# Spotify credentials from environment variables
|
37 |
+
SPOTIFY_USERNAME = os.getenv("SPOTIFY_USERNAME")
|
38 |
+
SPOTIFY_CREDENTIALS = os.getenv("SPOTIFY_CREDENTIALS")
|
39 |
+
|
40 |
+
if not SPOTIFY_USERNAME or not SPOTIFY_CREDENTIALS:
|
41 |
+
raise RuntimeError("Spotify credentials not found in environment variables")
|
42 |
+
|
43 |
+
# Initialize Spotify client
|
44 |
+
spo = SpoLogin(credentials_path=None) # We'll pass credentials directly
|
45 |
+
spo.username = SPOTIFY_USERNAME
|
46 |
+
spo.credentials = SPOTIFY_CREDENTIALS
|
47 |
+
|
48 |
|
49 |
# 定义请求体模型
|
50 |
class DownloadRequest(BaseModel):
|
|
|
52 |
quality: str
|
53 |
|
54 |
|
55 |
+
|
56 |
+
# Download a Spotify track and return a download URL
|
57 |
+
@app.post("/spot-track/{track_id}")
|
58 |
+
def download_spotify_track(track_id: str):
|
59 |
+
try:
|
60 |
+
# Download the track using spotloader
|
61 |
+
logger.info(f"Downloading Spotify track: {track_id}")
|
62 |
+
spo.download_track(
|
63 |
+
link_track=f"https://open.spotify.com/track/{track_id}",
|
64 |
+
output_dir="downloads",
|
65 |
+
quality_download="NORMAL",
|
66 |
+
recursive_quality=False,
|
67 |
+
recursive_download=False,
|
68 |
+
not_interface=False,
|
69 |
+
method_save=1
|
70 |
+
)
|
71 |
+
|
72 |
+
# Search for the downloaded file
|
73 |
+
downloaded_files = os.listdir("downloads")
|
74 |
+
logger.info(f"Files in downloads directory: {downloaded_files}")
|
75 |
+
|
76 |
+
# Find the file that matches the track ID
|
77 |
+
matching_files = [file for file in downloaded_files if track_id in file]
|
78 |
+
|
79 |
+
if not matching_files:
|
80 |
+
raise HTTPException(status_code=500, detail="File download failed")
|
81 |
+
|
82 |
+
# Use the first matching file
|
83 |
+
filename = matching_files[0]
|
84 |
+
filepath = os.path.join("downloads", filename)
|
85 |
+
|
86 |
+
# Return the download URL
|
87 |
+
download_url = f"/downloads/{quote(filename)}"
|
88 |
+
logger.info(f"Download successful: {download_url}")
|
89 |
+
return {"download_url": download_url}
|
90 |
+
except Exception as e:
|
91 |
+
logger.error(f"Error downloading Spotify track: {e}")
|
92 |
+
raise HTTPException(status_code=500, detail=str(e))
|
93 |
+
|
94 |
+
|
95 |
@app.get("/")
|
96 |
def read_root():
|
97 |
return {"message": "running"}
|