rocky250 commited on
Commit
8f09e88
Β·
verified Β·
1 Parent(s): c6c2c6c

Update fetcher.py

Browse files
Files changed (1) hide show
  1. fetcher.py +156 -181
fetcher.py CHANGED
@@ -1,231 +1,206 @@
1
  """
2
- fetcher.py β€” All YouTube Data API v3 + transcript fetching logic.
3
- Completely decoupled from the UI layer.
4
  """
5
 
6
- import os
7
  import re
8
- import time
9
- from typing import Optional, Tuple, List, Dict, Any
10
-
11
  import pandas as pd
12
- from googleapiclient.discovery import build
13
- from googleapiclient.errors import HttpError
14
- from youtube_transcript_api import (
15
- YouTubeTranscriptApi,
16
- NoTranscriptFound,
17
- TranscriptsDisabled,
18
- VideoUnavailable,
19
- )
20
 
21
 
22
- # ── ID Extraction ──────────────────────────────────────────────────────────────
 
 
23
 
24
- def extract_video_id(url_or_id: str) -> Optional[str]:
25
- """Extract YouTube video ID from any URL format or a raw ID."""
26
- url_or_id = url_or_id.strip()
27
  patterns = [
28
- r"(?:v=|youtu\.be/|embed/|shorts/|live/)([A-Za-z0-9_-]{11})",
29
- r"^([A-Za-z0-9_-]{11})$",
30
  ]
31
- for p in patterns:
32
- m = re.search(p, url_or_id)
33
  if m:
34
  return m.group(1)
35
  return None
36
 
37
 
38
- # ── YouTube API Client ─────────────────────────────────────────────────────────
 
 
39
 
40
- def get_yt_client(api_key: str):
41
- return build("youtube", "v3", developerKey=api_key, cache_discovery=False)
 
 
 
 
42
 
43
 
44
- # ── Video Metadata ─────────────────────────────────────────────────────────────
 
 
45
 
46
- def fetch_video_metadata(video_id: str, api_key: str) -> Tuple[Optional[Dict], Optional[str]]:
47
- """
48
- Returns (metadata_dict, error_string).
49
- metadata_dict keys: title, channel_title, description, tags, duration,
50
- published_at, view_count, like_count, comment_count,
51
- thumbnail_url, video_id
52
- """
53
  try:
54
- yt = get_yt_client(api_key)
55
- resp = yt.videos().list(
56
- part="snippet,contentDetails,statistics",
57
- id=video_id,
58
- ).execute()
59
-
60
- if not resp.get("items"):
61
- return None, "Video not found or unavailable."
62
-
63
- item = resp["items"][0]
64
- snippet = item.get("snippet", {})
65
- stats = item.get("statistics", {})
66
- content = item.get("contentDetails", {})
67
-
68
- # Parse ISO 8601 duration e.g. PT4M13S β†’ "4m 13s"
69
- raw_dur = content.get("duration", "PT0S")
70
- duration_str = _parse_duration(raw_dur)
71
-
72
- metadata = {
73
- "video_id": video_id,
74
- "title": snippet.get("title", "N/A"),
75
- "channel_title": snippet.get("channelTitle", "N/A"),
76
- "description": snippet.get("description", ""),
77
- "tags": snippet.get("tags", []),
78
- "published_at": snippet.get("publishedAt", "")[:10],
79
- "duration": duration_str,
80
- "view_count": int(stats.get("viewCount", 0)),
81
- "like_count": int(stats.get("likeCount", 0)),
82
- "comment_count": int(stats.get("commentCount", 0)),
83
  "thumbnail_url": (
84
- snippet.get("thumbnails", {})
85
- .get("maxres", snippet.get("thumbnails", {}).get("high", {}))
86
- .get("url", "")
87
  ),
 
 
 
 
88
  }
89
- return metadata, None
90
 
91
- except HttpError as e:
92
- return None, f"YouTube API HTTP error {e.resp.status}: {e._get_reason()}"
93
- except Exception as e:
94
- return None, f"Unexpected error: {e}"
95
 
96
 
