Chrunos commited on
Commit
4e5f4ac
·
verified ·
1 Parent(s): 0cf8fb7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -17
app.py CHANGED
@@ -472,6 +472,15 @@ async def test_download(request: Request):
472
  response = extract_video_info(video_url)
473
  return response
474
 
 
 
 
 
 
 
 
 
 
475
 
476
 
477
  @app.post("/maxs")
@@ -531,7 +540,7 @@ async def download_high_quality_video(request: Request):
531
  }
532
 
533
  if is_youtube_url:
534
- dl_url = await get_track_download_url(video_url)
535
  if dl_url and "http" in dl_url:
536
  return {"url": dl_url, "requests_remaining": rate_limiter.max_requests - rate_limiter.get_current_count(user_ip)}
537
  else:
@@ -578,13 +587,13 @@ async def verify_api_key(api_key: str = Security(api_key_header)):
578
 
579
  @app.post("/audio")
580
  async def download_audio(
581
- request: Request,
582
- api_key: str = Security(verify_api_key)
583
  ):
584
  data = await request.json()
585
  video_url = data.get('url')
586
- cookiefile = "firefox-cookies.txt"
587
- env_to_cookies_from_env("firefox-cookies.txt")
588
 
589
  timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
590
  output_template = str(Path(global_download_dir) / f'%(title).70s_{timestamp}.%(ext)s')
@@ -595,7 +604,7 @@ async def download_audio(
595
  'quiet': True,
596
  'no_warnings': True,
597
  'noprogress': True,
598
- 'cookiefile': cookiefile,
599
  'postprocessors': [{
600
  'key': 'FFmpegExtractAudio',
601
  'preferredcodec': 'mp3',
@@ -603,18 +612,26 @@ async def download_audio(
603
  }]
604
 
605
  }
606
-
607
- await run_in_threadpool(lambda: yt_dlp.YoutubeDL(ydl_opts).download([video_url]))
 
 
 
 
 
 
 
 
608
 
609
- downloaded_files = list(Path(global_download_dir).glob(f"*_{timestamp}.*"))
610
- if not downloaded_files:
611
- return {"error": "Download failed"}
612
-
613
- downloaded_file = downloaded_files[0]
614
- encoded_filename = urllib.parse.quote(downloaded_file.name)
615
- download_url = f"{BASE_URL}/file/{encoded_filename}"
616
- gc.collect()
617
- return {"url": download_url}
618
 
619
  # Configure logging
620
  logging.basicConfig(level=logging.INFO)
 
472
  response = extract_video_info(video_url)
473
  return response
474
 
475
+
476
+ async def get_audio_download_url(track_id: str, quality: str) -> str:
477
+ youtube_url = f'https://www.youtube.com/watch?v={track_id}'
478
+ if quality == 'mp3':
479
+ type = 'audio'
480
+ else:
481
+ type = 'video'
482
+ donwnload_url = f'https://chrunos-shadl.hf.space/yt/dl?url={youtube_url}&type={type}&quality={quality}'
483
+ return donwnload_url
484
 
485
 
486
  @app.post("/maxs")
 
540
  }
541
 
542
  if is_youtube_url:
543
+ dl_url = await get_audio_download_url(video_url)
544
  if dl_url and "http" in dl_url:
545
  return {"url": dl_url, "requests_remaining": rate_limiter.max_requests - rate_limiter.get_current_count(user_ip)}
546
  else:
 
587
 
588
  @app.post("/audio")
589
  async def download_audio(
590
+ request: Request
591
+ #api_key: str = Security(verify_api_key)
592
  ):
593
  data = await request.json()
594
  video_url = data.get('url')
595
+ #cookiefile = "firefox-cookies.txt"
596
+ #env_to_cookies_from_env("firefox-cookies.txt")
597
 
598
  timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
599
  output_template = str(Path(global_download_dir) / f'%(title).70s_{timestamp}.%(ext)s')
 
604
  'quiet': True,
605
  'no_warnings': True,
606
  'noprogress': True,
607
+ #'cookiefile': cookiefile,
608
  'postprocessors': [{
609
  'key': 'FFmpegExtractAudio',
610
  'preferredcodec': 'mp3',
 
612
  }]
613
 
614
  }
615
+ if is_youtube_url:
616
+ dl_url = await get_audio_download_url(video_url)
617
+ if dl_url and "http" in dl_url:
618
+ return {"url": dl_url, "requests_remaining": rate_limiter.max_requests - rate_limiter.get_current_count(user_ip)}
619
+ else:
620
+ return {
621
+ "error": "Failed to Fetch the video."
622
+ }
623
+ else:
624
+ await run_in_threadpool(lambda: yt_dlp.YoutubeDL(ydl_opts).download([video_url]))
625
 
626
+ downloaded_files = list(Path(global_download_dir).glob(f"*_{timestamp}.*"))
627
+ if not downloaded_files:
628
+ return {"error": "Download failed"}
629
+
630
+ downloaded_file = downloaded_files[0]
631
+ encoded_filename = urllib.parse.quote(downloaded_file.name)
632
+ download_url = f"{BASE_URL}/file/{encoded_filename}"
633
+ gc.collect()
634
+ return {"url": download_url}
635
 
636
  # Configure logging
637
  logging.basicConfig(level=logging.INFO)