Update main.py
Browse files
main.py
CHANGED
|
@@ -18,6 +18,7 @@ import subprocess
|
|
| 18 |
import pdfplumber
|
| 19 |
import pandas as pd
|
| 20 |
import json
|
|
|
|
| 21 |
|
| 22 |
app = FastAPI()
|
| 23 |
|
|
@@ -431,13 +432,21 @@ async def youtube_to_pdf(url: str = Form(...)):
|
|
| 431 |
if not video_id:
|
| 432 |
raise Exception("Could not extract Video ID.")
|
| 433 |
|
| 434 |
-
# 2. Fetch the Transcript (
|
| 435 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 436 |
|
| 437 |
-
# Combine the text chunks into
|
| 438 |
-
full_text = ""
|
| 439 |
-
for item in transcript_list:
|
| 440 |
-
full_text += item['text'] + " "
|
| 441 |
|
| 442 |
# 3. Create the PDF
|
| 443 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_pdf:
|
|
|
|
| 18 |
import pdfplumber
|
| 19 |
import pandas as pd
|
| 20 |
import json
|
| 21 |
+
import urllib.parse
|
| 22 |
|
| 23 |
app = FastAPI()
|
| 24 |
|
|
|
|
| 432 |
if not video_id:
|
| 433 |
raise Exception("Could not extract Video ID.")
|
| 434 |
|
| 435 |
+
# 2. Fetch the Transcript (Using the NEW Object-Oriented syntax)
|
| 436 |
+
yt_api = YouTubeTranscriptApi()
|
| 437 |
+
|
| 438 |
+
try:
|
| 439 |
+
# Get the list of all available transcripts for this video
|
| 440 |
+
transcripts = yt_api.list(video_id)
|
| 441 |
+
|
| 442 |
+
# Grab the very first available transcript (works for any language!)
|
| 443 |
+
transcript = next(iter(transcripts))
|
| 444 |
+
transcript_data = transcript.fetch()
|
| 445 |
+
except Exception as e:
|
| 446 |
+
raise Exception("Transcripts are disabled or unavailable for this video.")
|
| 447 |
|
| 448 |
+
# Combine the text chunks into a single string
|
| 449 |
+
full_text = " ".join([item['text'] for item in transcript_data])
|
|
|
|
|
|
|
| 450 |
|
| 451 |
# 3. Create the PDF
|
| 452 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_pdf:
|