97
- def _parse_duration(iso: str) -> str:
98
- """Convert PT4M13S β†’ '4m 13s'"""
99
- m = re.match(r"PT(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?", iso)
100
- if not m:
101
- return "N/A"
102
- h, mn, s = m.group(1), m.group(2), m.group(3)
103
- parts = []
104
- if h: parts.append(f"{h}h")
105
- if mn: parts.append(f"{mn}m")
106
- if s: parts.append(f"{s}s")
107
- return " ".join(parts) or "0s"
108
-
109
-
110
- # ── Transcript ─────────────────────────────────────────────────────────────────
111
-
112
- def fetch_transcript(video_id: str) -> Tuple[str, str]:
113
- """
114
- Returns (transcript_text, status_message).
115
- Tries English first, then any available language.
116
- """
117
  try:
118
- segments = YouTubeTranscriptApi.get_transcript(video_id, languages=["en"])
 
119
  text = " ".join(s["text"] for s in segments)
120
- return text, f"βœ… English transcript fetched ({len(segments)} segments, {len(text.split())} words)"
121
-
122
- except NoTranscriptFound:
123
- # Try any available
124
- try:
125
- tl = YouTubeTranscriptApi.list_transcripts(video_id)
126
- for t in tl:
127
- try:
128
- segments = t.fetch()
129
- text = " ".join(s["text"] for s in segments)
130
- return text, f"βœ… Transcript fetched (lang: {t.language_code}, {len(text.split())} words)"
131
- except Exception:
132
- continue
133
- return "", "⚠️ No usable transcript found for any language."
134
- except Exception as e:
135
- return "", f"⚠️ Transcript listing failed: {e}"
136
-
137
- except TranscriptsDisabled:
138
- return "", "⚠️ Transcripts are disabled for this video."
139
- except VideoUnavailable:
140
- return "", "❌ Video is unavailable."
141
- except Exception as e:
142
- return "", f"⚠️ Transcript error: {e}"
143
-
144
-
145
- # ── Comments ───────────────────────────────────────────────────────────────────
146
 
147
  def fetch_comments(
148
  video_id: str,
149
  api_key: str,
150
- max_comments: int = 200,
151
- ) -> Tuple[pd.DataFrame, str]:
152
- """
153
- Fetch top-level comment threads.
154
- Returns (DataFrame with cols: author, text, likes, published_at), status_msg.
155
- Handles disabled comments gracefully.
156
- """
157
  try:
158
- yt = get_yt_client(api_key)
159
- comments = []
160
- next_page = None
161
-
162
- while len(comments) < max_comments:
163
- kwargs = dict(
164
- part="snippet",
165
- videoId=video_id,
166
- maxResults=min(100, max_comments - len(comments)),
167
- order="relevance",
168
- textFormat="plainText",
 
 
 
 
 
169
  )
170
- if next_page:
171
- kwargs["pageToken"] = next_page
172
-
173
- resp = yt.commentThreads().list(**kwargs).execute()
174
-
175
- for item in resp.get("items", []):
176
- top = item["snippet"]["topLevelComment"]["snippet"]
177
- comments.append({
178
- "author": top.get("authorDisplayName", "Anonymous"),
179
- "text": top.get("textDisplay", ""),
180
- "likes": int(top.get("likeCount", 0)),
181
- "published_at": top.get("publishedAt", "")[:10],
 
182
  })
183
 
184
- next_page = resp.get("nextPageToken")
185
- if not next_page:
186
  break
187
 
188
- if not comments:
189
- return pd.DataFrame(), "⚠️ No comments found."
190
 
191
- df = pd.DataFrame(comments)
192
- return df, f"βœ… Fetched {len(df)} comments"
193
 
194
- except HttpError as e:
195
- reason = e._get_reason()
196
- if "commentsDisabled" in reason or e.resp.status == 403:
197
- return pd.DataFrame(), "⚠️ Comments are disabled for this video."
198
- return pd.DataFrame(), f"❌ API error {e.resp.status}: {reason}"
199
- except Exception as e:
200
- return pd.DataFrame(), f"❌ Comments error: {e}"
201
 
202
 
203
- # ── Search by keyword (for uploaded files) ────────────────────────────────────
 
 
204
 
205
- def search_videos_by_title(query: str, api_key: str, max_results: int = 5) -> List[Dict]:
206
- """
207
- Search YouTube for videos matching a title/keyword query.
208
- Used when user uploads a video file and we need to find it on YouTube.
209
- """
210
  try:
211
- yt = get_yt_client(api_key)
212
- resp = yt.search().list(
213
- part="snippet",
214
- q=query,
215
- type="video",
216
- maxResults=max_results,
217
- ).execute()
218
-
 
 
 
 
219
  results = []
220
- for item in resp.get("items", []):
 
 
 
 
221
  results.append({
222
- "video_id": item["id"]["videoId"],
223
- "title": item["snippet"]["title"],
224
- "channel_title": item["snippet"]["channelTitle"],
225
- "thumbnail_url": item["snippet"]["thumbnails"]["default"]["url"],
226
- "published_at": item["snippet"]["publishedAt"][:10],
227
  })
228
  return results
229
-
230
  except Exception:
231
  return []
 
1
  """
2
+ fetcher.py β€” YouTube Data API v3 helpers
 
3
  """
4
 
 
5
  import re
6
+ import requests
 
 
7
  import pandas as pd
 
 
 
 
 
 
 
 
8
 
9
 
10
+ # ─────────────────────────────────────────────────────────────────────────────
11
+ # Video ID extraction
12
+ # ─────────────────────────────────────────────────────────────────────────────
13
 
14
+ def extract_video_id(url_or_id: str) -> str | None:
15
+ """Return an 11-char YouTube video ID, or None if not found."""
 
16
  patterns = [
17
+ r"(?:youtube\.com/watch\?v=|youtu\.be/|youtube\.com/embed/|youtube\.com/shorts/)([a-zA-Z0-9_-]{11})",
18
+ r"^([a-zA-Z0-9_-]{11})$",
19
  ]
20
+ for pattern in patterns:
21
+ m = re.search(pattern, url_or_id)
22
  if m:
23
  return m.group(1)
24
  return None
25
 
26
 
27
+ # ─────────────────────────────────────────────────────────────────────────────
28
+ # Duration parser
29
+ # ─────────────────────────────────────────────────────────────────────────────
30
 
31
+ def _parse_duration(iso: str) -> str:
32
+ m = re.match(r"PT(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?", iso or "PT0S")
33
+ if not m:
34
+ return "0:00"
35
+ h, mn, s = (int(x or 0) for x in m.groups())
36
+ return f"{h}:{mn:02d}:{s:02d}" if h else f"{mn}:{s:02d}"
37
 
38
 
39
+ # ─────────────────────────────────────────────────────────────────────────────
40
+ # Metadata
41
+ # ─────────────────────────────────────────────────────────────────────────────
42
 
43
+ def fetch_video_metadata(video_id: str, api_key: str) -> tuple[dict | None, str | None]:
44
+ """Return (meta_dict, error_string). One will be None."""
 
 
 
 
 
45
  try:
46
+ resp = requests.get(
47
+ "https://www.googleapis.com/youtube/v3/videos",
48
+ params={
49
+ "id": video_id,
50
+ "key": api_key,
51
+ "part": "snippet,statistics,contentDetails",
52
+ },
53
+ timeout=15,
54
+ )
55
+ data = resp.json()
56
+ if "error" in data:
57
+ return None, data["error"].get("message", "YouTube API error")
58
+
59
+ items = data.get("items", [])
60
+ if not items:
61
+ return None, "Video not found β€” check the ID or URL."
62
+
63
+ item = items[0]
64
+ sn = item.get("snippet", {})
65
+ st = item.get("statistics", {})
66
+ cd = item.get("contentDetails", {})
67
+
68
+ meta = {
69
+ "title": sn.get("title", "Unknown"),
70
+ "description": sn.get("description", ""),
71
+ "channel_title": sn.get("channelTitle", "Unknown"),
72
+ "published_at": sn.get("publishedAt", "")[:10],
73
+ "tags": sn.get("tags", []),
 
74
  "thumbnail_url": (
75
+ sn.get("thumbnails", {}).get("high", {}).get("url", "")
76
+ or sn.get("thumbnails", {}).get("medium", {}).get("url", "")
 
77
  ),
78
+ "view_count": int(st.get("viewCount", 0)),
79
+ "like_count": int(st.get("likeCount", 0)),
80
+ "comment_count": int(st.get("commentCount", 0)),
81
+ "duration": _parse_duration(cd.get("duration", "PT0S")),
82
  }
83
+ return meta, None
84
 
85
+ except requests.exceptions.Timeout:
86
+ return None, "Request timed out. Check your internet connection."
87
+ except Exception as exc:
88
+ return None, str(exc)
89
 
90
 
91
+ # ─────────────────────────────────────────────────────────────────────────────
92
+ # Transcript
93
+ # ─────────────────────────────────────────────────────────────────────────────
94
+
95
+ def fetch_transcript(video_id: str) -> tuple[str, str]:
96
+ """Return (text, status_message)."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  try:
98
+ from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound
99
+ segments = YouTubeTranscriptApi.get_transcript(video_id)
100
  text = " ".join(s["text"] for s in segments)
101
+ return text, f"βœ… Transcript: {len(text.split())} words"
102
+ except Exception as exc:
103
+ short = str(exc)[:80]
104
+ return "", f"⚠️ Transcript unavailable: {short}"
105
+
106
+
107
+ # ─────────────────────────────────────────────────────────────────────────────
108
+ # Comments
109
+ # ─────────────────────────────────────────────────────────────────────────────
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
 
111
  def fetch_comments(
112
  video_id: str,
113
  api_key: str,
114
+ max_comments: int = 150,
115
+ ) -> tuple[pd.DataFrame, str]:
116
+ """Return (DataFrame, status_message)."""
117
+ rows = []
118
+ next_token = None
119
+
 
120
  try:
121
+ while len(rows) < max_comments:
122
+ want = min(100, max_comments - len(rows))
123
+ params = {
124
+ "videoId": video_id,
125
+ "key": api_key,
126
+ "part": "snippet",
127
+ "maxResults": want,
128
+ "order": "relevance",
129
+ }
130
+ if next_token:
131
+ params["pageToken"] = next_token
132
+
133
+ resp = requests.get(
134
+ "https://www.googleapis.com/youtube/v3/commentThreads",
135
+ params=params,
136
+ timeout=15,
137
  )
138
+ data = resp.json()
139
+
140
+ if "error" in data:
141
+ msg = data["error"].get("message", "Comment API error")
142
+ break
143
+
144
+ for item in data.get("items", []):
145
+ c = item["snippet"]["topLevelComment"]["snippet"]
146
+ rows.append({
147
+ "author": c.get("authorDisplayName", ""),
148
+ "text": c.get("textDisplay", ""),
149
+ "likes": int(c.get("likeCount", 0)),
150
+ "published_at": c.get("publishedAt", "")[:10],
151
  })
152
 
153
+ next_token = data.get("nextPageToken")
154
+ if not next_token or not data.get("items"):
155
  break
156
 
157
+ if not rows:
158
+ return pd.DataFrame(), "⚠️ No comments fetched (comments may be disabled)"
159
 
160
+ df = pd.DataFrame(rows)
161
+ return df, f"βœ… Comments: {len(df)} fetched"
162
 
163
+ except requests.exceptions.Timeout:
164
+ return pd.DataFrame(), "❌ Comments request timed out"
165
+ except Exception as exc:
166
+ return pd.DataFrame(), f"❌ Comments error: {str(exc)[:80]}"
 
 
 
167
 
168
 
169
+ # ────────────────────────────────────────────────────────��────────────────────
170
+ # Search by keyword
171
+ # ─────────────────────────────────────────────────────────────────────────────
172
 
173
+ def search_videos_by_title(
174
+ keyword: str,
175
+ api_key: str,
176
+ max_results: int = 5,
177
+ ) -> list[dict]:
178
  try:
179
+ resp = requests.get(
180
+ "https://www.googleapis.com/youtube/v3/search",
181
+ params={
182
+ "q": keyword,
183
+ "key": api_key,
184
+ "part": "snippet",
185
+ "type": "video",
186
+ "maxResults": max_results,
187
+ },
188
+ timeout=15,
189
+ )
190
+ data = resp.json()
191
  results = []
192
+ for item in data.get("items", []):
193
+ vid_id = item.get("id", {}).get("videoId", "")
194
+ sn = item.get("snippet", {})
195
+ if not vid_id:
196
+ continue
197
  results.append({
198
+ "video_id": vid_id,
199
+ "title": sn.get("title", ""),
200
+ "channel_title": sn.get("channelTitle", ""),
201
+ "published_at": sn.get("publishedAt", "")[:10],
202
+ "thumbnail_url": sn.get("thumbnails", {}).get("medium", {}).get("url", ""),
203
  })
204
  return results
 
205
  except Exception:
206
  return []