Update app.py
Browse files
app.py
CHANGED
|
@@ -269,7 +269,7 @@ def jio_search(query: str) -> str:
|
|
| 269 |
raise HTTPException(status_code=500, detail=f"An error occurred while searching: {str(e)}")
|
| 270 |
|
| 271 |
|
| 272 |
-
def jio_fetch(url: str) ->
|
| 273 |
try:
|
| 274 |
# Construct the API URL
|
| 275 |
api_url = f"https://saavn.dev/api/songs?link={url}"
|
|
@@ -278,20 +278,35 @@ def jio_fetch(url: str) -> dict:
|
|
| 278 |
response = session.get(api_url)
|
| 279 |
# Check if the request was successful
|
| 280 |
response.raise_for_status()
|
| 281 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 282 |
except cloudscraper.exceptions.CloudflareChallengeError as cf_err:
|
| 283 |
logger.error(f"Cloudflare challenge error while fetching {url}: {cf_err}")
|
| 284 |
raise HTTPException(status_code=503, detail="Cloudflare challenge failed")
|
|
|
|
|
|
|
|
|
|
| 285 |
except Exception as e:
|
| 286 |
logger.error(f"Error while fetching {url}: {e}")
|
| 287 |
raise HTTPException(status_code=500, detail=f"An error occurred while fetching: {str(e)}")
|
| 288 |
-
|
| 289 |
-
|
| 290 |
# Define the request model
|
| 291 |
class JioDownloadRequest(BaseModel):
|
| 292 |
url: str
|
| 293 |
quality: str
|
| 294 |
-
|
| 295 |
@app.post("/jio_dl")
|
| 296 |
async def jio_download(request: JioDownloadRequest):
|
| 297 |
try:
|
|
@@ -300,20 +315,8 @@ async def jio_download(request: JioDownloadRequest):
|
|
| 300 |
if not url or not quality:
|
| 301 |
logger.error("Missing 'url' or 'quality' in request data.")
|
| 302 |
raise HTTPException(status_code=400, detail="Missing 'url' or 'quality' in request data")
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
if not data or len(data) == 0:
|
| 306 |
-
logger.error("No data found in the response.")
|
| 307 |
-
raise HTTPException(status_code=404, detail="No data found for the given URL.")
|
| 308 |
-
download_urls = data[0].get("downloadUrl")
|
| 309 |
-
if not download_urls:
|
| 310 |
-
logger.error("No download URLs found in the response.")
|
| 311 |
-
raise HTTPException(status_code=404, detail="No download URLs found for the given song.")
|
| 312 |
-
for download_url in download_urls:
|
| 313 |
-
if download_url.get("quality") == quality:
|
| 314 |
-
return {"download_url": download_url.get("url")}
|
| 315 |
-
logger.error(f"No download URL found for quality {quality}.")
|
| 316 |
-
raise HTTPException(status_code=404, detail=f"No download URL found for quality {quality}.")
|
| 317 |
except HTTPException:
|
| 318 |
# Re - raise the HTTPException if it's already raised in jio_fetch
|
| 319 |
raise
|
|
|
|
| 269 |
raise HTTPException(status_code=500, detail=f"An error occurred while searching: {str(e)}")
|
| 270 |
|
| 271 |
|
| 272 |
+
def jio_fetch(url: str, quality: str) -> str:
|
| 273 |
try:
|
| 274 |
# Construct the API URL
|
| 275 |
api_url = f"https://saavn.dev/api/songs?link={url}"
|
|
|
|
| 278 |
response = session.get(api_url)
|
| 279 |
# Check if the request was successful
|
| 280 |
response.raise_for_status()
|
| 281 |
+
data = response.json()
|
| 282 |
+
song_data = data.get("data")
|
| 283 |
+
if not song_data or len(song_data) == 0:
|
| 284 |
+
logger.error("No data found in the response.")
|
| 285 |
+
raise HTTPException(status_code=404, detail="No data found for the given URL.")
|
| 286 |
+
download_urls = song_data[0].get("downloadUrl")
|
| 287 |
+
if not download_urls:
|
| 288 |
+
logger.error("No download URLs found in the response.")
|
| 289 |
+
raise HTTPException(status_code=404, detail="No download URLs found for the given song.")
|
| 290 |
+
for download_url in download_urls:
|
| 291 |
+
if download_url.get("quality") == quality:
|
| 292 |
+
return download_url.get("url")
|
| 293 |
+
logger.error(f"No download URL found for quality {quality}.")
|
| 294 |
+
raise HTTPException(status_code=404, detail=f"No download URL found for quality {quality}.")
|
| 295 |
except cloudscraper.exceptions.CloudflareChallengeError as cf_err:
|
| 296 |
logger.error(f"Cloudflare challenge error while fetching {url}: {cf_err}")
|
| 297 |
raise HTTPException(status_code=503, detail="Cloudflare challenge failed")
|
| 298 |
+
except HTTPException:
|
| 299 |
+
# Re - raise the HTTPException if it's already raised
|
| 300 |
+
raise
|
| 301 |
except Exception as e:
|
| 302 |
logger.error(f"Error while fetching {url}: {e}")
|
| 303 |
raise HTTPException(status_code=500, detail=f"An error occurred while fetching: {str(e)}")
|
| 304 |
+
|
|
|
|
| 305 |
# Define the request model
|
| 306 |
class JioDownloadRequest(BaseModel):
|
| 307 |
url: str
|
| 308 |
quality: str
|
| 309 |
+
|
| 310 |
@app.post("/jio_dl")
|
| 311 |
async def jio_download(request: JioDownloadRequest):
|
| 312 |
try:
|
|
|
|
| 315 |
if not url or not quality:
|
| 316 |
logger.error("Missing 'url' or 'quality' in request data.")
|
| 317 |
raise HTTPException(status_code=400, detail="Missing 'url' or 'quality' in request data")
|
| 318 |
+
download_url = jio_fetch(url, quality)
|
| 319 |
+
return {"download_url": download_url}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 320 |
except HTTPException:
|
| 321 |
# Re - raise the HTTPException if it's already raised in jio_fetch
|
| 322 |
raise
|