Update app.py
Browse files588 unmodified lines5895905915925935945955965975985996006016026036046056066076085 unmodified lines61461561661761861962062162262362462562662762820 unmodified lines649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682588 unmodified lines# --- Mocking your helper functions/classes for the script to run ---class SongInfoResponse(BaseModel):song_name: strsingers: stralbum: Optional[str]download_url: strduration: intcover_url: Optional[str]def legalizestring(s: str) -> str:return s.strip() if s else ""# --- API Logic ---HEADERS = {"referer": "https://spotubedl.com/","sec-ch-ua": '"Not:A-Brand";v="99", "Google Chrome";v="145", "Chromium";v="145"',5 unmodified lines"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36"}class InfoRequest(BaseModel):url: str # Expected format: .../metadata/{id} or just the ID@app.post("/info")async def get_info(request: InfoRequest):"""Extracts song metadata and returns the raw download details."""# Extract ID from URL if a full URL is provided, else assume it's an IDsong_id = request.url.split("/")[-1]async with httpx.AsyncClient() as client:20 unmodified linesexcept Exception as e:raise HTTPException(status_code=500, detail=str(e))@app.post("/download")async def get_download_link(request: InfoRequest):"""Fetches the actual MP3 download URL using the YouTube Video ID."""# First, we need to get the video ID (reusing the logic from /info)info_data = await get_info(request)vid = info_data["video_id"]metadata = info_data["metadata"]async with httpx.AsyncClient() as client:try:# 3. Get Download Linkdl_api_url = f"https://spotubedl.com/api/download/{vid}?engine=v1&format=mp3&quality=320"dl_resp = await client.get(dl_api_url, headers=HEADERS)dl_resp.raise_for_status()dl_data = dl_resp.json()# Constructing a response similar to your SongInfo objectreturn {"song_name": legalizestring(metadata.get("name", "Unknown")),"singers": legalizestring(", ".join(metadata.get("artists", []))),"album": legalizestring(metadata.get("album_name", "")),"download_url": dl_data.get("url"),"duration_s": metadata.get("duration"),"cover_url": metadata.get("cover_url"),}except Exception as e:raise HTTPException(status_code=500, detail=str(e))588 unmodified lines5895905915925935945955965975 unmodified lines60360460560660760860961061161261361461561661761861962062162220 unmodified lines643644645646647648649650651652653654655656657658659660661662663664665666667668669588 unmodified lines# --- Headers required by the source API ---HEADERS = {"referer": "https://spotubedl.com/","sec-ch-ua": '"Not:A-Brand";v="99", "Google Chrome";v="145", "Chromium";v="145"',5 unmodified lines"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36"}# --- Request Models ---class InfoRequest(BaseModel):url: str # The Spotify URL or Song IDclass DownloadRequest(BaseModel):vid: str # The YouTube Video ID extracted from the /info endpoint# --- Endpoints ---@app.post("/info")async def get_info(request: InfoRequest):"""Extracts song metadata and returns the YouTube Video ID (vid)."""song_id = request.url.split("/")[-1]async with httpx.AsyncClient() as client:20 unmodified linesexcept Exception as e:raise HTTPException(status_code=500, detail=str(e))@app.post("/download")async def get_download_link(request: DownloadRequest):"""Fetches the actual MP3 download URL using the provided YouTube Video ID (vid)."""async with httpx.AsyncClient() as client:try:# 3. Get Download Link using the vid from the request bodydl_api_url = f"https://spotubedl.com/api/download/{request.vid}?engine=v1&format=mp3&quality=320"dl_resp = await client.get(dl_api_url, headers=HEADERS)dl_resp.raise_for_status()dl_data = dl_resp.json()return {"video_id": request.vid,"download_url": dl_data.get("url"),"raw_response": dl_data}except Exception as e:raise HTTPException(status_code=500, detail=str(e))
app.py+17-30