Chrunos commited on
Commit
8e49823
·
verified ·
1 Parent(s): fe4a943

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -20
app.py CHANGED
@@ -251,7 +251,7 @@ async def get_track_download_url(video_url: str) -> str:
251
  return {"error": "Download URL not found"}
252
 
253
 
254
- def jio_search(query: str) -> str:
255
  try:
256
  # Construct the API URL
257
  api_url = f"https://saavn.dev/api/search/songs?query={query}"
@@ -260,13 +260,29 @@ def jio_search(query: str) -> str:
260
  response = session.get(api_url)
261
  # Check if the request was successful
262
  response.raise_for_status()
263
- return response.text
 
 
 
 
 
 
 
 
 
 
 
 
 
264
  except cloudscraper.exceptions.CloudflareChallengeError as cf_err:
265
- logger.error(f"Cloudflare challenge error while searching for {query}: {cf_err}")
266
  raise HTTPException(status_code=503, detail="Cloudflare challenge failed")
 
 
 
267
  except Exception as e:
268
- logger.error(f"Error while searching for {query}: {e}")
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:
@@ -304,48 +320,59 @@ def jio_fetch(url: str, quality: str) -> str:
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:
313
  url = request.url
 
314
  quality = request.quality
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
323
  except Exception as e:
324
  logger.error(f"Error in jio_download: {e}")
325
- raise HTTPException(status_code=500, detail=f"An error occurred during download: {str(e)}")
 
326
 
327
  @app.post("/jio_dls")
328
  async def jio_download(request: JioDownloadRequest):
329
  try:
330
  url = request.url
331
  quality = request.quality
332
- if not url or not quality:
333
- logger.error("Missing 'url' or 'quality' in request data.")
334
- raise HTTPException(status_code=400, detail="Missing 'url' or 'quality' in request data")
335
  if quality == '320kbps':
336
  return {
337
  "error": "Quality 320kbps is for Premium users only",
338
  "premium": "https://chrunos.com/premium-shortcuts/"
339
  }
340
- else:
341
  download_url = jio_fetch(url, quality)
342
  return {"download_url": download_url}
 
 
 
 
 
 
343
  except HTTPException:
344
- # Re - raise the HTTPException if it's already raised in jio_fetch
345
  raise
346
  except Exception as e:
347
  logger.error(f"Error in jio_download: {e}")
348
- raise HTTPException(status_code=500, detail=f"An error occurred during download: {str(e)}")
349
 
350
 
351
 
 
251
  return {"error": "Download URL not found"}
252
 
253
 
254
+ def jio_search(query: str, quality: str) -> str:
255
  try:
256
  # Construct the API URL
257
  api_url = f"https://saavn.dev/api/search/songs?query={query}"
 
260
  response = session.get(api_url)
261
  # Check if the request was successful
262
  response.raise_for_status()
263
+ data = response.json().get("data")
264
+ song_data = data.get("results")
265
+ if not song_data or len(song_data) == 0:
266
+ logger.error("No data found in the response.")
267
+ raise HTTPException(status_code=404, detail="No data found for the given URL.")
268
+ download_urls = song_data[0].get("downloadUrl")
269
+ if not download_urls:
270
+ logger.error("No download URLs found in the response.")
271
+ raise HTTPException(status_code=404, detail="No download URLs found for the given song.")
272
+ for download_url in download_urls:
273
+ if download_url.get("quality") == quality:
274
+ return download_url.get("url")
275
+ logger.error(f"No download URL found for quality {quality}.")
276
+ raise HTTPException(status_code=404, detail=f"No download URL found for quality {quality}.")
277
  except cloudscraper.exceptions.CloudflareChallengeError as cf_err:
278
+ logger.error(f"Cloudflare challenge error while fetching {url}: {cf_err}")
279
  raise HTTPException(status_code=503, detail="Cloudflare challenge failed")
280
+ except HTTPException:
281
+ # Re - raise the HTTPException if it's already raised
282
+ raise
283
  except Exception as e:
284
+ logger.error(f"Error while fetching {url}: {e}")
285
+ raise HTTPException(status_code=500, detail=f"An error occurred while fetching: {str(e)}")
286
 
287
 
288
  def jio_fetch(url: str, quality: str) -> str:
 
320
 
321
  # Define the request model
322
  class JioDownloadRequest(BaseModel):
323
+ url: str = None
324
+ query: str = None
325
+ quality: str = None
326
 
327
  @app.post("/jio_dl")
328
  async def jio_download(request: JioDownloadRequest):
329
  try:
330
  url = request.url
331
+ query = request.query
332
  quality = request.quality
333
+ if url and quality:
334
+ download_url = jio_fetch(url, quality)
335
+ return {"download_url": download_url}
336
+ elif query:
337
+ download_url = jio_search(query, quality)
338
+ return {"download_url": download_url}
339
+ else:
340
+ logger.error("Missing 'url' and 'quality' or 'query' in request data.")
341
+ raise HTTPException(status_code=400, detail="Missing 'url' and 'quality' or 'query' in request data")
342
  except HTTPException:
343
+ # Re - raise the HTTPException if it's already raised
344
  raise
345
  except Exception as e:
346
  logger.error(f"Error in jio_download: {e}")
347
+ raise HTTPException(status_code=500, detail=f"An error occurred during the operation: {str(e)}")
348
+
349
 
350
  @app.post("/jio_dls")
351
  async def jio_download(request: JioDownloadRequest):
352
  try:
353
  url = request.url
354
  quality = request.quality
355
+ query = request.query
 
 
356
  if quality == '320kbps':
357
  return {
358
  "error": "Quality 320kbps is for Premium users only",
359
  "premium": "https://chrunos.com/premium-shortcuts/"
360
  }
361
+ if url and quality:
362
  download_url = jio_fetch(url, quality)
363
  return {"download_url": download_url}
364
+ elif query:
365
+ download_url = jio_search(query, quality)
366
+ return {"download_url": download_url}
367
+ else:
368
+ logger.error("Missing 'url' and 'quality' or 'query' in request data.")
369
+ raise HTTPException(status_code=400, detail="Missing 'url' and 'quality' or 'query' in request data")
370
  except HTTPException:
371
+ # Re - raise the HTTPException if it's already raised
372
  raise
373
  except Exception as e:
374
  logger.error(f"Error in jio_download: {e}")
375
+ raise HTTPException(status_code=500, detail=f"An error occurred during the operation: {str(e)}")
376
 
377
 
378