Update app.py
Browse files
app.py
CHANGED
@@ -336,3 +336,47 @@ class SpotDlResponse(BaseModel):
|
|
336 |
class ErrorResponse(BaseModel):
|
337 |
detail: str
|
338 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
336 |
class ErrorResponse(BaseModel):
|
337 |
detail: str
|
338 |
|
339 |
+
# --- API Endpoint ---
|
340 |
+
@app.post(
|
341 |
+
"/spot_dl",
|
342 |
+
response_model=SpotDlResponse,
|
343 |
+
responses={
|
344 |
+
400: {"model": ErrorResponse, "description": "Bad Request - Invalid input"},
|
345 |
+
422: {"model": ErrorResponse, "description": "Validation Error - Input data is not valid"}
|
346 |
+
},
|
347 |
+
summary="Generate SpotDL Link",
|
348 |
+
description="Accepts a URL and quality, returns a processed URL if quality is '128', "
|
349 |
+
"otherwise returns an error for higher qualities."
|
350 |
+
)
|
351 |
+
async def create_spot_dl_link(request: SpotDlRequest = Body(...)):
|
352 |
+
"""
|
353 |
+
Processes a URL and quality to generate a download link.
|
354 |
+
|
355 |
+
- **url**: The URL to process (must be a valid HTTP/HTTPS URL).
|
356 |
+
- **quality**: The desired quality. Must be one of "128", "320", "FLAC".
|
357 |
+
Currently, only "128" will result in a successful link generation.
|
358 |
+
"""
|
359 |
+
# Log the received request for debugging (optional)
|
360 |
+
print(f"Received request: url='{request.url}', quality='{request.quality}'")
|
361 |
+
|
362 |
+
# Check the quality
|
363 |
+
if request.quality == "128":
|
364 |
+
# URL-encode the input URL
|
365 |
+
encoded_url = urllib.parse.quote(str(request.url), safe='')
|
366 |
+
# Construct the target URL
|
367 |
+
output_url = f"https://velynapi.vercel.app/api/downloader/spotifydl?url={encoded_url}"
|
368 |
+
return SpotDlResponse(download_url=output_url)
|
369 |
+
elif request.quality == "320" or request.quality == "FLAC":
|
370 |
+
# If quality is 320 or FLAC, return an error as per requirements
|
371 |
+
raise HTTPException(
|
372 |
+
status_code=400,
|
373 |
+
detail=f"Quality '{request.quality}' is for Premium Users Only. '128' is allowed."
|
374 |
+
)
|
375 |
+
else:
|
376 |
+
# This case should ideally be caught by Pydantic's Literal validation,
|
377 |
+
# but as a fallback or for more general invalid quality values:
|
378 |
+
raise HTTPException(
|
379 |
+
status_code=400,
|
380 |
+
detail=f"Invalid quality value: '{request.quality}'. Allowed values are '128', '320', 'FLAC'."
|
381 |
+
)
|
382 |
+
|