m8chaa commited on
Commit
7d9b622
·
verified ·
1 Parent(s): a4d4816

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -59
app.py CHANGED
@@ -591,75 +591,34 @@ async def share_target_handle(request: Request):
591
 
592
  if "youtube.com" in shared_data or "youtu.be" in shared_data:
593
  try:
594
- # Configure yt-dlp with cookies and other options
595
  ydl_opts = {
 
596
  'quiet': True,
597
  'no_warnings': True,
598
- 'extract_flat': True, # Don't download, just get metadata
599
- 'cookiesfrombrowser': ('chrome',), # Use Chrome cookies
600
- # Add fallback options
601
- 'cookiefile': '/app/secrets/youtube.cookies', # Path to cookies file
602
- 'ignoreerrors': True,
603
  }
604
 
605
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
606
- try:
607
- info = ydl.extract_info(shared_data, download=False)
608
- if not info:
609
- raise Exception("Could not extract video info")
610
-
611
- video_id = info['id']
612
- try:
613
- # Try getting transcript first
614
- transcript_list = YouTubeTranscriptApi.get_transcript(video_id)
615
- return {
616
- "status": "success",
617
- "type": "youtube",
618
- "transcript": transcript_list,
619
- "video_info": {
620
- "title": info.get('title'),
621
- "duration": info.get('duration'),
622
- "view_count": info.get('view_count'),
623
- "thumbnail": info.get('thumbnail')
624
- }
625
- }
626
- except Exception as transcript_error:
627
- logging.error(f"Transcript error: {str(transcript_error)}")
628
- # Return video info even if transcript fails
629
- return {
630
- "status": "partial_success",
631
- "type": "youtube",
632
- "error": "Could not fetch transcript",
633
- "video_info": {
634
- "title": info.get('title'),
635
- "duration": info.get('duration'),
636
- "view_count": info.get('view_count'),
637
- "thumbnail": info.get('thumbnail')
638
- }
639
- }
640
 
641
- except Exception as ydl_error:
642
- logging.error(f"yt-dlp error: {str(ydl_error)}")
643
- # Try direct API approach if yt-dlp fails
644
- video_id = extract_video_id(shared_data)
645
- if video_id:
646
- try:
647
- transcript_list = YouTubeTranscriptApi.get_transcript(video_id)
648
- return {
649
- "status": "partial_success",
650
- "type": "youtube",
651
- "transcript": transcript_list,
652
- "video_info": None # No video info available
653
- }
654
- except Exception as api_error:
655
- logging.error(f"API error: {str(api_error)}")
656
- raise Exception("Could not fetch video data through any method")
657
-
658
  except Exception as e:
659
- logging.error(f"Error processing YouTube URL: {str(e)}")
660
  return {"status": "error", "message": str(e)}
661
 
662
- # Handle other types of shared data
663
  return {"status": "success", "message": "Data logged successfully"}
664
 
665
  def extract_video_id(url: str) -> str:
 
591
 
592
  if "youtube.com" in shared_data or "youtu.be" in shared_data:
593
  try:
594
+ # Configure yt-dlp for direct audio stream download
595
  ydl_opts = {
596
+ 'format': 'bestaudio', # Just get the best audio stream
597
  'quiet': True,
598
  'no_warnings': True,
599
+ 'outtmpl': '/tmp/%(id)s.%(ext)s', # Temporary file path
 
 
 
 
600
  }
601
 
602
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
603
+ info = ydl.extract_info(shared_data, download=True)
604
+ if not info:
605
+ raise Exception("Could not download audio")
606
+
607
+ # Get the path of the downloaded file (using actual extension)
608
+ audio_path = f"/tmp/{info['id']}.{info['ext']}"
609
+
610
+ return {
611
+ "status": "success",
612
+ "type": "youtube_audio",
613
+ "audio_path": audio_path,
614
+ "title": info.get('title', ''),
615
+ "duration": info.get('duration', 0)
616
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
617
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
618
  except Exception as e:
619
+ logging.error(f"Error downloading YouTube audio: {str(e)}")
620
  return {"status": "error", "message": str(e)}
621
 
 
622
  return {"status": "success", "message": "Data logged successfully"}
623
 
624
  def extract_video_id(url: str) -> str: