-
-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathcontent_fetcher.py
More file actions
312 lines (255 loc) · 11.5 KB
/
content_fetcher.py
File metadata and controls
312 lines (255 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
"""Content fetcher for various platforms.
Extracts content from URLs: YouTube, Reddit, Twitter, web pages.
"""
import re
import json
from dataclasses import dataclass
from typing import Optional
from urllib.parse import urlparse, parse_qs
import requests
from youtube_transcript_api import YouTubeTranscriptApi
from youtube_transcript_api._errors import TranscriptsDisabled, NoTranscriptFound
@dataclass
class FetchedContent:
"""Represents fetched content from a URL."""
url: str
platform: str # youtube, reddit, twitter, web
title: Optional[str] = None
content: Optional[str] = None
transcript: Optional[str] = None
author: Optional[str] = None
metadata: Optional[dict] = None
error: Optional[str] = None
class ContentFetcher:
"""Fetches and extracts content from various platforms."""
def __init__(self):
self.session = requests.Session()
self.session.headers.update({
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36"
})
def detect_platform(self, url: str) -> str:
"""Detect which platform a URL belongs to."""
parsed = urlparse(url)
domain = parsed.netloc.lower()
if "youtube.com" in domain or "youtu.be" in domain:
return "youtube"
elif "reddit.com" in domain or "redd.it" in domain:
return "reddit"
elif "twitter.com" in domain or "x.com" in domain:
return "twitter"
elif "instagram.com" in domain:
return "instagram"
elif "linkedin.com" in domain:
return "linkedin"
else:
return "web"
def fetch(self, url: str) -> FetchedContent:
"""Fetch content from any supported URL."""
platform = self.detect_platform(url)
fetchers = {
"youtube": self._fetch_youtube,
"reddit": self._fetch_reddit,
"twitter": self._fetch_twitter,
"instagram": self._fetch_instagram,
"linkedin": self._fetch_linkedin,
"web": self._fetch_web,
}
fetcher = fetchers.get(platform, self._fetch_web)
return fetcher(url, platform)
def _extract_youtube_id(self, url: str) -> Optional[str]:
"""Extract video ID from YouTube URL."""
parsed = urlparse(url)
if "youtu.be" in parsed.netloc:
return parsed.path.lstrip("/")
if "youtube.com" in parsed.netloc:
if parsed.path == "/watch":
return parse_qs(parsed.query).get("v", [None])[0]
elif "/shorts/" in parsed.path:
return parsed.path.split("/shorts/")[1].split("/")[0]
elif "/live/" in parsed.path:
return parsed.path.split("/live/")[1].split("/")[0]
return None
def _fetch_youtube(self, url: str, platform: str) -> FetchedContent:
"""Fetch YouTube video transcript and metadata."""
video_id = self._extract_youtube_id(url)
if not video_id:
return FetchedContent(
url=url,
platform=platform,
error="Could not extract YouTube video ID"
)
result = FetchedContent(url=url, platform=platform, metadata={"video_id": video_id})
# Try to get video title via oEmbed (no API key needed)
try:
oembed_url = f"https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v={video_id}&format=json"
resp = self.session.get(oembed_url, timeout=10)
if resp.ok:
data = resp.json()
result.title = data.get("title")
result.author = data.get("author_name")
except Exception:
pass
# Try to get transcript
try:
api = YouTubeTranscriptApi()
transcript_result = api.fetch(video_id)
# Format transcript with timestamps
lines = []
for snippet in transcript_result:
start = int(snippet.start)
mins, secs = divmod(start, 60)
hours, mins = divmod(mins, 60)
if hours:
timestamp = f"[{hours}:{mins:02d}:{secs:02d}]"
else:
timestamp = f"[{mins}:{secs:02d}]"
lines.append(f"{timestamp} {snippet.text}")
result.transcript = "\n".join(lines)
result.content = f"YouTube video with {len(transcript_result)} transcript segments"
except TranscriptsDisabled:
result.error = "Transcripts are disabled for this video"
except NoTranscriptFound:
result.error = "No transcript available for this video"
except Exception as e:
result.error = f"Could not fetch transcript: {str(e)}"
return result
def _fetch_reddit(self, url: str, platform: str) -> FetchedContent:
"""Fetch Reddit post content."""
result = FetchedContent(url=url, platform=platform)
try:
# Reddit JSON API - append .json to URL
json_url = url.rstrip("/") + ".json"
resp = self.session.get(json_url, timeout=10)
if not resp.ok:
result.error = f"Reddit returned status {resp.status_code}"
return result
data = resp.json()
# Post data is in first element
if data and len(data) > 0:
post = data[0]["data"]["children"][0]["data"]
result.title = post.get("title")
result.author = post.get("author")
result.content = post.get("selftext") or post.get("url")
result.metadata = {
"subreddit": post.get("subreddit"),
"score": post.get("score"),
"num_comments": post.get("num_comments"),
"created_utc": post.get("created_utc"),
}
# Get top comments if available
if len(data) > 1:
comments = data[1]["data"]["children"]
top_comments = []
for c in comments[:5]: # Top 5 comments
if c["kind"] == "t1":
comment_data = c["data"]
top_comments.append({
"author": comment_data.get("author"),
"body": comment_data.get("body", "")[:500],
"score": comment_data.get("score"),
})
if top_comments:
result.metadata["top_comments"] = top_comments
except Exception as e:
result.error = f"Could not fetch Reddit post: {str(e)}"
return result
def _fetch_twitter(self, url: str, platform: str) -> FetchedContent:
"""Fetch Twitter/X content (limited without API)."""
result = FetchedContent(url=url, platform=platform)
result.content = "Twitter/X content (requires API or manual viewing)"
result.error = "Twitter content requires API access"
return result
def _fetch_instagram(self, url: str, platform: str) -> FetchedContent:
"""Fetch Instagram content (limited without API)."""
result = FetchedContent(url=url, platform=platform)
try:
# Try to get oEmbed data
oembed_url = f"https://api.instagram.com/oembed?url={url}"
resp = self.session.get(oembed_url, timeout=10)
if resp.ok:
data = resp.json()
result.title = data.get("title")
result.author = data.get("author_name")
result.content = data.get("title") or "Instagram content"
result.metadata = {
"author_url": data.get("author_url"),
"thumbnail_url": data.get("thumbnail_url"),
}
else:
result.content = "Instagram content (requires login to view)"
result.error = "Instagram content is restricted"
except Exception as e:
result.error = f"Could not fetch Instagram content: {str(e)}"
result.content = "Instagram content (could not fetch)"
return result
def _fetch_linkedin(self, url: str, platform: str) -> FetchedContent:
"""Fetch LinkedIn content (limited without API)."""
result = FetchedContent(url=url, platform=platform)
result.content = "LinkedIn content (requires login to view full content)"
result.error = "LinkedIn content is restricted without authentication"
return result
def _fetch_web(self, url: str, platform: str) -> FetchedContent:
"""Fetch generic web page content."""
result = FetchedContent(url=url, platform=platform)
try:
resp = self.session.get(url, timeout=15)
if not resp.ok:
result.error = f"HTTP {resp.status_code}"
return result
# Basic extraction - title from <title> tag
html = resp.text
title_match = re.search(r"<title[^>]*>([^<]+)</title>", html, re.IGNORECASE)
if title_match:
result.title = title_match.group(1).strip()
# Try to extract main content (very basic)
# Remove script and style tags
content = re.sub(r"<script[^>]*>.*?</script>", "", html, flags=re.DOTALL | re.IGNORECASE)
content = re.sub(r"<style[^>]*>.*?</style>", "", content, flags=re.DOTALL | re.IGNORECASE)
# Remove HTML tags
content = re.sub(r"<[^>]+>", " ", content)
# Clean up whitespace
content = re.sub(r"\s+", " ", content).strip()
# Truncate to reasonable length
result.content = content[:5000] if len(content) > 5000 else content
except Exception as e:
result.error = f"Could not fetch page: {str(e)}"
return result
def extract_links(self, text: str) -> list[str]:
"""Extract all URLs from text."""
url_pattern = r'https?://[^\s<>"{}|\\^`\[\]]+'
return re.findall(url_pattern, text)
def main():
"""CLI for testing content fetcher."""
import argparse
parser = argparse.ArgumentParser(description="Fetch content from URLs")
parser.add_argument("url", help="URL to fetch")
parser.add_argument("--json", action="store_true", help="Output as JSON")
args = parser.parse_args()
fetcher = ContentFetcher()
result = fetcher.fetch(args.url)
if args.json:
output = {
"url": result.url,
"platform": result.platform,
"title": result.title,
"author": result.author,
"content": result.content[:500] if result.content else None,
"has_transcript": bool(result.transcript),
"transcript_length": len(result.transcript) if result.transcript else 0,
"error": result.error,
"metadata": result.metadata,
}
print(json.dumps(output, indent=2))
else:
print(f"Platform: {result.platform}")
print(f"Title: {result.title or 'N/A'}")
print(f"Author: {result.author or 'N/A'}")
if result.error:
print(f"Error: {result.error}")
if result.content:
print(f"\nContent preview:\n{result.content[:500]}...")
if result.transcript:
print(f"\nTranscript ({len(result.transcript)} chars):")
print(result.transcript[:1000] + "..." if len(result.transcript) > 1000 else result.transcript)
if __name__ == "__main__":
main()