-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutube.py
375 lines (309 loc) · 12.6 KB
/
youtube.py
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import asyncio
import re
import json
import traceback
from typing import Optional, Tuple, List, Dict
from pathlib import Path
import requests
from openai import AsyncOpenAI
import aiofiles
from yt_dlp import YoutubeDL
from youtube_transcript_api import YouTubeTranscriptApi
from config import load_env_var
# Configuration constants
CACHE_DIR = Path("transcript_cache")
OUTPUTS_DIR = Path("outputs")
TELEGRAM_BOT_TOKEN = load_env_var("TELEGRAM_BOT_TOKEN", prompt_if_missing=False)
TELEGRAM_CHAT_ID = load_env_var("TELEGRAM_CHAT_ID", prompt_if_missing=False)
# OpenAI client initialization
client = AsyncOpenAI(
base_url="https://openrouter.ai/api/v1",
api_key=load_env_var("OPENROUTER_API_KEY"),
)
def send_telegram_message(message: str) -> None:
"""
Send a message to Telegram.
Args:
message: The message to send
"""
if not TELEGRAM_BOT_TOKEN or not TELEGRAM_CHAT_ID:
print("Telegram configuration not found. Skipping notification.")
return
try:
url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage"
data = {
"chat_id": TELEGRAM_CHAT_ID,
"text": message,
"parse_mode": "HTML"
}
response = requests.post(url, json=data)
response.raise_for_status()
except Exception as e:
print(f"Failed to send Telegram message: {e}")
def ensure_directories() -> None:
"""Ensure required directories exist."""
CACHE_DIR.mkdir(exist_ok=True)
OUTPUTS_DIR.mkdir(exist_ok=True)
def get_cache_path(video_id: str) -> Path:
"""
Get the cache file path for a video ID.
Args:
video_id: YouTube video ID
Returns:
Path object for the cache file
"""
return CACHE_DIR / f"{video_id}.json"
async def read_from_cache(video_id: str) -> Tuple[Optional[str], Optional[str]]:
"""
Read transcript data from cache if it exists.
Args:
video_id: YouTube video ID
Returns:
Tuple of (title, transcript) if found, else (None, None)
"""
try:
cache_path = get_cache_path(video_id)
if cache_path.exists():
async with aiofiles.open(cache_path, "r", encoding="utf-8") as f:
cache_data = json.loads(await f.read())
return cache_data["title"], cache_data["transcript"]
except Exception as e:
send_telegram_message(f"❌ Error reading from cache: {str(e)}\n\n{traceback.format_exc()}")
return None, None
async def write_to_cache(video_id: str, title: str, transcript: str) -> None:
"""
Write transcript data to cache.
Args:
video_id: YouTube video ID
title: Video title
transcript: Video transcript
"""
try:
cache_path = get_cache_path(video_id)
cache_data = {"title": title, "transcript": transcript}
async with aiofiles.open(cache_path, "w", encoding="utf-8") as f:
await f.write(json.dumps(cache_data, ensure_ascii=False, indent=2))
except Exception as e:
send_telegram_message(f"❌ Error writing to cache: {str(e)}\n\n{traceback.format_exc()}")
async def fetch_transcript(video_url: str) -> Tuple[Optional[str], Optional[str]]:
"""
Fetch the YouTube transcript and title given a URL.
Args:
video_url: YouTube video URL
Returns:
Tuple of (title, transcript) if successful, else (None, None)
"""
try:
ensure_directories()
ydl_opts = {
"format": "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best",
"outtmpl": "%(id)s.%(ext)s",
"noplaylist": True,
"writesubtitles": True,
"subtitleslangs": ["en"],
"skip_download": True,
}
with YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(video_url, download=False)
video_id = info_dict["id"]
title = info_dict["title"]
# Check cache first
cached_title, cached_transcript = await read_from_cache(video_id)
if cached_title and cached_transcript:
print(f"Using cached transcript for video: {video_id}")
return cached_title, cached_transcript
# Fetch transcript from YouTube
transcript = await get_youtube_transcript(video_id)
if transcript:
await write_to_cache(video_id, title, transcript)
return title, transcript
except Exception as e:
error_msg = f"❌ Error fetching video info for {video_url}: {str(e)}\n\n{traceback.format_exc()}"
send_telegram_message(error_msg)
print(error_msg)
return None, None
async def get_youtube_transcript(video_id: str) -> Optional[str]:
"""
Get transcript for a YouTube video.
Args:
video_id: YouTube video ID
Returns:
Transcript text if successful, else None
"""
try:
transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
transcript = None
# Try to get English transcript first
for t in transcript_list:
if t.language_code.startswith("en"):
transcript = t.fetch()
break
# Fall back to any available transcript
if not transcript:
transcript = transcript_list.find_transcript(
transcript_list._manually_created_transcripts
+ transcript_list._generated_transcripts
).fetch()
return " ".join([seg["text"] for seg in transcript]) if transcript else None
except Exception as e:
error_msg = f"❌ Error fetching transcript: {str(e)}\n\n{traceback.format_exc()}"
send_telegram_message(error_msg)
print(error_msg)
return None
async def call_openai_api(prompt: str) -> Optional[str]:
"""
Call OpenAI API for transcript normalization.
Args:
prompt: Input prompt for the API
Returns:
API response text if successful, else None
"""
try:
response = await client.chat.completions.create(
model="anthropic/claude-3.5-sonnet:beta",
messages=[
{
"role": "system",
"content": "You need to continue generation that was left by the previous AI assistant. DO NOT output anything which is not explicitly defined in its guidelines.",
},
{"role": "user", "content": prompt},
],
)
return response.choices[0].message.content
except Exception as e:
error_msg = f"❌ Error calling OpenAI API: {str(e)}\n\n{traceback.format_exc()}"
send_telegram_message(error_msg)
print(error_msg)
raise
async def normalize_transcript(title: str, transcript: str, glossary: str) -> Optional[str]:
"""
Normalize transcript using OpenAI API.
Args:
title: Video title
transcript: Raw transcript
glossary: Reference glossary
Returns:
Normalized transcript if successful, else None
"""
try:
prompt = (
"<START GAINING KNOWLEDGE>"
+ glossary
+ "<END GAINING KNOWLEDGE> <START AUTOMATICALLY GENERATED TRANSCRIPT>"
+ title
+ "\n\n"
+ transcript
+ "<END AUTOMATICALLY GENERATED TRANSCRIPT> <START GUIDELINES FOR THE ASSISTANT> "
"Normalize this transcript verbatim from the start to the end. Do not write anything else. "
"The original transcript was automatically generated, so it contains mistakes and incorrectly transcribed terms that you should already know. "
"Stop only when you hit the length limit. Do not output that you've reached the limit. If you reach the end of the original transcript, write THE END. "
"<END GUIDELINES FOR THE ASSISTANT> <START NORMALIZED TRANSCRIPT>"
)
prev_result = ""
while True:
result = await call_openai_api(prompt)
if not result:
return None
print(f"Processing: {result[:20]}...")
# sometimes it gives the equivalent stuff
if result[:20] != prev_result[:20]:
prompt += result
if result.strip().endswith("THE END") and all([x not in result for x in ["[", "]"]]):
break
return prompt.split("<START NORMALIZED TRANSCRIPT>")[1]
except Exception as e:
error_msg = f"❌ Error normalizing transcript: {str(e)}\n\n{traceback.format_exc()}"
send_telegram_message(error_msg)
print(error_msg)
raise
def sanitize_filename(filename: str) -> str:
"""
Create a safe filename from a title.
Args:
filename: Original filename
Returns:
Sanitized filename
"""
filename = filename.strip().replace(" ", "_")
filename = re.sub(r"[^A-Za-z0-9_\-\.]", "", filename)
return filename[:200]
async def process_video(url: str, glossary: str, topic_name: str) -> None:
"""
Process a single video URL.
Args:
url: YouTube video URL
glossary: Reference glossary
topic_name: Name of the topic for organizing output
"""
try:
title, transcript = await fetch_transcript(url)
if not transcript:
error_msg = f"❌ No transcript found for: {url}"
send_telegram_message(error_msg)
print(error_msg)
return
print(f"Normalizing transcript for: {title}")
normalized = await normalize_transcript(title, transcript, glossary)
if not normalized:
return
# Create topic directory
topic_dir = OUTPUTS_DIR / sanitize_filename(topic_name)
topic_dir.mkdir(exist_ok=True)
safe_title = sanitize_filename(title) or "video"
output_file = topic_dir / f"{safe_title}.txt"
async with aiofiles.open(output_file, "w", encoding="utf-8") as f:
await f.write(normalized)
success_msg = f"✅ Successfully processed video:\nTopic: {topic_name}\nTitle: {title}\nOutput: {output_file}"
send_telegram_message(success_msg)
print(f"Saved normalized transcript to {output_file}")
except Exception as e:
error_msg = f"❌ Error processing video {url}: {str(e)}\n\n{traceback.format_exc()}"
send_telegram_message(error_msg)
print(error_msg)
def main() -> None:
"""Main execution function."""
try:
# Dictionary to store topics and their associated videos
topics: Dict[str, Dict[str, List[str]]] = {}
while True:
glossary_file = input("Enter the path to the glossary/docs .txt file (press Enter to stop adding topics): ").strip().replace("'", "")
if not glossary_file:
break
glossary_path = Path(glossary_file)
if not glossary_path.exists():
print("❌ Glossary file not found. Please provide a valid file path.")
continue
# Use the filename (without extension) as the topic name
topic_name = glossary_path.stem
with open(glossary_path, "r", encoding="utf-8") as gf:
glossary = gf.read()
video_urls: List[str] = []
print(f"\nEnter YouTube video URLs for topic '{topic_name}':")
while True:
video_url = input("Enter YouTube video URL (press Enter when done with this topic): ").strip()
if not video_url:
break
video_urls.append(video_url)
if video_urls:
topics[topic_name] = {
"glossary": glossary,
"videos": list(set(video_urls)) # Remove duplicates
}
print(f"Added {len(video_urls)} videos for topic '{topic_name}'\n")
if not topics:
print("No topics or videos provided.")
return
async def process_all_videos() -> None:
tasks = []
for topic_name, topic_data in topics.items():
for url in topic_data["videos"]:
tasks.append(process_video(url, topic_data["glossary"], topic_name))
await asyncio.gather(*tasks)
print(f"\nProcessing {sum(len(t['videos']) for t in topics.values())} videos across {len(topics)} topics...")
asyncio.run(process_all_videos())
except Exception as e:
error_msg = f"❌ Error in main function: {str(e)}\n\n{traceback.format_exc()}"
send_telegram_message(error_msg)
print(error_msg)
if __name__ == "__main__":
main()