yt package change
Browse files
main.py
CHANGED
|
@@ -8,7 +8,8 @@ import os
|
|
| 8 |
import requests
|
| 9 |
from bs4 import BeautifulSoup
|
| 10 |
import validators
|
| 11 |
-
|
|
|
|
| 12 |
# from cachetools import TTLCache
|
| 13 |
# import asyncio
|
| 14 |
# import hashlib
|
|
@@ -200,36 +201,72 @@ def genetate_gemini_content(prompt,content):
|
|
| 200 |
response=model.generate_content([prompt,content])
|
| 201 |
return response.text
|
| 202 |
|
| 203 |
-
def extract_transcript(youtube_link):
|
| 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 |
|
|
|
|
| 8 |
import requests
|
| 9 |
from bs4 import BeautifulSoup
|
| 10 |
import validators
|
| 11 |
+
import yt_dlp
|
| 12 |
+
# from youtube_transcript_api import YouTubeTranscriptApi
|
| 13 |
# from cachetools import TTLCache
|
| 14 |
# import asyncio
|
| 15 |
# import hashlib
|
|
|
|
| 201 |
response=model.generate_content([prompt,content])
|
| 202 |
return response.text
|
| 203 |
|
| 204 |
+
# def extract_transcript(youtube_link):
|
| 205 |
+
# transcript = ""
|
| 206 |
+
# ytt_api = YouTubeTranscriptApi()
|
| 207 |
|
| 208 |
+
# try:
|
| 209 |
+
# if not validators.url(youtube_link):
|
| 210 |
+
# return 400, "Enter correct URL"
|
| 211 |
|
| 212 |
|
| 213 |
+
# if "youtu.be/" in youtube_link:
|
| 214 |
+
# video_id = youtube_link.split("youtu.be/")[1].split("?")[0]
|
| 215 |
+
# else:
|
| 216 |
+
# video_id = youtube_link.split("v=")[1].split("&")[0]
|
| 217 |
|
| 218 |
|
| 219 |
+
# transcript_data = ytt_api.fetch(video_id)
|
| 220 |
+
# print(transcript_data)
|
| 221 |
|
| 222 |
|
| 223 |
+
# for i in transcript_data.snippets:
|
| 224 |
+
# transcript += " " + i.text
|
| 225 |
|
| 226 |
+
# return 200, transcript.strip()
|
| 227 |
|
| 228 |
+
# except Exception as e:
|
| 229 |
+
# print("Transcript error:", e)
|
| 230 |
+
# return 404, "No information available"
|
| 231 |
|
| 232 |
|
| 233 |
+
|
| 234 |
+
|
| 235 |
+
def extract_transcript(url):
|
| 236 |
+
transcript = ""
|
| 237 |
+
|
| 238 |
+
try:
|
| 239 |
+
ydl_opts = {
|
| 240 |
+
"skip_download": True,
|
| 241 |
+
"quiet": True,
|
| 242 |
+
"writesubtitles": True,
|
| 243 |
+
"writeautomaticsub": True,
|
| 244 |
+
"subtitlesformat": "vtt",
|
| 245 |
+
"subtitleslangs": ["en"],
|
| 246 |
+
}
|
| 247 |
+
|
| 248 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 249 |
+
info = ydl.extract_info(url, download=False)
|
| 250 |
+
subs = info.get("subtitles") or info.get("automatic_captions")
|
| 251 |
+
|
| 252 |
+
if not subs or "en" not in subs:
|
| 253 |
+
return 404, "No transcript available"
|
| 254 |
+
|
| 255 |
+
vtt_url = subs["en"][0]["url"]
|
| 256 |
+
|
| 257 |
+
# download VTT
|
| 258 |
+
import requests
|
| 259 |
+
vtt_data = requests.get(vtt_url).text
|
| 260 |
+
|
| 261 |
+
# convert VTT → text
|
| 262 |
+
text = "\n".join([line for line in vtt_data.split("\n") if "-->" not in line and line.strip()])
|
| 263 |
+
|
| 264 |
+
return 200, text
|
| 265 |
+
|
| 266 |
+
except Exception as e:
|
| 267 |
+
print("Error:", e)
|
| 268 |
+
return 404, "No transcript available"
|
| 269 |
+
|
| 270 |
|
| 271 |
|
| 272 |
|