Spaces:
Sleeping
Sleeping
Update fetcher.py
Browse files6 unmodified lines7891011121314158 unmodified lines2425262728293031323 unmodified lines36373839404142434443 unmodified lines8889909192939495961 unmodified line989910010110210310410510610710810911011111242 unmodified lines1551561571581591601611621631641651661671681691701711721731746 unmodified linesimport pandas as pd# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# Video ID extraction# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββdef extract_video_id(url_or_id: str) -> str | None:"""Return an 11-char YouTube video ID, or None if not found."""8 unmodified linesreturn None# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# Duration parser# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββdef _parse_duration(iso: str) -> str:m = re.match(r"PT(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?", iso or "PT0S")3 unmodified linesreturn f"{h}:{mn:02d}:{s:02d}" if h else f"{mn}:{s:02d}"# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# Metadata# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββdef fetch_video_metadata(video_id: str, api_key: str) -> tuple[dict | None, str | None]:"""Return (meta_dict, error_string). One will be None."""43 unmodified linesreturn None, str(exc)# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# Transcript# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββdef fetch_transcript(video_id: str) -> tuple[str, str]:"""Return (text, status_message)."""1 unmodified linefrom youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFoundsegments = YouTubeTranscriptApi.get_transcript(video_id)text = " ".join(s["text"] for s in segments)return text, f"β Transcript: {len(text.split())} words"except Exception as exc:short = str(exc)[:80]return "", f"β οΈ Transcript unavailable: {short}"# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# Comments# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββdef fetch_comments(video_id: str,42 unmodified linesbreakif not rows:return pd.DataFrame(), "β οΈ No comments fetched (comments may be disabled)"df = pd.DataFrame(rows)return df, f"β Comments: {len(df)} fetched"except requests.exceptions.Timeout:return pd.DataFrame(), "β Comments request timed out"except Exception as exc:return pd.DataFrame(), f"β Comments error: {str(exc)[:80]}"# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# Search by keyword# βββββββββββββββββββββββββββββββοΏ½οΏ½βββββββββββββββββββββββββββββββββββββββββββββdef search_videos_by_title(keyword: str,6 unmodified lines7891011121314158 unmodified lines2425262728293031323 unmodified lines36373839404142434443 unmodified lines8889909192939495961 unmodified line989910010110210310410510610710810911011111242 unmodified lines1551561571581591601611621631641651661671681691701711721731746 unmodified linesimport pandas as pd# Video ID extractiondef extract_video_id(url_or_id: str) -> str | None:"""Return an 11-char YouTube video ID, or None if not found."""8 unmodified linesreturn None# Duration parserdef _parse_duration(iso: str) -> str:m = re.match(r"PT(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?", iso or "PT0S")3 unmodified linesreturn f"{h}:{mn:02d}:{s:02d}" if h else f"{mn}:{s:02d}"# Metadatadef fetch_video_metadata(video_id: str, api_key: str) -> tuple[dict | None, str | None]:"""Return (meta_dict, error_string). One will be None."""43 unmodified linesreturn None, str(exc)# Transcriptdef fetch_transcript(video_id: str) -> tuple[str, str]:"""Return (text, status_message)."""1 unmodified linefrom youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFoundsegments = YouTubeTranscriptApi.get_transcript(video_id)text = " ".join(s["text"] for s in segments)return text, f" Transcript: {len(text.split())} words"except Exception as exc:short = str(exc)[:80]return "", f" Transcript unavailable: {short}"# Commentsdef fetch_comments(video_id: str,42 unmodified linesbreakif not rows:return pd.DataFrame(), " No comments fetched (comments may be disabled)"df = pd.DataFrame(rows)return df, f" Comments: {len(df)} fetched"except requests.exceptions.Timeout:return pd.DataFrame(), " Comments request timed out"except Exception as exc:return pd.DataFrame(), f" Comments error: {str(exc)[:80]}"# Search by keyworddef search_videos_by_title(keyword: str,
fetcher.py+18-18